【问题标题】:Enable textbox inside Repeater through JS function通过JS函数启用Repeater内的文本框
【发布时间】:2013-11-05 04:02:31
【问题描述】:

我的文本框 (asp.net) 位于中继器详细信息部分的 DIV 和面板中(切换) 我如何访问 java 脚本函数中的文本框以使其在单击同一面板中的链接按钮时启用。

   <div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">

   <asp:Panel ID="Panel2" runat="server" Height="195px" BackColor="gray" Font-Bold="False"   ForeColor="Maroon">
  <br />
      <asp:Label ID="Label1" runat="server" Text="LicenseID"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval   (Container.DataItem,"LicenseID") %>' Enabled="False" BackColor="Gray"  BorderStyle="None"></asp:TextBox>

我试过这种方法,但显示错误

    <script type="text/javascript">
      function MyJSFunction() {
             var tet = document.getElementById("<%=TextBox1.ClientID %>");

                       }
 </script>

但显示以下错误消息。

BC30451:“TextBox1”未声明

由于其保护级别,它可能无法访问。

编辑 这是我在 vb.net 中的代码。但是转换为 js 函数我发现了错误

       If e.CommandName = "edit" Then 
       DirectCast(e.Item.FindControl("TextBox2"),   TextBox).Enabled = True
       DirectCast(e.Item.FindControl("Textbox2"), TextBox).BorderStyle =BorderStyle.NotSet 
       DirectCast(e.Item.FindControl("Textbox2"), TextBox).BackColor = Drawing.Color.White       
      end if 

【问题讨论】:

    标签: javascript asp.net vb.net


    【解决方案1】:

    这是因为在中继器中,您的 id 变为 yourRepeaterID_TextBox1_Sequence 使用 Javascript 获取所有输入。像这样的:

      <script>
        function MyJSFunction() {
          var tb = document.getElementsByTagName('INPUT');
          for (var i = 0; i < tb.length; i++) {
            if (tb[i].type != "text") { continue; } // this is not TextBox
            // do whatever
          }
        }
      </script>
    

    【讨论】:

    • 非常感谢,我已经编写了 vb.net 代码来做同样的事情,但为了避免回发,我正在重新编写代码。下面是我必须重写的服务器端代码。 If e.CommandName = "edit" Then DirectCast(e.Item.FindControl("TextBox2"), TextBox).Enabled = True DirectCast(e.Item.FindControl("Textbox2"), TextBox).BorderStyle = BorderStyle.NotSet DirectCast (e.Item.FindControl("Textbox2"), TextBox).BackColor = Drawing.Color.White end if
    【解决方案2】:

    像这样使用带有选择器的 jquery:

    var textBox = $(".details input[type='text'][id*='TextBox1']");
    

    问候, 乌罗斯

    【讨论】:

    • 如何启用中继器文本框
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2013-05-04
    相关资源
    最近更新 更多