1

I have a aspx page which contains few controls along with a Button.

 <asp:Button ID="savebtn" runat="server" OnClick="savebtn_Click" Style="display: none" />

I use this button to trigger post pack using the click event of this button.

 //For post back
        $(document).ready(function () {
                var id = document.getElementById('<%= savebtn.ClientID %>');
                //causes post back
                id.Text = "postback";
                id.click();

        });

Now at the page load I want to know which control caused the post back.

I have used below code to find out which control caused the postpack but its returning null. May be because I am triggered the post pack from JavaScript.

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;
}

Please help

SharpCoder
  • 18,279
  • 43
  • 153
  • 249

1 Answers1

1

Invoking the click event as you are guarantees that the save button will cause the postback. Is there a particular function you want to achieve by not putting your code in the server-side method (savebtn_Click)?

Update

If you would like to use the name button click for multiple select2 controls, try How to use __doPostBack().

If you just have the one select2, place your code in the button's event and use either of the following to extract the value:

selectedValueFromRequest = Request["Select"]

OR

selectedValueFromControl = Select.SelectedValue

Community
  • 1
  • 1
  • I am using a JQuery Plugin which called select2. I use this plugin for auto complete feature. After I select some value from that plugin, I want to cause postpack so that I can perform some task at the server side. – SharpCoder Aug 23 '13 at 04:37