Whether the Page.IsPostBack property returns true or false is usually not the issue. The page might be in a postback and still not need to have any dynamic controls loaded. It might require a specific interaction with the page that causes the first dynamic
control to be loaded. Ultimately, it is important to keep track of what dynamic controls (if any) may have been loaded and then continue to load them early in the page life-cycle on all subsequent postbacks.
If Button "Add New" didn't press i delete the last dropdownlist beacuse another event happened and decrease the session counter by one.
My question is " Can i make my code better ?" , i don't want to create and delete controls after . I decided to do this because the "Add New" Event fires between Page_Load and PreRender steps and the only way to create a control that its handlers are registered
is to create before PreRender. So I create it at Init It could be in Page_Load as well and then if the button pressed i don't delete it.
When I first started working with the Accordian control in an update Panel I had this issue. I was data binding a lot of things so as a result, I kept having to use a lot of caching in order to not over hit the database but also to retain the controls
on the page. This is a pretty good way to do it from what I see. I need to figure out VB a bit more as I really only us C#.
This is amazing article explaing in detail how to get the dynamic controls value after the potback occurs.
I am working on C# and I can't seem to find AddHadler and could't convert your code to C# because of other issues. Can you please detail me how do I convert your code into C# or if you have a C# version of the above code, can you please provide me.
I am having a dropdownlist when I am assigning the values to it as shown below it is working fine when I am retreiving the value in other control events.
But if I am assigning the values as below it is not allowing me to read the .selected value. it returns ""
drSocket.Items.Clear()
drSocket.Items.Add(New ListItem(strSelect, strSelect))
For Each dr As DataRow In dsSocket.Tables(0).Rows
drSocket.Items.Add(New ListItem(dr.Item("SOCKET"), dr.Item("SOCKET")))
Next
I am using DirectCast(item.FindControl("drSocket"), DropDownList).SelectedValue to retreive the value in other events.
mbanavige
All-Star
134963 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Apr 08, 2008 10:49 PM|LINK
Whether the Page.IsPostBack property returns true or false is usually not the issue. The page might be in a postback and still not need to have any dynamic controls loaded. It might require a specific interaction with the page that causes the first dynamic control to be loaded. Ultimately, it is important to keep track of what dynamic controls (if any) may have been loaded and then continue to load them early in the page life-cycle on all subsequent postbacks.
spavkov
Member
144 Points
26 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Apr 20, 2008 09:50 AM|LINK
This subject is always problematic to the beginner asp.net developers and this post does a good job clearing things out.
Thumbs Up.
ASP.NET FAQs
Fluent DateTime Project
Click "Mark as Answer" on the post that helped you to help future readers.
koraki_g
Member
2 Points
1 Post
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
May 07, 2008 10:08 AM|LINK
Hi , i want to ask something related to this thread.
I want to add a new DropDownList to a panel only when user clicks button "Add New", and add an event handler CheckedChanged to this control.
I also want to have the viewstate of every dropdownlist i have already added in order to use it when user clicks "Submit" (another button).
I figured out that the solution is ( according to lifecycle of aspx page ) :
PageInit
if (Session["countDDL"] == null) Session["countDDL"] = 0; Session["countDDL"] = int.Parse(Session["countDDL"].ToString()) + 1; for (int i = 0; i < int.Parse(Session["countDDL"].ToString()); i++) { DropDownList processes = new DropDownList(); ListItem lsi = new ListItem("Choose "); processes.ID = "processes" + i.ToString(); processes.Width = 150; processes.Items.Add(lsi); processes.AutoPostBack = true; FillList(processes); processes.SelectedIndexChanged += new EventHandler(processes_SelectedIndexChanged); pnlProcesses.Controls.Add(processes); }Create and fill all the dropdownlists that exist plus one more . The Viewstate works fine if the IDs are the same.
PageLoad
Reset Session key.
If Button "Add New" didn't press i delete the last dropdownlist beacuse another event happened and decrease the session counter by one.
My question is " Can i make my code better ?" , i don't want to create and delete controls after . I decided to do this because the "Add New" Event fires between Page_Load and PreRender steps and the only way to create a control that its handlers are registered is to create before PreRender. So I create it at Init It could be in Page_Load as well and then if the button pressed i don't delete it.
Thanx in advance,
koraki_g
dynamic c# web forms
paul_mendoza
Member
16 Points
14 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
May 10, 2008 12:01 AM|LINK
When I first started working with the Accordian control in an update Panel I had this issue. I was data binding a lot of things so as a result, I kept having to use a lot of caching in order to not over hit the database but also to retain the controls on the page. This is a pretty good way to do it from what I see. I need to figure out VB a bit more as I really only us C#.
DEEPAK VODET...
Member
10 Points
20 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Oct 17, 2008 06:47 PM|LINK
This is amazing article explaing in detail how to get the dynamic controls value after the potback occurs.
I am working on C# and I can't seem to find AddHadler and could't convert your code to C# because of other issues. Can you please detail me how do I convert your code into C# or if you have a C# version of the above code, can you please provide me.
-Deepak
parshuram
Member
7 Points
21 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Dec 17, 2008 02:29 AM|LINK
Hi,
I am having a dropdownlist when I am assigning the values to it as shown below it is working fine when I am retreiving the value in other control events.
drSocket.DataSource = dsSocket
drSocket.DataTextField = "SOCKET"
drSocket.DataValueField = "SOCKET"
drSocket.DataBind()
But if I am assigning the values as below it is not allowing me to read the .selected value. it returns ""
drSocket.Items.Clear()
drSocket.Items.Add(New ListItem(strSelect, strSelect))
For Each dr As DataRow In dsSocket.Tables(0).Rows
drSocket.Items.Add(New ListItem(dr.Item("SOCKET"), dr.Item("SOCKET")))
Next
I am using DirectCast(item.FindControl("drSocket"), DropDownList).SelectedValue to retreive the value in other events.
They this is happening?
Drop down list post backs DropDownList ASP.NET .net
sirdneo
All-Star
15171 Points
2509 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Dec 25, 2008 04:49 AM|LINK
Nice post, this issue always consufe every new asp.net developer. This post will be a usefull link for them.
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
davidfowl
Contributor
2683 Points
608 Posts
Microsoft
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Jan 01, 2009 05:31 AM|LINK
http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx
Senior SDE, ASP.NET Team, Microsoft
vsurana
Member
28 Points
24 Posts
Re: dynamic control help plzzzzzzzzzzzzzzz.....
Feb 12, 2009 09:52 AM|LINK
i want to create dynamic controls mainly textbox and can be any number
i have created it dynamically and assign id to them but when i enter values in these textboxes
i cant read them on button click.
So how to retrive values from dynamically created textboxes on button click
Thanks in Advance
naseem124
Member
2 Points
3 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Mar 20, 2009 11:41 AM|LINK
Hi,
but in onClick() method if i want to assign value say integer value=90 or a string "xyz" to the textbox
it not works .value not be able to assign, Can u give some solution ?
Thanks
asp with c#.net