views:

346

answers:

3

I have a treeview in a user control. I need to run a javascript function with every asynch postback to scroll the div it's in to the right position. I've got it working, but I think there has to be a "cleaner" way to do it. In the Page_Load function of the control, I have the following code. Is there a better way to do it?

ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "key" + DateTime.Now.Ticks, "RestorePosition();", true);

For the benefit of anyone searching for this answer, here is what I finally did that worked. At the top of the ascx page, I have the following code:


<script type="text/javascript">
    function pageLoad(sender, args) {
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(SavePosition);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RestorePosition);
    }

    function SavePosition(sender, args) {
        document.getElementById('hdnScrollSaver').value = document.getElementById('reportTreeViewdiv').scrollTop;
    } 
    function RestorePosition(sender, args) {
        document.getElementById('reportTreeViewdiv').scrollTop = document.getElementById('hdnScrollSaver').value;
    } 

</script>

And then I wrapped the treeview in a div tag as follows:


<div class="reportTreeView" id="reportTreeViewdiv">
                    <asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate" 
                        OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" PathSeparator="|" SkinID="ReportTreeView" />
                </div>

Hope this helps someone.

+1  A: 

I think this is a "clean" way to do it. You're using ScriptManager the way it is supposed to be used.

If you don't like the look of that line of code, you can always refactor it out to a "Script Utility" class or something. I wouldn't, but that's just me.

Chris Dwyer
It just seems hacked to have to run the RegisterStartupScript with some random key name on every postback and that there should be some sort of function like RegisterPostbackScript that lets the script be run after every post back
Kevin
I'm not quite sure I understand why you are giving it a unique key on every postback. It should work fine if your key is the same each time. All you're doing is registering the script for a particular rendering of the page. Outside of that, you're using it the way it is supposed to be used. I agree, it may not "look" like the most beautiful line of code, but it is doing exactly what you want it to do: run a client side script when the page loads in the browser.
Chris Dwyer
If I don't give it a unique key name on every postback, it blows up.
Kevin
What error do you get? According to MSDN, it shouldn't error if the type/key pair already exists.http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.aspx
Chris Dwyer
+2  A: 

Ajax gives you a page life cycle similar to an ASP.Net page, but on the client side. So in the pageLoad event we can wire up functions to be called on each begin request and end request.

function pageLoad(sender, args) {
  Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
}

function beginRequest(sender, args) {
        // begin request code - i.e. make a "processing" div visible
}

function endRequest(sender, args) {
        // we are back
        RestorePosition(); 
}
JBrooks
This did it. Thanks for the help.
Kevin
+1  A: 

I think you are doing this correctly. I would get rid of the unique key though.

As per MSDN:

By identifying the script with the key, multiple server control instances can request the script block without it being emitted to the output stream twice.

Any script blocks with the same key parameter values are considered duplicates.

The key is used to prevent you from having the scripts created multiple times so you don't need it to be unique in your case.

Kelsey