Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible solution to UpdatePanel and ClientIDMode="Static"

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)
}
like image 208
Jorgito Gutierrez Avatar asked Dec 06 '12 13:12

Jorgito Gutierrez


1 Answers

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;
    };
}
like image 160
matty_simms Avatar answered Sep 29 '22 21:09

matty_simms