Possible solution to UpdatePanel and ClientIDMode=“Static”

徘徊边缘 提交于 2019-12-22 06:11:29

问题


I've been looking everywhere for a solution to the static ClientIDMode + UpdatePanel in Asp.NET, as seen in http://connect.microsoft.com/VisualStudio/feedback/details/584991/clientidmode-static-in-updatepanel-fails-to-do-async-postback

The problem is in the Sys.WebForms.PageRequestManager.uniqueIDToClientID function, that converts names to id by replacing "$" characters to "". I made a fix that seems to work but I want you guys to tell me what you think and if I'm missing something. Thanks a lot!

var old_uniqueIDToClientID = Sys.WebForms.PageRequestManager.prototype._uniqueIDToClientID;
Sys.WebForms.PageRequestManager.prototype._uniqueIDToClientID = function (arg) {
    var element = this._form.elements[arg];
    return (element) ? element.id : old_uniqueIDToClientID(arg)
}

回答1:


We made a similar fix, but we changed another function that was involved in the search for the element that caused the postback.

We have placed the following code at the bottom of our master page to make sure that it is included after the scriptmanager has loaded its scripts. Essentially it keeps modifying the id until it finds the element that caused the postback. The original code searched for the element by removing tokens from the right hand side of the name delimited by the dollar sign. So "$ctl00$ddl001" would become "$ctl00". If you are using static ids then that suffix might never exist. We modified the function to start from the left and remove the container names until an element is found.

It seems to work for us for now. :)

   if (Sys.WebForms.PageRequestManager) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm._findNearestElement = function (uniqueID) {
        while (uniqueID.length > 0) {
            var clientID = this._uniqueIDToClientID(uniqueID);
            var element = document.getElementById(clientID);
            if (element) {
                return element;
            }
            var indexOfFirstDollar = uniqueID.indexOf('$', 1);
            if (indexOfFirstDollar === -1) {
                return null;
            }
            uniqueID = uniqueID.substring(indexOfFirstDollar + 1, uniqueID.length);
        }
        return null;
    };
}



回答2:


An updatepanel to work in asychronous mode you need to add a scriptmanager tag in the form.

<asp:ScriptManager EnablePartialRendering="true"
 ID="ScriptManager1" runat="server"></asp:ScriptManager>

or you can add triggers

<Triggers>
     <asp:AsyncPostBackTrigger ControlID="ddl_Manufacturer" EventName="SelectedIndexChanged" />
</Triggers>


来源:https://stackoverflow.com/questions/13744513/possible-solution-to-updatepanel-and-clientidmode-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!