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?
The postback on submit button can be avoided by giving return=false in the event handler function as below.
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With