Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check whether postback caused by a Dynamic link button
    text
    copied!<p>I have a button control. On click of this button I need to add a Link Button dynamically. The Link Button needs an event handler. Hence the dynamic Link button is first added in the Page_Load and cleared and added again in the button click handler. Please read <a href="https://stackoverflow.com/questions/14364332/dynamic-controls-event-handlers-working">Dynamic Control’s Event Handler’s Working</a> for understanding the business requirement for this.</p> <p>I have read <a href="https://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event">On postback, how can I check which control cause postback in Page_Init event</a> for identifying the control that caused the postback (inside Page_Load). But it is not working for my scenario.</p> <p>What change need to be done to confirm whether the postback was caused by link button (inside Page_Load)?</p> <p><strong>Note</strong>: Refer the following for another scenario where it is inevitable <a href="https://codereview.stackexchange.com/questions/20510/custom-paging-in-asp-net-web-application">https://codereview.stackexchange.com/questions/20510/custom-paging-in-asp-net-web-application</a></p> <p>Note 1: I need to get the postback control ID as the first step inside <code>if (Page.IsPostBack)</code>. I need to add the dynamic link buttons control only if it is a postback from the button or the link button. There will be other controls that causes postback. For such postbacks we should not execute this code.</p> <p>Note 2: I am getting empty string for <code>Request["__EVENTARGUMENT"]</code> in the Page_Load</p> <p><strong>Related Question</strong>: By what <code>event</code>, the dynamic controls will be available in the Page (for using in FindControl). @Tung says - "Your GetPostBackControlId method is properly getting the name of the control that caused the postback, but it is unable to find a control with that id through page.FindControl because the linkbutton has not been created yet, and so page does not know of its existence. "</p> <p><strong>ASPX</strong></p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="PostbackTest.aspx.cs" Inherits="PostbackTest" MasterPageFile="~/TestMasterPage.master" %&gt; &lt;asp:Content ID="myContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt; &lt;div id="holder" runat="server"&gt; &lt;/div&gt; &lt;asp:Button ID="Button1" runat="server" Text="Button" OnClick="TestClick" /&gt; &lt;/asp:Content&gt; </code></pre> <p><strong>CODE BEHIND</strong></p> <pre><code>public partial class PostbackTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(Page.IsPostBack) { string IDValue = GetPostBackControlId(this.Page); int x = 0; holder.Controls.Clear(); LinkButton lnkDynamic = new LinkButton(); lnkDynamic.Click += new EventHandler(LinkClick); lnkDynamic.ID = "lnkDynamic123"; lnkDynamic.Text = "lnkDynamic123"; holder.Controls.Add(lnkDynamic); } } protected void TestClick(object sender, EventArgs e) { holder.Controls.Clear(); LinkButton lnkDynamic = new LinkButton(); lnkDynamic.Click += new EventHandler(LinkClick); lnkDynamic.ID = "lnkDynamic123"; lnkDynamic.Text = "lnkDynamic123"; holder.Controls.Add(lnkDynamic); } protected void LinkClick(object sender, EventArgs e) { } public static string GetPostBackControlId(Page page) { if (!page.IsPostBack) { return string.Empty; } Control control = null; // First check the "__EVENTTARGET" for controls with "_doPostBack" function string controlName = page.Request.Params["__EVENTTARGET"]; if (!String.IsNullOrEmpty(controlName)) { control = page.FindControl(controlName); } else { // if __EVENTTARGET is null, the control is a button type string controlId; Control foundControl; foreach (string ctl in page.Request.Form) { // Handle ImageButton they having an additional "quasi-property" in their Id which identifies mouse x and y coordinates if (ctl.EndsWith(".x") || ctl.EndsWith(".y")) { controlId = ctl.Substring(0, ctl.Length - 2); foundControl = page.FindControl(controlId); } else { foundControl = page.FindControl(ctl); } if (!(foundControl is Button || foundControl is ImageButton)) continue; control = foundControl; break; } } return control == null ? String.Empty : control.ID; } } </code></pre> <p><strong>REFERENCE</strong></p> <ol> <li><a href="https://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event">On postback, how can I check which control cause postback in Page_Init event</a></li> <li><a href="https://stackoverflow.com/questions/14364332/dynamic-controls-event-handlers-working">Dynamic Control’s Event Handler’s Working</a></li> <li><a href="http://aspalliance.com/articleViewer.aspx?aId=895&amp;pId=-1" rel="nofollow noreferrer">Understanding the JavaScript __doPostBack Function</a></li> <li><a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=203" rel="nofollow noreferrer">Access JavaScript variables on PostBack using ASP.NET Code</a></li> <li><a href="https://stackoverflow.com/questions/9989122/how-does-asp-net-know-which-event-to-fire-during-a-postback">How does ASP.NET know which event to fire during a postback?</a></li> <li><a href="https://stackoverflow.com/questions/4424476/asp-net-how-to-remove-name-attribute-from-server-controls">how to remove &#39;name&#39; attribute from server controls?</a></li> <li><a href="https://stackoverflow.com/questions/3591634/how-to-use-dopostback/3914500#3914500">How to use __doPostBack()</a></li> </ol>
 

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