Click here to Skip to main content
11,647,659 members (74,202 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
According to some sites I read, I should be able to know which control caused the postback of a page. I dumped 2 buttons on a page (accepted all default properties and behaviours) and test on Page Load what this variable gives me but null is returned. The Form and both Button's runat="server". Help appreciated.
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        var target = Request.Form["__EVENTTARGET"];
        var arg = Request.Form["__EVENTARGUMENT"];
    }
}
Posted 31-Oct-10 14:36pm
lee23285
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just an improvement to Brij answer.
public Control GetPostBackControl(Page page)
{
    Control control = null;
    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control mycontrol = page.FindControl(ctl);
            if (mycontrol is System.Web.UI.WebControls.Button)
            {
                control =mycontrol ;
               // This gives you ID of which button caused postback
                Response.Write(mycontrol.ID);
                break;
            }
        }
    }
    return control;
}
  Permalink  
Comments
lee23 at 4-Nov-10 18:24pm
   
Thank you for the correction in the code. Again my question, what happens if there are 2 buttons that can do postbacks?
madhukk at 5-Nov-10 3:53am
   
Though you have two buttons the foreach loop used will get only the id of the button which does postback and gets out of loop.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are different mechanism for different controls for postback. Above one will work for CheckBoxes, DropDownLists, LinkButtons, etc, this does not work for Button controls. For button controls, If you take a look at how the server controls render as HTML, you'll see that the buttons do not call the __doPostBack Javascript function so the __EVENTTARGET is never set. Instead, the Buttons render as simple input type=“submit” tags. Since the button (or input) is what causes the form to submit, it is added to the items in the Form collection, along with all the other values from the submitted form. So you can find the postback control by the following method
public static Control GetPostBackControl(Page page)
{
    Control control = null;
    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control mycontrol = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control =mycontrol ;
                break;
            }
        }
    }
    return control;
}
Hope this will solve your problem.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thank you. Understand your explanation but don't understand the pasted solution. The For loop iterates over all the controls on the page and checks if the control is a button. What happens if there are more than one button?
  Permalink  
Comments
madhukk at 4-Nov-10 2:11am
   
use add comment to reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 jyo.net 500
1 F-ES Sitecore 370
2 OriginalGriff 322
3 DamithSL 315
4 CPallini 240
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795


Advertise | Privacy | Mobile
Web04 | 2.8.150804.3 | Last Updated 4 Nov 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100