FAQ: Why do dynamic controls disappear on postback and not raise events?http://forums.asp.net/t/1186195.aspx/1?FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Fri, 04 Jun 2010 19:01:18 -040011861952024280http://forums.asp.net/p/1186195/2024280.aspx/1?FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+FAQ: Why do dynamic controls disappear on postback and not raise events? <p>One common error that many have run into when first starting out with creating dynamic page controls is an incorrect assumption that those dynamic controls will survive page postbacks.</p> <p>For example -&nbsp;a common scenario is to:</p> <ol> <li>&lt;div mce_keep=&quot;true&quot;&gt;Declare a button (Button1) in your page markup&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Add an event handler for Button1 and in that handler, create a new dynamic button (Button2) &lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Wire up and event handler for Button2&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Page loads an you see Button1&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Click on Button1 and the page posts-back Button1's Click event fires and you now see Button1 and Button2&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Click on Button2 and...the page posts-back, Button2 disappears and no event for Button2 is raised.&lt;/div&gt;</li></ol> <p>What happened to Button2?</p> <p>Since Button2 is not part of your pages declared markup, the framework has no way to know if it should be recreated or not on a postback. You'll need to keep track of the fact that you have <br> created dynamic control(s) and you will need to add the necessary code to recreate those dynamic control(s) yourself on all subsequent page postbacks.&nbsp; In order for your dynamic control(s) <br> to work correctly, you'll need to get them all recreated by the Page_Load event <u> at the latest</u>.</p> <p>The following example demonstrates how to toggle between 2 different sets of dynamic controls and how to handle events raised from those controls.&nbsp; In this example, I've chosen ViewState as the place where i will store the information needed to know which dynamic controls need to be recreated on a postback.</p> <p>First - here's the basic&nbsp;page markup:</p> <pre class="prettyprint">&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div&gt; &lt;hr /&gt; &lt;asp:Button ID=&quot;cmdAlphabet&quot; runat=&quot;server&quot; Text=&quot;Load Alphabet&quot; /&gt; &lt;asp:Button ID=&quot;cmdNumbers&quot; runat=&quot;server&quot; Text=&quot;Load Numbers&quot; /&gt; &lt;asp:Label ID=&quot;lblViewStateValue&quot; runat=&quot;server&quot; Text=&quot;&quot; EnableViewState=&quot;false&quot;&gt;&lt;/asp:Label&gt; &lt;hr /&gt; &lt;asp:PlaceHolder ID=&quot;PlaceHolder1&quot; runat=&quot;server&quot;&gt;&lt;/asp:PlaceHolder&gt; &lt;hr /&gt; &lt;asp:Label ID=&quot;lblClickResult&quot; runat=&quot;server&quot; Text=&quot;&quot; EnableViewState=&quot;false&quot;&gt;&lt;/asp:Label&gt; &lt;/div&gt; &lt;/form&gt;</pre> <P>And here's the associated codebehind:</P><pre class="prettyprint">Partial <SPAN class=kwd>Class</SPAN> DynamicControls <SPAN class=kwd>Inherits</SPAN> System.Web.UI.Page <SPAN class=kwd>Const</SPAN> ALPHABET_SELECTION <SPAN class=kwd>As String</SPAN> = <SPAN class=st>"ALPHABET"</SPAN> <SPAN class=kwd>Const</SPAN> NUMBER_SELECTION <SPAN class=kwd>As String</SPAN> = <SPAN class=st>"NUMBERS"</SPAN> <SPAN class=kwd>Const</SPAN> VIEWSTATEKEY_DYNCONTROL <SPAN class=kwd>As String</SPAN> = <SPAN class=st>"DynamicControlSelection"</SPAN> <SPAN class=cmt>'store property value in viewstate so that it will survive postbacks </SPAN> <SPAN class=kwd>Private Property</SPAN> DynamicControlSelection() <SPAN class=kwd>As String Get Dim</SPAN> result <SPAN class=kwd>As String</SPAN> = ViewState.Item(VIEWSTATEKEY_DYNCONTROL) <SPAN class=kwd>If</SPAN> result <SPAN class=kwd>Is Nothing Then</SPAN> <SPAN class=cmt>'doing things like this lets us access this property without 'worrying about this property returning null/Nothing </SPAN> <SPAN class=kwd>Return String</SPAN>.Empty <SPAN class=kwd>Else Return</SPAN> result <SPAN class=kwd>End If End Get Set</SPAN>(<SPAN class=kwd>ByVal</SPAN> value <SPAN class=kwd>As String</SPAN>) ViewState.Item(VIEWSTATEKEY_DYNCONTROL) = value <SPAN class=kwd>End Set End Property Protected Sub</SPAN> Page_Load(<SPAN class=kwd>ByVal</SPAN> sender <SPAN class=kwd>As Object</SPAN>, <SPAN class=kwd>ByVal</SPAN> e <SPAN class=kwd>As</SPAN> System.EventArgs) _ <SPAN class=kwd>Handles Me</SPAN>.Load <SPAN class=cmt>'running this code on every page_load - even when it's a postback 'check our page property (that we stored in viewstate) to see 'if we need to load a specific set of dynamic controls </SPAN> <SPAN class=kwd>Select Case Me</SPAN>.DynamicControlSelection <SPAN class=kwd>Case</SPAN> ALPHABET_SELECTION CreateDynamicAlphabetLinks() <SPAN class=kwd>Case</SPAN> NUMBER_SELECTION CreateDynamicNumberButtons() <SPAN class=kwd>Case Else</SPAN> <SPAN class=cmt>'no dynamic controls need to be loaded...yet </SPAN> <SPAN class=kwd>End Select End Sub Private Sub</SPAN> onClick(<SPAN class=kwd>ByVal</SPAN> sender <SPAN class=kwd>As Object</SPAN>, <SPAN class=kwd>ByVal</SPAN> e <SPAN class=kwd>As</SPAN> EventArgs) <SPAN class=cmt>'all of the dynamic linkbuttons/buttons will trigger this event handler 'since we used both linkbuttons and regular buttons for our dynamic controls, 'we will cast the sender control to an interface that is common to both 'of those button controls - the IButtonControl interface </SPAN> <SPAN class=kwd>Dim</SPAN> btn <SPAN class=kwd>As</SPAN> IButtonControl = <SPAN class=kwd>DirectCast</SPAN>(sender, IButtonControl) <SPAN class=kwd>Me</SPAN>.lblClickResult.Text = _ <SPAN class=kwd>String</SPAN>.Format(<SPAN class=st>"You clicked - CommandName: {0} CommandArgument: {1}"</SPAN>, _ btn.CommandName, btn.CommandArgument) <SPAN class=kwd>End Sub Protected Sub</SPAN> cmdAlphabet_Click(<SPAN class=kwd>ByVal</SPAN> sender <SPAN class=kwd>As Object</SPAN>, <SPAN class=kwd>ByVal</SPAN> e <SPAN class=kwd>As</SPAN> System.EventArgs) _ <SPAN class=kwd>Handles</SPAN> cmdAlphabet.Click <SPAN class=cmt>'user is selecting to show the dynamic Alphabet buttons </SPAN> <SPAN class=kwd>Me</SPAN>.CreateDynamicAlphabetLinks() <SPAN class=kwd>End Sub Protected Sub</SPAN> cmdNumbers_Click(<SPAN class=kwd>ByVal</SPAN> sender <SPAN class=kwd>As Object</SPAN>, <SPAN class=kwd>ByVal</SPAN> e <SPAN class=kwd>As</SPAN> System.EventArgs) _ <SPAN class=kwd>Handles</SPAN> cmdNumbers.Click <SPAN class=cmt>'user is selecting to show the dynamic Number buttons </SPAN> <SPAN class=kwd>Me</SPAN>.CreateDynamicNumberButtons() <SPAN class=kwd>End Sub Private Sub</SPAN> CreateDynamicAlphabetLinks() <SPAN class=cmt>'clear the placeholder first - in case something else was dynamically loaded </SPAN> <SPAN class=kwd>Me</SPAN>.PlaceHolder1.Controls.Clear() <SPAN class=cmt>'dynamically create a series of linkbuttons </SPAN> <SPAN class=kwd>For</SPAN> keycode <SPAN class=kwd>As Integer</SPAN> = 65 <SPAN class=kwd>To</SPAN> 90 <SPAN class=cmt>'one for each letter in the alphabet </SPAN> <SPAN class=kwd>Dim</SPAN> lnk <SPAN class=kwd>As New</SPAN> LinkButton <SPAN class=cmt>'assign the ID ourself to make sure it is consistent 'if you let the framework assign it, the dynamic control may 'not behave correctly </SPAN> lnk.ID = <SPAN class=st>"alpha_"</SPAN> &amp; keycode.ToString lnk.Text = Chr(keycode) <SPAN class=cmt>'we'll add a CommandName and a CommandArgument 'so we can determine what was clicked when the event is raised </SPAN> lnk.CommandName = <SPAN class=st>"ALPHABET"</SPAN> lnk.CommandArgument = Chr(keycode) <SPAN class=cmt>'have them all use the same event handler </SPAN> <SPAN class=kwd>AddHandler</SPAN> lnk.Click, <SPAN class=kwd>AddressOf</SPAN> onClick <SPAN class=cmt>'add these dynamic controls to our strategically place placeholder control 'the position of the placeholder determines 'where on the page the dynamic controls will appear </SPAN> <SPAN class=kwd>Me</SPAN>.PlaceHolder1.Controls.Add(lnk) <SPAN class=kwd>Me</SPAN>.PlaceHolder1.Controls.Add(<SPAN class=kwd>New</SPAN> LiteralControl(<SPAN class=st>" "</SPAN>)) <SPAN class=cmt>'space them out </SPAN> <SPAN class=kwd>Next</SPAN> <SPAN class=cmt>'VERY IMPORTANT -&gt; remember that we created these controls for the next postback </SPAN> <SPAN class=kwd>Me</SPAN>.DynamicControlSelection = ALPHABET_SELECTION <SPAN class=kwd>End Sub Private Sub</SPAN> CreateDynamicNumberButtons() <SPAN class=cmt>'clear the placeholder first - in case something else was dynamically loaded </SPAN> <SPAN class=kwd>Me</SPAN>.PlaceHolder1.Controls.Clear() <SPAN class=cmt>'dynamically create a series of button controls </SPAN> <SPAN class=kwd>For</SPAN> number <SPAN class=kwd>As Integer</SPAN> = 0 <SPAN class=kwd>To</SPAN> 25 <SPAN class=kwd>Dim</SPAN> btn <SPAN class=kwd>As New</SPAN> Button <SPAN class=cmt>'assign the ID ourself to make sure it is consistent 'if you let the framework assign it, the dynamic control may 'not behave correctly </SPAN> btn.ID = <SPAN class=st>"number_"</SPAN> &amp; number.ToString btn.Text = number.ToString <SPAN class=cmt>'we'll add a CommandName and a CommandArgument 'so we can determine what was clicked when the event is raised </SPAN> btn.CommandName = <SPAN class=st>"NUMBER"</SPAN> btn.CommandArgument = number.ToString <SPAN class=cmt>'have them all use the same event handler </SPAN> <SPAN class=kwd>AddHandler</SPAN> btn.Click, <SPAN class=kwd>AddressOf</SPAN> onClick <SPAN class=cmt>'add these dynamic controls to our strategically place placeholder control 'the position of the placeholder determines 'where on the page the dynamic controls will appear </SPAN> <SPAN class=kwd>Me</SPAN>.PlaceHolder1.Controls.Add(btn) <SPAN class=kwd>Me</SPAN>.PlaceHolder1.Controls.Add(<SPAN class=kwd>New</SPAN> LiteralControl(<SPAN class=st>" "</SPAN>)) <SPAN class=cmt>'space them out </SPAN> <SPAN class=kwd>Next</SPAN> <SPAN class=cmt>'VERY IMPORTANT -&gt; remember that we created these controls for the next postback </SPAN> <SPAN class=kwd>Me</SPAN>.DynamicControlSelection = NUMBER_SELECTION <SPAN class=kwd>End Sub Protected Sub</SPAN> Page_PreRender(<SPAN class=kwd>ByVal</SPAN> sender <SPAN class=kwd>As Object</SPAN>, <SPAN class=kwd>ByVal</SPAN> e <SPAN class=kwd>As</SPAN> System.EventArgs) _ <SPAN class=kwd>Handles Me</SPAN>.PreRender <SPAN class=kwd>Me</SPAN>.lblViewStateValue.Text = <SPAN class=kwd>Me</SPAN>.DynamicControlSelection <SPAN class=kwd>End Sub End Class</SPAN> </pre>&nbsp; <p>&nbsp;</p> 2007-11-25T17:50:21-05:002024300http://forums.asp.net/p/1186195/2024300.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>It is important to note that it is not sufficient to re-create the dynamic controls on all subsequent page postbacks. <br> You need to re-create them with the same ID's (as shown in the code above). The ID's are important for the event mechanism.</p> <p>If you don't assign ID's, the ASP.NET engine will assign ID's automatically (something like ctl_01, ctl_02, etc.), and sometimes these ID's&nbsp;may be different before and after the postback (for instance if the number of controls has changed in the mean time).</p> <p>This remark is already stated in the code above, but I think it is important to keep it in mind as a general rule before you start coding dynamic controls.</p> <p>Jos</p> 2007-11-25T18:17:50-05:002024839http://forums.asp.net/p/1186195/2024839.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>&nbsp;Hi,</p> <p>The dynamically created control will disappear on&nbsp; postback why because controls are created but initialized on the page load.</p> <p>That is why whenever the page postback happens the controls will disappear. So if you don't to disappear you control on the post back you should create it in the Page_Init()</p> <p>Here once created the control won't disappear not only on the page post back but also in the control events.&nbsp;</p> 2007-11-26T05:07:21-05:002025563http://forums.asp.net/p/1186195/2025563.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>While you can create dynamic controls in either Page_Init or Page_Load, I chose Page_Load as I was using ViewState to store a value that indicated which set of dynamic controls should be loaded. ViewState values are available in Page_Load - but they are not&nbsp;available during the Page_Init event. If the control creation logic were to be moved to Page_Init, then the&nbsp;example i posted&nbsp;would not work.</p> <p>&nbsp;</p> 2007-11-26T12:24:48-05:002031151http://forums.asp.net/p/1186195/2031151.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>Hi, can you please post it in C#?</p> <p>Thanks.</p> 2007-11-28T20:26:32-05:002066975http://forums.asp.net/p/1186195/2066975.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>When using AddHandler, will this work when the control is created within a seperate class?</p> 2007-12-18T21:29:36-05:002067095http://forums.asp.net/p/1186195/2067095.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>Yes - it will.&nbsp; Consider the following example:</p> <p>First we need a simple page that contains a&nbsp;PlaceHolder control named PlaceHolder1. In the Page_Init event of our page we can call a separate class to create a dynamic button.</p> <pre class="prettyprint">Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init 'ask a separate class to create a button control in a designated placeholder 'note that it's very important that the dynamic control is created on each and every 'postback and that the dynamic control also has a consistent control ID. Dim buttonCreator As New DynamicButtonCreator buttonCreator.CreateButton(Me.PlaceHolder1) End Sub</pre> <P mce_keep="true">So now we need that separate class to both create the dynamic button control and to handle the click event from it.</P><pre class="prettyprint"><SPAN class=kwd>Public Class</SPAN> DynamicButtonCreator <SPAN class=cmt>'this method creates a dynamic button in the designated placeholder </SPAN> <SPAN class=kwd>Public Sub</SPAN> CreateButton(<SPAN class=kwd>ByVal</SPAN> placeHolder <SPAN class=kwd>As</SPAN> PlaceHolder) <SPAN class=kwd>Dim</SPAN> b <SPAN class=kwd>As New</SPAN> Button b.ID = <SPAN class=st>"mySpecialButton"</SPAN> b.Text = <SPAN class=st>"A Dynamic Button"</SPAN> <SPAN class=cmt>'wire up an event handler </SPAN> <SPAN class=kwd>AddHandler</SPAN> b.Click, <SPAN class=kwd>AddressOf</SPAN> onClick <SPAN class=cmt>'add dynamic button to the placeholder </SPAN> placeHolder.Controls.Add(b) <SPAN class=kwd>End Sub Private Sub</SPAN> onClick(<SPAN class=kwd>ByVal</SPAN> sender <SPAN class=kwd>As Object</SPAN>, <SPAN class=kwd>ByVal</SPAN> e <SPAN class=kwd>As</SPAN> EventArgs) <SPAN class=cmt>'handle the click event from the dynamic button </SPAN> <SPAN class=kwd>Dim</SPAN> btn <SPAN class=kwd>As</SPAN> IButtonControl = <SPAN class=kwd>DirectCast</SPAN>(sender, IButtonControl) btn.Text = <SPAN class=st>"You just clicked me..."</SPAN> <SPAN class=kwd>End Sub End Class</SPAN></pre>&nbsp; 2007-12-18T23:46:22-05:002100250http://forums.asp.net/p/1186195/2100250.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>If you need to access viewstate you can initalize/create dynamic controls in&nbsp;LoadViewState which occurs between Page_Init and Page_Load.&nbsp; In LoadViewstate, access viewstate properties and create the controls , after the base.LoadViewstate call.</p> 2008-01-09T21:00:24-05:002222063http://forums.asp.net/p/1186195/2222063.aspx/1?dynamic+control+help+plzzzzzzzzzzzzzzz+dynamic control help plzzzzzzzzzzzzzzz..... <p>this is my program logic </p> <p>i am new to this web developement so looking forward for your help</p> <p>plzzzzzzzzzz go thru</p> <p>reply asap</p> <p>having error with find control part</p> <p>do suggest all changes nedeed</p> <p>or else</p> <p>if possible&nbsp; can u modify&nbsp; my code n send rest of my project is going good i am really stuck with this plzz</p> <p>i hav to take values frm text box n calculate base on it</p> <p>&nbsp;</p> <font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">form</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;frmPension&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Panel</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;pnlone&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Height</font><font color="#0000ff" size="2">=&quot;74px&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Width</font><font color="#0000ff" size="2">=&quot;384px&quot;&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Label</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;lblAmnt&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Text</font><font color="#0000ff" size="2">=&quot; Enter the pension amount:&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Width</font><font color="#0000ff" size="2">=&quot;180px&quot;&gt;&lt;/</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Label</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">TextBox</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;tbAmnt&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;/&gt;&lt;</font><font color="#a31515" size="2">br</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Label</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;lblNo&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Text</font><font color="#0000ff" size="2">=&quot; Enter the number of persons:&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Width</font><font color="#0000ff" size="2">=&quot;180px&quot;&gt;&lt;/</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Label</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">TextBox</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;tbNo&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;/&gt;&lt;</font><font color="#a31515" size="2">br</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">center</font><font color="#0000ff" size="2">&gt;</font><font size="2"> </font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">button</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;Enter&quot;</font><font size="2"> </font><font color="#ff0000" size="2">text</font><font color="#0000ff" size="2">=&quot;Enter&quot;</font><font size="2"> </font><font color="#ff0000" size="2">OnClick</font><font color="#0000ff" size="2">=&quot;SubmitBtn_Click&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;/&gt;&lt;/</font><font color="#a31515" size="2">center</font><font color="#0000ff" size="2">&gt;&lt;</font><font color="#a31515" size="2">br</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">p</font><font color="#0000ff" size="2">&gt;&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">PlaceHolder</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;Area1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;&gt;&lt;/</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">PlaceHolder</font><font color="#0000ff" size="2">&gt;&lt;/</font><font color="#a31515" size="2">p</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">table</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;tblReslt&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">tr</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">td</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;col1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;&gt;&lt;/</font><font color="#a31515" size="2">td</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">td</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;col2&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;&gt;&lt;/</font><font color="#a31515" size="2">td</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;/</font><font color="#a31515" size="2">tr</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">tr</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">td</font><font color="#0000ff" size="2">&gt;&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Button</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;submit&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">visible</font><font color="#0000ff" size="2">=&quot;false&quot;</font><font size="2"> </font><font color="#ff0000" size="2">OnClick</font><font color="#0000ff" size="2">=&quot;calInheritAmnt&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Text</font><font color="#0000ff" size="2">=&quot;submit&quot;</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;&lt;/</font><font color="#a31515" size="2">td</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;/</font><font color="#a31515" size="2">tr</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;/</font><font color="#a31515" size="2">table</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;/</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">Panel</font><font color="#0000ff" size="2">&gt;</p> </font><font size="2"> <p>&nbsp;</p> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#a31515" size="2">table</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;tblInheritDetails&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">width</font><font color="#0000ff" size="2">=&quot;100%&quot;/&gt;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">&lt;/</font><font color="#a31515" size="2">form</font><font color="#0000ff" size="2">&gt;</font></p> <p><font color="#0000ff" size="2"></font>&nbsp;</p> <font color="#0000ff" size="2"><font color="#0000ff" size="2">Partial</font><font color="#000000" size="2"> </font><font color="#0000ff" size="2">Class</font><font size="2"><font color="#000000"> testajax</font></font></font><font size="2"> <p></font><font color="#0000ff" size="2">Inherits</font><font size="2"> System.Web.UI.Page </p> </font><font color="#0000ff" size="2">Sub</font><font size="2"> Page_Load(</font><font color="#0000ff" size="2">ByVal</font><font size="2"> Sender </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Object</font><font size="2">, </font><font color="#0000ff" size="2">ByVal</font><font size="2"> E </font><font color="#0000ff" size="2">As</font><font size="2"> EventArgs)</font><font size="2"> <p></font><font color="#0000ff" size="2">If</font><font size="2"> IsPostBack </font> <font color="#0000ff" size="2">Then</p> </font><font size="2"> <p>SubmitBtn_Click(Sender, E)</p> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">If</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">Sub</p> </font><font size="2"></font><font color="#0000ff" size="2">Public</font><font size="2"> </font><font color="#0000ff" size="2">Sub</font><font size="2"> SubmitBtn_Click(</font><font color="#0000ff" size="2">ByVal</font><font size="2"> Sender </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Object</font><font size="2">, </font><font color="#0000ff" size="2">ByVal</font><font size="2"> E </font><font color="#0000ff" size="2">As</font><font size="2"> EventArgs)</font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> I </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Integer</p> </font><font size="2"></font><font color="#0000ff" size="2">For</font><font size="2"> I = 1 </font><font color="#0000ff" size="2">To</font><font size="2"> </font><font color="#0000ff" size="2">CInt</font><font size="2">(tbNo.Text.Trim())</font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> tbInhrtrName </font> <font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">New</font><font size="2"> TextBox</p> tbInhrtrName.ID = </font><font color="#a31515" size="2">&quot;tbDynamic&quot;</font><font size="2"> &amp; I.ToString</font><font size="2">tbInhrtrName.Text = </font><font color="#a31515" size="2">&quot;Control&quot;</font><font size="2"> &amp; I</font><font size="2"> <p>Form.Controls.Add(tbInhrtrName)</p> <p></font><font color="#008000" size="2">'Area1.Controls.Add(tbInhrtrName)</p> </font><font size="2"></font><font color="#0000ff" size="2">Dim</font><font size="2"> MyLiteral = </font><font color="#0000ff" size="2">New</font><font size="2"> LiteralControl</font><font size="2"> <p>MyLiteral.Text = </font><font color="#a31515" size="2">&quot;&lt;BR&gt;&lt;BR&gt;&quot;</p> </font><font size="2"> <p>Form.Controls.Add(MyLiteral)</p> <p></font><font color="#008000" size="2">'Area1.Controls.Add(MyLiteral)</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Next</font></p> <font color="#0000ff" size="2"><font size="2"> <p>submit.Visible = </font><font color="#a31515" size="2">&quot;true&quot;</p> </font><font size="2"> <p>tblInheritDetails.Visible = </font><font color="#a31515" size="2">&quot;true&quot;</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">Sub</font></p> <font color="#0000ff" size="2"><font size="2"></font><font color="#0000ff" size="2">Protected</font><font size="2"> </font><font color="#0000ff" size="2">Sub</font><font size="2"> calInheritAmnt(</font><font color="#0000ff" size="2">ByVal</font><font size="2"> sender </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Object</font><font size="2">, </font><font color="#0000ff" size="2">ByVal</font><font size="2"> e </font><font color="#0000ff" size="2">As</font><font size="2"> System.EventArgs) </font><font color="#0000ff" size="2">Handles</font><font size="2"> submit.Click</font></font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> id </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">String</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> I </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">Integer</p> </font><font size="2"> <p></font><font color="#008000" size="2">'Dim tb As New TextBox</p> </font><font size="2"></font><font color="#0000ff" size="2">For</font><font size="2"> I = 1 </font><font color="#0000ff" size="2">To</font><font size="2"> </font><font color="#0000ff" size="2">CInt</font><font size="2">(tbNo.Text.Trim())</font></font><font size="2"> <p></font><font color="#0000ff" size="2">Dim</font><font size="2"> tb </font><font color="#0000ff" size="2">As</font><font size="2"> </font><font color="#0000ff" size="2">New</font><font size="2"> TextBox</p> id = </font><font color="#a31515" size="2">&quot;tbDynamic&quot;</font><font size="2"> &amp; I.ToString</font><font size="2">tb = </font><font color="#0000ff" size="2">DirectCast</font><font size="2">(sender, TextBox)</font><font size="2"> <p>Response.Write(tb.Text.ToString())</p> <p></font><font color="#008000" size="2">'Response.Write(Session(id) &amp; &quot;&lt;br/&gt;&quot;)</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">Next</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">End</font><font size="2"> </font><font color="#0000ff" size="2">Sub</font></p> <font color="#0000ff" size="2"><font color="#0000ff" size="2"> <p>End</font><font color="#000000" size="2"> </font><font color="#0000ff" size="2">Class</p> </font></font> 2008-03-10T05:32:50-04:002282993http://forums.asp.net/p/1186195/2282993.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>why don't use&nbsp;&quot;IsPostBack&quot; in server side to check? seems it works in my case</p> 2008-04-08T08:32:14-04:002285018http://forums.asp.net/p/1186195/2285018.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>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&nbsp;have any dynamic controls loaded.&nbsp; It might require a specific interaction with the page that causes the first dynamic control to be loaded.&nbsp; Ultimately, it is important to keep track of what dynamic controls (if any)&nbsp;may have been loaded and then continue to load them early in the page life-cycle&nbsp;on all subsequent postbacks.</p> 2008-04-08T22:49:04-04:002309064http://forums.asp.net/p/1186195/2309064.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>This subject is always problematic to the beginner asp.net developers and this post does a good job clearing things out.</p> <p>Thumbs Up.&nbsp;</p> 2008-04-20T09:50:03-04:002342589http://forums.asp.net/p/1186195/2342589.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>Hi , i want to ask something related to this thread.</p> <p>I want to add a <u>new</u> DropDownList to a panel&nbsp;only when user clicks button &quot;Add New&quot;,&nbsp;and add an event handler CheckedChanged to this control.</p> <p>I also want to have the viewstate of every dropdownlist i have already added in order to use it when user clicks &quot;Submit&quot; (another button).</p> <p>I figured out that the solution is ( according to lifecycle of aspx page ) :</p> <p><strong>PageInit&nbsp;</strong></p> <p>&nbsp;<pre class="prettyprint">if (Session[&quot;countDDL&quot;] == null) Session[&quot;countDDL&quot;] = 0; Session[&quot;countDDL&quot;] = int.Parse(Session[&quot;countDDL&quot;].ToString()) &#43; 1; for (int i = 0; i &lt; int.Parse(Session[&quot;countDDL&quot;].ToString()); i&#43;&#43;) { DropDownList processes = new DropDownList(); ListItem lsi = new ListItem(&quot;Choose &quot;); processes.ID = &quot;processes&quot; &#43; i.ToString(); processes.Width = 150; processes.Items.Add(lsi); processes.AutoPostBack = true; FillList(processes); processes.SelectedIndexChanged &#43;= new EventHandler(processes_SelectedIndexChanged); pnlProcesses.Controls.Add(processes); }</pre> <P mce_keep="true">&nbsp;</P> <P>Create and fill&nbsp;&nbsp;all the dropdownlists that&nbsp;exist plus one more . The Viewstate works fine if the IDs are the same.</P> <P mce_keep="true"><STRONG>PageLoad</STRONG></P> <P mce_keep="true">&nbsp;<pre class="prettyprint">Session[<SPAN class=st>"buttonAddNewPressed"</SPAN>] = <SPAN class=kwd>false</SPAN>; </pre> <P mce_keep="true">&nbsp;</P> <P>Reset Session key.</P> <P mce_keep="true">&nbsp;</P><pre class="prettyprint"><SPAN class=kwd>protected void</SPAN> AddNewProcess_Click(<SPAN class=kwd>object</SPAN> sender, ImageClickEventArgs e) { Session[<SPAN class=st>"buttonAddNewPressed"</SPAN>] = <SPAN class=kwd>true</SPAN>; }</pre> <P mce_keep="true">&nbsp;</P><pre class="prettyprint"><SPAN class=kwd>protected void</SPAN> Page_PreRender(<SPAN class=kwd>object</SPAN> sender, EventArgs e) { <SPAN class=kwd>if</SPAN> ((<SPAN class=kwd>bool</SPAN>)Session[<SPAN class=st>"buttonAddNewPressed"</SPAN>] == <SPAN class=kwd>false</SPAN>) { pnlProcesses.Controls.RemoveAt(pnlProcesses.Controls.Count -1); Session[<SPAN class=st>"countDDL"</SPAN>] = <SPAN class=kwd>int</SPAN>.Parse(Session[<SPAN class=st>"countDDL"</SPAN>].ToString()) - 1; } }</pre> </p> <p>If Button &quot;Add New&quot; didn't press i delete the last dropdownlist&nbsp;beacuse&nbsp;another event happened&nbsp;and decrease the session counter by one.</p> <p>My question is &quot; Can i make my code better ?&quot; , i don't want to create and delete controls after&nbsp;. I decided to do this because the &quot;Add New&quot; 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.</p> <p>Thanx in advance,</p> <p>koraki_g</p> 2008-05-07T10:08:49-04:002349293http://forums.asp.net/p/1186195/2349293.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p><b>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#.</b> <br> </p> 2008-05-10T00:01:20-04:002690680http://forums.asp.net/p/1186195/2690680.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>This is amazing article explaing in detail how to get the dynamic controls value after the potback occurs.</p> <p>&nbsp;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.</p> <p>&nbsp;-Deepak</p> 2008-10-17T18:47:23-04:002814150http://forums.asp.net/p/1186195/2814150.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>Hi, <br> </p> <p>&nbsp;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.</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.DataSource = dsSocket<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.DataTextField = &quot;SOCKET&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.DataValueField = &quot;SOCKET&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.DataBind()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> </p> <p>&nbsp;But if I am assigning the values as below it is not allowing me to read the .selected value. it returns &quot;&quot;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.Items.Clear()</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.Items.Add(New ListItem(strSelect, strSelect))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Each dr As DataRow In dsSocket.Tables(0).Rows<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drSocket.Items.Add(New ListItem(dr.Item(&quot;SOCKET&quot;), dr.Item(&quot;SOCKET&quot;)))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next </p> <p>&nbsp;I am using&nbsp; DirectCast(item.FindControl(&quot;drSocket&quot;), DropDownList).SelectedValue to retreive the value in other events. <br> </p> <p>&nbsp;They this is happening?</p> <p>&nbsp;</p> 2008-12-17T02:29:18-05:002830252http://forums.asp.net/p/1186195/2830252.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>&nbsp;Nice post, this issue always consufe every new asp.net developer. This post will be a usefull link for them.<br> </p> 2008-12-25T04:49:42-05:002840989http://forums.asp.net/p/1186195/2840989.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p><a href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx">http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx</a></p> 2009-01-01T05:31:10-05:002936322http://forums.asp.net/p/1186195/2936322.aspx/1?Re+dynamic+control+help+plzzzzzzzzzzzzzzz+Re: dynamic control help plzzzzzzzzzzzzzzz..... <p>i want to create dynamic controls mainly textbox and can be any number </p> <p>i have created it dynamically and assign id to them but when i enter values in these textboxes</p> <p>i cant read them on button click.</p> <p>&nbsp;So how to retrive values from dynamically created textboxes on button click</p> <p>Thanks in Advance </p> 2009-02-12T09:52:27-05:003023540http://forums.asp.net/p/1186195/3023540.aspx/1?Re+FAQ+Why+do+dynamic+controls+disappear+on+postback+and+not+raise+events+Re: FAQ: Why do dynamic controls disappear on postback and not raise events? <p>Hi,</p> <p>&nbsp;</p> <p>but in&nbsp; onClick() method if i want to assign value say integer value=90 or a string &quot;xyz&quot; to the textbox</p> <p>it not works .value not be able to assign, Can u give some solution ?</p> <p>Thanks</p> 2009-03-20T11:41:40-04:00