Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which control caused the postback?

I have two buttons:

<asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Button ID="Button2" runat="server" Text="Button" /> 

How can I determine on pageLoad which one of this two caused the postback? Is there a short solution as I know there are only two controls that can cause this postback?

like image 685
hhh3112 Avatar asked Sep 01 '11 10:09

hhh3112


People also ask

How do I turn off postback?

The postback on submit button can be avoided by giving return=false in the event handler function as below.

What IsPostBack request?

Whenever a user made a request to the web server, the web server has to return the response to the user. PostBack is the name given to the process of submitting all the information that the user is currently working on and send it all back to the server.

What is the post back event handling?

A Postback Event is a string of information that is sent to a network's specific URL that contains information about the post-install event pertinent to the network.

What is the used of IsPostBack property?

IsPostBack is used to check if the page is responding to a post back event, like clicking a button. So, lets say you have some textboxes for users to change some data and then click a button to submit the data.


1 Answers

You can use this method to get the control that caused the postback:

/// <summary> /// Retrieves the control that caused the postback. /// </summary> /// <param name="page"></param> /// <returns></returns> private Control GetControlThatCausedPostBack(Page page) {     //initialize a control and set it to null     Control ctrl = null;      //get the event target name and find the control     string ctrlName = page.Request.Params.Get("__EVENTTARGET");     if (!String.IsNullOrEmpty(ctrlName))         ctrl = page.FindControl(ctrlName);      //return the control to the calling method     return ctrl; } 
like image 164
James Johnson Avatar answered Oct 08 '22 13:10

James Johnson