【问题标题】:Can't change JavaScript variable created inside an UpdatePanel无法更改在 UpdatePanel 中创建的 JavaScript 变量
【发布时间】:2015-02-09 22:36:01
【问题描述】:

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

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

注意:我最初是在 web 用户控件中编写此代码,现在代码在根页面下

【问题讨论】:

    标签: javascript c# asp.net updatepanel


    【解决方案1】:

    这样试试

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

    然后在javascript中

    function myFunction(parameter){
       SHOWTHIS= parameter;
    }
    

    参考here

    【讨论】:

    • 这样 .js 函数将在第一次被调用...当我按下另一个按钮时该方法不响应
    • 将 RegisterStartupScript 替换为 RegisterClientScriptBlock
    • 仍然只调用一次:( ...我可以使用 Sys.Application.add_load(GridJson); 让它无限次调用;但是这样我在 js 变量中所做的每一个更改都会被忽略
    • 我认为还有其他问题。现在应该很好了。如果调用到达 javascript 函数,请尝试调试。您的更新面板是否触发了两个按钮的点击事件?
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      相关资源
      最近更新 更多