Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether postback caused by a Dynamic link button

Tags:

c#

.net

asp.net

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 Dynamic Control’s Event Handler’s Working for understanding the business requirement for this.

I have read On postback, how can I check which control cause postback in Page_Init event for identifying the control that caused the postback (inside Page_Load). But it is not working for my scenario.

What change need to be done to confirm whether the postback was caused by link button (inside Page_Load)?

Note: Refer the following for another scenario where it is inevitable https://codereview.stackexchange.com/questions/20510/custom-paging-in-asp-net-web-application

Note 1: I need to get the postback control ID as the first step inside if (Page.IsPostBack). 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.

Note 2: I am getting empty string for Request["__EVENTARGUMENT"] in the Page_Load

Related Question: By what event, 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. "

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PostbackTest.aspx.cs" Inherits="PostbackTest"
MasterPageFile="~/TestMasterPage.master" %>

<asp:Content ID="myContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="holder" runat="server">
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="TestClick" />
</asp:Content>

CODE BEHIND

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;
}
}

REFERENCE

  1. On postback, how can I check which control cause postback in Page_Init event
  2. Dynamic Control’s Event Handler’s Working
  3. Understanding the JavaScript __doPostBack Function
  4. Access JavaScript variables on PostBack using ASP.NET Code
  5. How does ASP.NET know which event to fire during a postback?
  6. how to remove 'name' attribute from server controls?
  7. How to use __doPostBack()
like image 527
LCJ Avatar asked Jan 23 '13 18:01

LCJ


1 Answers

A postback in asp.net is done by the java script function __doPostback(source, parameter)

so in your case it would be

    __doPostback("lnkDynamic123","") something like this

So in the code behind do the following

    var btnTrigger = Request["__EVENTTARGET"]; 

     if(btnTrigger=="lnkDynamic123") 
{
}

--- this would tell that it is your linkbutton that causes the postback

like image 115
Dan Hunex Avatar answered Oct 06 '22 00:10

Dan Hunex