Page view counter

Including javascript in an update panel

Last post 09-02-2008 3:25 AM by Thomas Sun – MSFT. 3 replies.

Sort Posts:

  • Including javascript in an update panel

    08-28-2008, 2:47 PM
    • Loading...
    • Intermouse
    • Joined on 08-22-2008, 9:15 PM
    • Posts 8
    • Points 3

    I have a pice of javascript that is included in an update panel which displays ok when the page is first loaded. When the panel is updated the javascript doesn't run.

    Can anyone enlightne me as to how this can be achieved.

    The javascript is included in a .js file.

    I have a master page with the script manager and a scriptmanagerproxy on the subsequent pages.

    The update panel is updated with a timer control.

    TIA

    Mark

     

  • Re: Including javascript in an update panel

    08-28-2008, 2:52 PM
    Answer
    • Loading...
    • codenenterp
    • Joined on 08-25-2008, 8:58 PM
    • Boise,Id
    • Posts 234
    • Points 1,059

    You have to use the RegisterClientScriptBlock of the ScriptManager control.  This will add the javascript to the page.

    Solutions Architect
    Coden Enterprises
    http://www.codenenterprises.com/iblog
  • Re: Including javascript in an update panel

    08-28-2008, 3:43 PM
    • Loading...
    • Intermouse
    • Joined on 08-22-2008, 9:15 PM
    • Posts 8
    • Points 3

    Can you elaborate please.

     

  • Re: Including javascript in an update panel

    09-02-2008, 3:25 AM
    Answer

    Hi,

    When we place the JavaScript in the Updatepanel, we will notice that any javascript elements in the html are not fired.

    To work around this, we can add this JavaScript on the fly. For instance:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToString();
        }
    
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToString();
            string strScript = "var obj = $get('Text1');" +
                                " var obj1 = new Date();" +
                                " obj.value = obj1.toLocaleString()";
    
            //If we don't comment this line, the date that is in the input text will not be update.
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "script", strScript, true);
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:scriptmanager ID="ScriptManager1" runat="server">
                </asp:scriptmanager>
            </div>
            <asp:updatepanel ID="UpdatePanel1" runat="server">
                <contenttemplate>
                    <input id="Text1" type="text" />
                    <asp:label ID="Label1" runat="server"></asp:label>
    
                    <script type="text/javascript">
                    
                    var obj = $get('Text1');
                    var obj1 = new Date();
                    obj.value = obj1.toLocaleString();
                    
                  
                    </script>
    
                    <asp:timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick">
                    </asp:timer>
                </contenttemplate>
            </asp:updatepanel>
        </form>
    </body>
    </html>

     

    I look forward to hearing from you.  

     

    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (4 items)