UpdatePanel

Last post 09-27-2008 8:45 AM by daniel.mihalcea. 2 replies.

Sort Posts:

  • UpdatePanel

    09-22-2008, 4:59 AM
    Hello. I have some Updatepanel's in my .aspx page and some code behind for the communication with the server. I send some id's to the server and I receive and xml file and a thumbnail. I click the thumb and I open the large image. When I open the large image I need to pass some values to my javascript functions, to make some client-side work. This thing happens at the second click on the page. At first the page loads and the variables are 0(the variables are public in c#). I make the request for the thumb, I click the thumb and I have to pass the values(for some zoom and pan). Every asp component is in a different updatepanel. I use Asp .Net 3.5 with c# 3.0 in VS 2008. Thank you.
  • Re: UpdatePanel

    09-25-2008, 10:57 PM
    Answer

    Hi,

    If we want to pass server variable to JavaScript, we can declare this variable to public and use <% = VariableName%> in JavaScript.

    We also can write the Variable to client. For example:

    <%@ 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">
    
        public string strVar = "";
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                strVar = "Hello world";
            }
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            strVar = "New Variable";
    
            string strScript = "str= '" + strVar + "'";
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "script", strScript, true);
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
    
                <script type="text/javascript">
        
                var str = '<%= strVar %>';
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
                     
                function EndRequestHandler()
                { 
    
                   alert(str);
                   
                }
    
        
                </script>
    
                <asp:updatepanel ID="UpdatePanel1" runat="server">
                    <contenttemplate>
                        <asp:button ID="Button1" runat="server" Text="Show Image" OnClick="Button1_Click" />
                    </contenttemplate>
                </asp:updatepanel>
            </div>
        </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.
  • Re: UpdatePanel

    09-27-2008, 8:45 AM
    Thank you. It work.
Page 1 of 1 (3 items)