2

I'm trying to access a variable in Javascript from the server side (c#). I've declared this variable as public from the server side:

public string elementSliderAppend;

and accessing in javascript using:

<%=elementSliderAppend %>

So far so good, however the variable doesn't seem to be updated after the update panel refresh in my aspx page.

The variable changes all okay in the backend but the change is not happening in Javascript. I have placed the accessing of the variable inside the update panel so it should refresh but it doesn't.

below is the code from the aspx page

    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="10000">
    </asp:Timer>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

         <Triggers>
            <asp:Asyncpostbacktrigger controlid="Timer1">  </asp:Asyncpostbacktrigger>
        </Triggers>

        <ContentTemplate>
          <div id="flexViewer" class="flexslider">
          </div>
            <script type="text/javascript">

                function getImgHtml() {

                    return <%=elementSliderAppend %>;
                }
                </script>

        </ContentTemplate>

    </asp:UpdatePanel>

Calling the variable from the below code (also on the aspx page)

     function pageLoad(sender, args) {


         $("#flexViewer").html("<ul class=slides>" + getImgHtml() + "</ul>");

         flexSliderRUN();


     }

NEW CODE

I've now inserted the below code inside the updatepanel above

             <asp:HiddenField ID="hiddimgHtml" runat="server"  />

Im setting the hidden field value from code behind using

        hiddimgHtml.value= elementSliderAppend;

using the below in my javascript file to access the value

var value = $("#hiddimgHtml").val();

alert(value);

$("#flexViewer").html("<ul class=slides>" + value + "</ul>");

However on the refresh im getting a very nasty looking error

POST http://:1562/FridayViewer.aspx 500 (Internal Server Error) ScriptResource.axd?d=xz1-gVxgQeQi9AUxmqDjtx8455SyoL-b2LZdBEiJTo8-XZn2n4ZRBb…NYDXi9xlT_-BAcGHV0ZqGBzbItBzEvsjInrEIWh3G93x0XMte0bSN00lh0&t=6119e399:6073 XHR finished loading: "http://:1562/FridayViewer.aspx". ScriptResource.axd?d=xz1-gVxgQeQi9AUxmqDjtx8455SyoL-b2LZdBEiJTo8-XZn2n4ZRBb…NYDXi9xlT_-BAcGHV0ZqGBzbItBzEvsjInrEIWh3G93x0XMte0bSN00lh0&t=6119e399:6073 Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500 ScriptResource.axd?d=xz1-gVxgQeQi9AUxmqDjtx8455SyoL-b2LZdBEiJTo8-XZn2n4ZRBb…RNYDXi9xlT_-BAcGHV0ZqGBzbItBzEvsjInrEIWh3G93x0XMte0bSN00lh0&t=6119e399:237

2
  • 2
    Could you post more of your javascript, particularly what you have in the script tags inside your update panel? Commented Aug 17, 2013 at 23:17
  • OK this is not working at all. Let me explain further. I have now used <%=elementSliderAppend%>, setting the hidden field from code behind and also registerstartupscript all of these give me the Internal Server error (from the browser console) upon partial postback. The content of the variable consists of html code and tags. The variable does show on the page so I know its being passed but once the partial postback occur I get the internal server error. If I set the variable to a simple string e.g elementSliderAppend="bear"; everything works fine. Commented Aug 23, 2013 at 10:48

3 Answers 3

1

<%= %> is an equivalent of Response.Write which is not intended to work with AJAX (which what UpdatePanel is). If you need to display something in an UpdatePanel - assign it to a property of a control (it could be some standard ASP.NET Control like Label and its Text property or even a SPAN with runat="server" and its innerHTML property)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks I dont need to display anything but using a variable currently I know only of <%= %> keep the variable but the value of this doesnt get updated with partial postback - the problem Im seeing is similiar to stackoverflow.com/questions/7647682/…
My apologizes, by displaying I actually mean "making accessible in client". And yes it will work during initial page load, but not during AJAX postbacks - you have to assign value to a control that is a part of UpdatPanel. If you don't want to display value - assign it to a hidden field.
1

you can also use hidden field.

Steps

1) set value of hidden field
2) get the value of hidden field using java script or jQuery

1 Comment

use $("#<%= hiddimgHtml.ClientID %>").val()
0

Thanks for everyones answers they all help resolve the issue Im using the hidden control and setting its value from code behind

The reason for the error on postback seemed to be the length of string that was assigned to the hidden field which caused the issues. To resolve I blanked the value in javascript before postback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.