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 29th, 2012, 04:41 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default ItemInserting or ItemInserted?

what will i do to make the dropdownlist values change after i insert a new data in a details view? ItemInserting or ItemInserted?


Code:
Imports AlumniTblModel
Partial Class Management_Sample
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Using myEntities As New AlumniTblEntities

            Dim allProvinces = From province In myEntities.ProvinceTbls
                               Order By province.Province
                               Select province.Province




            ProvinceGrid.DataSource = allProvinces
            ProvinceGrid.DataBind()
            ProvinceGrid.HeaderRow.Cells(0).Text = "Provinces"

        End Using
    End Sub

    Protected Sub DetailsView1_ItemInserted(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted

        Using myEntities As New AlumniTblEntities

            Dim allProvinces = From province In myEntities.ProvinceTbls
                               Order By province.Province
                               Select province.Province



            If Not IsPostBack Then
                With ProvinceDdl
                    .DataValueField = "Province"
                    .DataTextField = "Province"
                    .DataSource = allProvinces
                End With
                DataBind()

            End If

        End Using

    End Sub

End Class
I used both ItemInserting and ItemInserted but the DropDownList Control values does not update unless you close the browser and re-run the application again.
 
Old August 29th, 2012, 05:28 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. Sorry.

Also, since this is not directly related to my book, please post questions like these in a general ASP.NET catergory from this list: http://p2p.wrox.com/asp-net-4-539/

heers,

Imnar
__________________
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, 02:58 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

yes it is related on your book sir. Im using the things i learned from chapter 13 page 459 under the title Using DetailsView to Insert and Update Data.
 
Old August 30th, 2012, 03:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

But I still don't understand the question.... ;-)

Maybe it's the check for IsPostBack? That returns true in the ItemInserted event.

Also, consider moving code like this to a sub or function. The stuff in Load and ItemIsnerted is almost the same, forcing you to code and maintain similar code in multiple locations.

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!

Last edited by Imar; August 30th, 2012 at 03:40 AM..
 
Old August 30th, 2012, 03:37 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

what i mean is this. if i entered a data in the detailsview, the dropdownlist control which display the data inserted using the detailsview should be updated after i insert the data. but it seems not it. i have to close the browser, and re run the application again to see if the data is added in the drop down list
 
Old August 30th, 2012, 03:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hence my remark: Maybe it's the check for IsPostBack? That returns true in the ItemInserted event. You're not rebinding the drop down list in ItemInserted.

Debug your page and break into ItemInserted to see what I mean....

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, 04:03 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

can you suggest some things how to do rebinding in the dropdownlist after a data has been inserted? i don't know about the load function. and is there a refresh function for dropdownlist?
 
Old August 30th, 2012, 04:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're doing the right thing; you just need to remove the check for IsPostback. With Load, I was referring to Page_Load.

FWIIW: all of this is taken care off if you use the data-source controls. I still haven't seen a compelling reason to do all of this in code. More work, more maintenance, while, IMO, it doesn't add much value here.

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, 04:15 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

yeah im doing this all in hard coding LINQ to Entities for me to learn more of it in coding practice.
 
Old August 30th, 2012, 04:42 AM
Authorized User
 
Join Date: May 2012
Posts: 29
Thanks: 11
Thanked 0 Times in 0 Posts
Default

thanks i found a solution by using this code:

Code:
Imports AlumniTblModel
Partial Class Management_Sample
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        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

    Protected Sub DetailsView1_ItemInserted(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
        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
End Class
it works perfectly now :)





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.