0

As the title suggests, Is there a way to check if a "Dynamically" created Button inside a Gridview has caused a Postback. As there are more than one button in page!

I have tried the following:

String ButtonID = Page.Request.Params["__EVENTTARGET"];
String ButtonID = Request.Form["__EVENTTARGET"];
String ButtonID = Request.Params["__EVENTTARGET"];

But these all return Null value. I need to identify the button created Dynamically inside the GrdiView.

Mitesh Vora
  • 458
  • 8
  • 21

2 Answers2

0

Button creates Postback when it is clicked. Make one property and set it to true when you enter onClick event.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

You can use the function below Reference
On postback, how can I check which control cause postback in Page_Init event

/// <summary>
/// Gets the ID of the post back control.
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    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 and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        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;
}
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117