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

标签 javascript c# asp.net updatepanel

我被这个问题困扰好几天了。

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

问题是,我无法更改 UpdatePanel 内的变量“SHOWTHIS”值该值永远不会改变

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

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

已编辑...由于您的建议,我做到了,编辑这些行以使其正常工作

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

注意:我最初在网络用户控件中编写了此代码,现在代码位于根页面下

最佳答案

简单地尝试一下

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

然后在 JavaScript 中

function myFunction(parameter){
   SHOWTHIS= parameter;
}

引用here

关于javascript - 无法更改在 UpdatePanel 内创建的 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27422932/

相关文章:

javascript - OnClick 元素用 JS 填充数组

javascript - 在 extJS 中使用分页过滤商店

c# - C# 中的 ComImport,如何找到 shell32.dll 中 IFileDialog 等类的 GUID?

c# - Unity 中的 SQLite 数据库文件损坏

asp.net - 从存储过程获取结果以填充 GridView

Javascript 函数作为变量

javascript - 使用 NodeJS 在特定目录中运行命令

c# - 如何在Unity中制作径向渐变天空盒?

asp.net - 浏览 MVC3 DLL 为 'forgotten' 作为应用程序池回收的一部分

Asp.net core 2 与 Angular 6 模板