Wrox Programmer Forums
|
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
 
Old August 30th, 2012, 04:49 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
rmanapul (August 30th, 2012)
 
Old August 30th, 2012, 05:09 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

is BindData() a function created by you?
 
Old August 30th, 2012, 05:23 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, don't you see it in the code?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old August 30th, 2012, 05:25 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

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.
 
Old August 30th, 2012, 05:26 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

and thanks for the new knowledge :)
 
Old August 30th, 2012, 07:51 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
rmanapul (August 30th, 2012)
 
Old August 30th, 2012, 07:40 PM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

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

is it possible to do a file for a class then call it on another file by importing it?
 
Old August 31st, 2012, 02:25 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 12: ItemInserted; Endediting not declared ChristopherS BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 0 September 28th, 2011 11:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.