 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

August 30th, 2012, 04:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
While this works, it violates the Dry Principle.
Your code is identical in both cases. Move it to a Sub and then call it from both places, Also, in Load, don't load the data after a PostBack. You end up with this:
Code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Protected Sub DetailsView1_ItemInserted(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
BindData()
End Sub
Private Sub BindData()
Using myEntities As New AlumniTblEntities
Dim allProvinces = From province In myEntities.ProvinceTbls
Order By province.Province
Select New With {province.Province}
ProvinceGrid.DataSource = allProvinces
ProvinceGrid.DataBind()
ProvinceGrid.HeaderRow.Cells(0).Text = "Provinces"
ProvinceDdl.DataSource = allProvinces
ProvinceDdl.DataValueField = "Province"
ProvinceDdl.DataTextField = "Province"
ProvinceDdl.DataBind()
End Using
End Sub
Much cleaner, if you ask me....
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

August 30th, 2012, 05:09 AM
|
|
Authorized User
|
|
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
is BindData() a function created by you?
|
|

August 30th, 2012, 05:23 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes, don't you see it in the code?
Imar
|
|

August 30th, 2012, 05:25 AM
|
|
Authorized User
|
|
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
sorry i dont know how to do functions in visual basic only in c/c++ so i have no idea on how to do it nor recognize it.
|
|

August 30th, 2012, 05:26 AM
|
|
Authorized User
|
|
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
and thanks for the new knowledge :)
|
|

August 30th, 2012, 07:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
i dont know how to do functions in visual basic
|
I would try to master that before you go the "all code route". Chapter 5 shows how to create functions and sub routines.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

August 30th, 2012, 07:40 PM
|
|
Authorized User
|
|
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
thanks. im going to study that XD
|
|

August 30th, 2012, 08:36 PM
|
|
Authorized User
|
|
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
is it possible to do a file for a class then call it on another file by importing it?
|
|

August 31st, 2012, 02:25 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I have no idea what you're asking.
Also, please create a new thread for a new topic, in an appropriate forum. This question doesn't seem to be related to ItemInserted, nor to my book directly. So, a new post in the General ASP.NET 4 category is probably the right thing to do.
Cheers,
Imar
|
|
 |
|