无法更改在UpdatePanel内创建的JavaScript变量

时间:2014-12-11 12:29:26

标签: javascript c# asp.net updatepanel

我已经陷入这个问题好几天了。

1-用户按其中一个按钮 2-按下按钮在 LABEL-> lbJavaScriptVar 内插入.js变量 3- Sys.Application.add_load(GridJson); 调用.js函数

问题是,我无法改变变量" SHOWTHIS" UpdatePanel中的值值永远不会改变

----------------------------- [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] ------------- -----------------------

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

已编辑 ...感谢你的sugestions,编辑这些行以使其正常工作

.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);

注意:我首先在Web用户控件中创建了此代码,现在代码位于根页面

2 个答案:

答案 0 :(得分:1)

以这种方式尝试

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

然后在javascript中

function myFunction(parameter){
   SHOWTHIS= parameter;
}

参考here

答案 1 :(得分:0)