【问题标题】:Javascript do not return a proper value when used in a content page ASP.NETJavascript 在内容页面 ASP.NET 中使用时不返回正确的值
【发布时间】:2012-12-12 06:32:27
【问题描述】:

我对 Javascript(Jquery) 完全陌生,并且仍然是 ASP.NET 的初学者。 我有一个包含网格视图的内容页面 gvUsers 我为事件 RowDatabound 编写了一个代码,该代码在其标记中为每一行添加了一个“id”。 并且它的行是可点击的,并且可以通过 contentpage 页面中的 javascript 来选择。

  <script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
             <script type="text/javascript">
                 $(function() {

                $('#<%=gvUsers.ClientID%> tr[id]').click(function() {
                    $('#<%=gvUsers.ClientID%> tr[id]').css({ "background-color":  "White", "color": "Black" });
                    $(this).css({ "background-color": "Black", "color": "White" });          
                });

                $('#<%=gvUsers.ClientID%> tr[id]').mouseover(function() {
                    $(this).css({ cursor: "hand", cursor: "pointer" });
                });

            });

                 $(function() {

                var RowID = $('#hdnEmailID').val();
               if (RowID != "0") {
                    $('#<%=gvUsers.ClientID%> tr[id=' + RowID + ']').css({ "background- color": "Black", "color": "White" });
               }

                $('#<%=gvUsers.ClientID%> tr[id]').click(function() {
                    $('#<%=gvUsers.ClientID%> tr[id]').css({ "background-color": "White", "color": "Black" });
                    $(this).css({ "background-color": "Black", "color": "White" });
                    $('#hdnEmailID').val($(this).attr("id"));
                });

                $('#<%=gvUsers.ClientID%> tr[id]').mouseover(function() {
                    $(this).css({ cursor: "hand", cursor: "pointer" });
                });

                });

            </script>

在这些脚本中,它们获取所选行的“id”属性并将其保存到 hdnEmailID...

<INPUT id="hdnEmailID" type="hidden" value="0" runat="server" >

为了使 hdnEmailID 值显示在页面中,我在按钮单击事件中编写了一个代码,该事件具有这样的代码..

Response.write(hdnEmailID.value);

起初它工作得很好并显示我单击的行的“id”但是在我在主内容页面中使用此代码之后 脚本返回 0。似乎当我单击按钮时.. 母版页已重新加载,似乎脚本也在重新加载,因此返回默认 0 我不确定这是否真的发生了,因为我是 ASP.NET 的初学者 :)

【问题讨论】:

    标签: c# javascript jquery asp.net master-pages


    【解决方案1】:

    是因为你没有使用渲染的id,错误就在这段代码上:

      var RowID = $('#hdnEmailID').val();
    

      $('#hdnEmailID').val($(this).attr("id"));
    

    应该是这样的

      var RowID = $('#<%=hdnEmailID.ClientID%>').val();
    

      $('#<%=hdnEmailID.ClientID%>').val($(this).attr("id"));
    

    控件呈现的 id 不是您在后面的代码中看到的,当您在单个页面上使用它时有时是相同的,但是当您将它添加到 master 时会动态更改,现在在您的html 渲染代码,您需要渲染该动态 id。

    在 asp.net 版本 4 中,控件上有一个参数,您可以使用它来使 them static and not change 成为 ClientIDMode ="Static"

    类似:
    How do I access a DIV from javascript, if ASP.NET mangles its ID?
    Accessing control client name and not ID in ASP.NET
    Get clientid in user control from external javascript file
    How to find and modify an asp.net control with JavaScript?

    【讨论】:

    • 非常感谢!它真的奏效了!从昨晚开始我一直在调试它,但无法追踪我的问题谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2021-04-22
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    相关资源
    最近更新 更多