开发者

get_postBackElement() is always undefined

<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    function BeginRequestHandler(sender, args) {
        var elem = args.get_postBackElement();
        alert("begin " + elem.value);
    }
    function EndRequestHandler(sender, args) {
        alert("end request handler");
    }
</script>

This is my simple attempt to retrieve the postback element, triggered from my UpdatePanel. My update panel looks like this:

            <asp:UpdatePanel ID="UpdatePanel_Project" UpdateMode="Conditional" runat="ser开发者_StackOverflowver">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList_Project" runat="server">
                    </asp:DropDownList>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="DropDownList_Customer" />
                </Triggers>
            </asp:UpdatePanel>

I believe I have did everything correctly...

Any ideas?


You have to set the ClientIDMode property value of the control (the DropDownList_Customer drop down list in this case) to AutoID. Please view my reply here.


What is your postback trigger ? This control seems to be missing DropDownList_Customer

 <asp:AsyncPostBackTrigger ControlID="DropDownList_Customer" />


I finally solved this pain, here's my solution.

Basically we just need to override Microsoft's _uniqueIDToClientID function so it doesn't ignore our Static client Ids on postback elements.

You'll just need to add the following code at the bottom of your page.

if (Sys.WebForms.PageRequestManager) {
  var prm = Sys.WebForms.PageRequestManager.getInstance();

  prm._uniqueIDToClientID = function (uniqueID) {
    var clientID = uniqueID.replace(/\$/g, "_");
    if (document.getElementById(clientID)) {
      return clientID;
    }

    var lastDollar = uniqueID.lastIndexOf("$");
    if (lastDollar === -1) {
      return clientID;
    }

    if (lastDollar+1 > uniqueID.length) {
      return clientID;
    }

    var staticID = uniqueID.slice(lastDollar + 1);

    return document.getElementById(staticID) ? staticID : clientID;
  };
}

Now, get_postBackElement() in your BeginRequestHandler will no longer be undefined!

Just make sure our code is executed after Microsoft's MicrosoftAjaxWebForms.js because we are overriding its _uniqueIDToClientID function.

Note: My function always returns the default WebForms ClientID if the element exists on the page. Only if the element cannot be found on the page, does it check to see if an element with a static ID exists on the page. If it exists, the staticID is used, otherwise it defaults back to the default WebForms ClientID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜