Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Control’s Event Handler’s Working
    primarykey
    data
    text
    <p>I have following code that adds dynamic LinkButton on the click event of a normal button. The LinkButtons need a EventHandler <code>“LinkButton_Click“</code>. Since the event handler need to be registered while <code>Page_Load/Page_Init</code> itself, I am first adding all the possible links within <code>Page_Load</code> (in postback scenario). (There are totally fours linkbuttons; only two will be displayed in the screen based on business scenario)</p> <p>The code is working fine and the event handlers are getting invoked on click event. </p> <p><strong>Note:</strong> The business scenario is – if the current time’s second portion is less than 30, the first two LinkButtons need to be displayed; if greater than 30 the last two LinkButtons need to be displayed. </p> <p><strong>Note:</strong> The controls are cleared and added again inside <code>PopulateLinksBasedOnCriteria()</code> method</p> <p><strong>QUESTIONS</strong></p> <ol> <li>In the <code>PopulateLinksBasedOnCriteria()</code> method, I am newly creating the link button instances. Still the event handler is firing. This is working because the ID that got added in the <code>Page_Load</code> is same. Is there any MSDN reference that substantiate this behavior? (I.e. the event handler will be based on the IDs registered while <code>Page_Load/Page_Init</code>. This will work even if the controls are cleared and added again)</li> <li>There is duplicate code in creating LinkButtons – one inside <code>Page_Load</code> and other inside <code>PopulateLinksBasedOnCriteria()</code> method. Is there a better way to achieve this business task without duplicate code?</li> </ol> <p><strong>MarkUp</strong></p> <pre><code>&lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /&gt; &lt;br /&gt; &lt;asp:PlaceHolder ID="plhDynamicLinks" runat="server"&gt;&lt;/asp:PlaceHolder&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p><strong>CODE BEHIND</strong></p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { LinkButton lnk1 = new LinkButton(); lnk1.ID = "D1"; lnk1.Text = "A"; //Event handler must be registered in the Page_Load/Page_Init lnk1.Click += new EventHandler(LinkButton_Click); plhDynamicLinks.Controls.Add(lnk1); LinkButton lnk2 = new LinkButton(); lnk2.ID = "D2"; lnk2.Text = "B"; lnk2.Click += new EventHandler(LinkButton_Click); plhDynamicLinks.Controls.Add(lnk2); LinkButton lnk3 = new LinkButton(); lnk3.ID = "D3"; lnk3.Text = "C"; lnk3.Click += new EventHandler(LinkButton_Click); plhDynamicLinks.Controls.Add(lnk3); LinkButton lnk4 = new LinkButton(); lnk4.ID = "D4"; lnk4.Text = "D"; lnk4.Click += new EventHandler(LinkButton_Click); plhDynamicLinks.Controls.Add(lnk4); } } protected void LinkButton_Click(object sender, EventArgs e) { PopulateLinksBasedOnCriteria(); LinkButton clickedControl = (LinkButton)sender; Response.Write(DateTime.Now.ToString()+"___"+ clickedControl.ID + " Link Button Clicked" ); } protected void btnAdd_Click(object sender, EventArgs e) { PopulateLinksBasedOnCriteria(); } private void PopulateLinksBasedOnCriteria() { plhDynamicLinks.Controls.Clear(); if (DateTime.Now.Second &lt; 30) { LinkButton linkButton1 = new LinkButton(); linkButton1.ID = "D1"; linkButton1.Text = "1"; plhDynamicLinks.Controls.Add(linkButton1); LinkButton linkButton2 = new LinkButton(); linkButton2.ID = "D2"; linkButton2.Text = "2"; plhDynamicLinks.Controls.Add(linkButton2); } else { LinkButton linkButton3 = new LinkButton(); linkButton3.ID = "D3"; linkButton3.Text = "3"; plhDynamicLinks.Controls.Add(linkButton3); LinkButton linkButton4 = new LinkButton(); linkButton4.ID = "D4"; linkButton4.Text = "4"; plhDynamicLinks.Controls.Add(linkButton4); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload