Search code examples
javascriptc#asp.netupdatepanel

Can't change JavaScript variable created inside an UpdatePanel


I'm stuck in this problem for days.

1- User press one of the buttons.
2- The button pressed insert a .js variable inside LABEL->lbJavaScriptVar
3- Sys.Application.add_load(GridJson); calls the .js function

The problem is, i can't change the variable "SHOWTHIS" value inside an UpdatePanel the value never change

-----------------------------[aspx]------------------------------------

<head>
<script type="text/javascript">
    function GridJson()
    {
        alert(SHOWTHIS);
        document.getElementById("Grid").innerHTML = 
                "<tr><td>" + SHOWTHIS+ "</td></tr>"
    }            
</script>
</head>

...

<asp:UpdatePanel runat="server">
<ContentTemplate>

<asp:Button ID="btMan" runat="server" OnClick="btMan_click" Text="Man"/>
<asp:Button ID="btWoman" runat="server" OnClick="btWoman_click" Text="Woman"/>

<asp:Label ID="lbJavaScriptVar" runat="server" ></asp:Label>

<table id="Grid" border="5" class="cssMiniGrid"></table>
<script type="text/javascript">Sys.Application.add_load(GridJson);</script>

</ContentTemplate>
</asp:UpdatePanel>

-----------------------------[/aspx]------------------------------------

-----------------------------[.cs]------------------------------------

(btMan_click)...
lbJavaScriptVar.Text = "<script> var SHOWTHIS= 9999</script> ";

(btWoman_click)...
lbJavaScriptVar.Text = "<script> var SHOWTHIS = 7777</script> ";

-----------------------------[/.cs]------------------------------------

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

EDITED...I made it thanks to your sugestions, edit these lines to make it work

.ASPX<
[ALTER]function GridJson() ------> function GridJson(SHOWTHIS) {

.CS
[DELETE](btMan_click)... 
[DELETE]lbJavaScriptVar.Text = "<script> var SHOWTHIS= 9999</script> ";   
[DELETE](btWoman_click)... 
[DELETE]lbJavaScriptVar.Text = "<script> var SHOWTHIS = 7777</script> "; 
[INCLUDE]ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "GridJson(" + "THANKS" + ");", true);

Note: I made this code at first inside a web user control, now the code is under the root page


Solution

  • try simply in this way

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "myFunction('value');", true);
    

    then in javascript

    function myFunction(parameter){
       SHOWTHIS= parameter;
    }
    

    refer here