Kendra Kuhl

blog

  Home  |   Contact  |   Syndication    |   Login
  5 Posts | 0 Stories | 3 Comments | 1 Trackbacks

News



Archives

Post Categories

Blog Roll

Tuesday, December 11, 2007 #

I really wanted my controls to work the same way PageFlakes controls work by loading after the page loads. So, somebody was kind enough to point me to Matt Berseth's article on how to do just that. But, after looking at it, I realized that it wouldn't work as written for me. I use Master Pages with content pages and the '<%= this.btn.ClientID %>' part of his code wouldn't produce the results that I wanted. So, I came up with this solution.

  1. Wrap your hidden button control (see Matt's article) in a div / span / paragraph with a class of "HiddenButton". This allows us to find it in the Javascript.
  2. Create a javascript file with this code in it. What this does is get the element you created in step number one, finds its only child item, and then gets the id of the child. I had to replace the underscores with $ because that is the client id of the child element.

    var _isInitialLoad = true;

    function pageLoad(sender, args)
    {
        if(_isInitialLoad)
        {
            _isInitialLoad = false;
            // Go get button that needs the postback
            // Have to get parent div first
            var parentDiv = document.getElementById("HiddenButton");
            // Get child button
            var ourButton = parentDiv.getElementsByTagName("input").items(0).id; 
            // replace _ with $
            // get index of first _
            var intIndexOfMatch = ourButton.indexOf("_");
            // Loop through and get the rest of the _
            while (intIndexOfMatch != -1)
            {
                ourButton = ourButton.replace("_", "$");
                intIndexOfMatch = ourButton.indexOf("_");
            }

            //  simulate a button click by forcing the postback
            //  causing the updatepanel to update
            __doPostBack(ourButton);  
        }
    }

  3. Register the javascript file from #2 with the ScriptManager used for the update panel. Or, if you want it in the content page, in the ScriptManagerProxy control.

This should work the way that Matt's article does, only you can use Master / Content pages. I didn't need the OnUpdated / OnUpdating portion of his code so I didn't recreate it. If there is something I missed or a more elegant way to accomplish this, I'm open for suggestions.