patel.milanb patel.milanb - 3 months ago 14
ASP.NET (C#) Question

find control name which caused postback inside nested update panel

I have a structure like this.

UpdatePanel1
| |
| CheckBoxes - in code behind CheckBoxCheckedHandel() method
|
|---Usercontrol1
|
UpdatePanel2
|
Button1


Now here is the problem.

When I click
Button1
, it is causing
UpdatePanel1
to refresh and calling
CheckBoxCheckedHandel()
method. So either I want a control name which caused postback or I want to refresh only
UpdatePanel2
when
Button1
clicked.

One more thing I would like to say is I am displaying Time on both update panel to see if they are refreshing when something happens. At this time button1 click event does not refresh
UpdatePanel1
time but still it calls CheckBoxCheckedHandel() method.

I am stuck with this issue from last 2 days, please help.

Notes:
UpdatePanel1 - updatemode - Conditional and no triggres
UpdatePanel2 - updatemode - Conditional and asp:asyncTriggre for button1 click event

Answer

Here is how you can get the ID of a control that caused asynchronous postback. In your Page_Load handler put this code:

    if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
    {
        string id = Utils.GetAsyncPostBackControlID(Page, Page.Request);
    }

Following function gets the ID:

    public static string GetAsyncPostBackControlID(Page page, HttpRequest request)
    {
        string smUniqueId = ScriptManager.GetCurrent(page).UniqueID;
        string smFieldValue = request.Form[smUniqueId];

        if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains('|'))
            return smFieldValue.Split('|')[1];

        return String.Empty;
    }

ID of a control that caused postback is stored in a hiddenfield whose name is ID of a ScriptManager for the page. Value is stored in format: Script_Manager_ID|Control_ID