Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        var target = Request.Form["__EVENTTARGET"];
        var arg = Request.Form["__EVENTARGUMENT"];
    }
}
Posted
Updated 17-Jun-19 20:56pm

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;
}
 
Share this answer
 
Comments
lee23 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?
m@dhu 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.
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

C#
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.
 
Share this answer
 
Just add UseSubmitBehavior="False" to your button. This worked for me
c# - __EVENTTARGET is empty on postback of button click - Stack Overflow[^]
 
Share this answer
 
v2
Comments
CHill60 18-Jun-19 5:06am    
Even though this is a very old question this is a good answer - it would have been better to explain why though e.g.
Quote:"When UseSubmitBehavior is set to False, the button server control is rendered to a client browser as an HTML “button” (HTML tag with type=“button”) instead of an HTML “submit button” (HTML tag with type=”submit”) which is the default behavior. As a result, client side JavaScript is able to intercept the submission process when the button is clicked."
From Submit Once with an ASP.NET Button Server Control[^]
ashish47 18-Jun-19 7:35am    
Thanks for explanation @CHill60
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?
 
Share this answer
 
Comments
m@dhu 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900