AShah
AShah

Reputation: 942

Accessing a dynamic variable in javascript from Server Side using c#

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

Upvotes: 2

Views: 3658

Answers (3)

AShah
AShah

Reputation: 942

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

Upvotes: 0

Aijaz Chauhan
Aijaz Chauhan

Reputation: 1649

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

Upvotes: 1

suff trek
suff trek

Reputation: 39777

<%= %> 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)

Upvotes: 1

Related Questions