Tabriz Atayi
Tabriz Atayi

Reputation: 6120

How to know which button postbacks the page?

I have a problem. When I press "return" key in the text box ASP.NETpage page must do some AJAX operation.It does Ajax operation but it also reloads or postbacks the page. And now I want to ask:

How can I know which button is clicked?

Thank your for your attention!!!

Upvotes: 1

Views: 62

Answers (2)

James Johnson
James Johnson

Reputation: 46047

Try this:

   /// <summary>
/// Retrieves the control that caused the postback.
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
private Control GetControlThatCausedPostBack()
{
    //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;
}

Upvotes: 4

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114377

When a button has a name property, that gets passed back with the form data. Ensure each button has a unique name.

Upvotes: 0

Related Questions