[RESOLVED]question on ScriptManager.RegisterStartupScript

Hello Everyone,

Actually this topic, together with the updatepanel, has confused me already for some time.

Inside a content page, I have some javascript files, an ASP.net Panel, inside which there is a multiview, inside which there is a gridview, each encapsulated with an updatepanel, something like the following:

<asp:Content ID="contentMain" runat="server" contentplaceholderid="contentMainMaster">

    <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="../JavaScript/JSFile.js" /> <%-- where the JS function stays --%>
        </Scripts>
    </asp:ScriptManagerProxy>

    <asp:UpdatePanel ID="UpdatePanelMain" runat="server"> <%-- the 1st updatepanel --%>
    <ContentTemplate>
        <asp:Panel ID="panMain" runat="server" >
            <asp:UpdatePanel ID="UpdatePanelMultiView" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False"> <%-- the 2nd updatepanel --%>
            <ContentTemplate>
                <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0" >
                    <asp:View ID="View1" runat="server">
                        <asp:Panel ID="panSub1" runat="server>
                            <asp:UpdatePanel ID="UpdatePanelGrid1" runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:GridView ID="Grid1" runat="server">
                                <%-- some contents --%>
                                OnDataBound="onGrid1DataBound" <%-- I use registerstartupscript inside this function --%>
                                </asp:GridView>
                            </ContentTemplate>
                            </asp:UpdatePanel>
                        </asp:Panel>
                    </asp:View>
                </asp:MultiView>
            </ContentTemplate>
            </asp:UpdatePanel>
        </asp:Panel>
    </ContentTemplate>
    </asp:UpdatePanel>

Whenever the databound of gridview takes place, I want to execute a javascript function inside the file already registered with ScriptReference. Like this (notice that I use UpdatePanel control as the “control” parameter):

protected void onGrid1DataBound(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(UpdatePanelGrid1, UpdatePanelGrid1.GetType(), "Grid1Bound", "onGrid1Active()", true);
}

Now the things that I don’t understand:

I use a timer control to update the gridview (bind) every 2seconds. With the code above, with firebug, I see that the "onGrid1Active" javascript function indeed gets called on every databound event.

However if I remove the 1ST updatepanel (ID ==UpdatePanelMain), the function gets called only once, 

I could not figure out how the presence of this 1st updatepanel could influnce the gridview, which is inside the 3rd updatepanel ?

I would appreciate any comment.

Edit:

If I remove the updatepanel with ID==UpdatePanelMain, but in the timer function, after making "bind" to the gridview, If I call manually "UpdatePanelGrid1.update()", the javascript function gets called correctly.

You have specified

ChildrenAsTriggers="False"
for the second update panel which might be the culprit. ChildrenAsTriggers indicates whether postbacks from immediate child controls of an UpdatePanel control update the panel’s content.

Because you set it to false no child elements inside the update panel will refresh the content unless you give the triggers to the updatepanel.

I also do not see any triggers for the second update panel . So obviously the content won’t refresh.

The 1st UppdatePanel comes to your rescue here as any control inside it will update the page.

.Update() method nothing but , you are explicity updating the updatePanel. so this works.

Thank you very much! I made a couple of test. I think that now I understand better the usage of the updatepanel.

Leave a Reply