繁体   English   中英

如果TextBox不为空,如何禁用它?

[英]How to disable TextBox if it not empty By JavaScript on page Load

我有一个asp.net页面,在ListView中包含一些文本框
我想通过asp.net ItemDataBound ListView事件或Javascript代码禁用包含某些文本的文本框,该怎么办?

<asp:ListView ID="ListView1" runat="server"  DataKeyNames="ID" >
    <ItemTemplate>
        <tr class="xl68" height="29" style='mso-height-source: userset; height: 21.75pt'>

        <td >&nbsp; <asp:Label ID="lblID" runat="server" Visible="false" Text='<%# Eval("ID") %>'></asp:Label></td>
        <td class="xl66" style='border-top: none'><%# Container.DataItemIndex + 1 %> </td>
        <td class="xl69" width="351" style='border-top: none; border-left: none; width: 263pt'> <%# Eval("Name") %></td>
        <td><asp:TextBox runat="server" MaxLength="2" Text='<%# Bind("C1") %>' ID="txb1"  ></asp:TextBox></td>
        <td><asp:TextBox runat="server" MaxLength="2" Text='<%# Bind("C2") %>' ID="txb2"  ></asp:TextBox></td>
        <td><asp:TextBox runat="server" MaxLength="2" Text='<%# Bind("C3") %>' ID="txb3"  ></asp:TextBox></td>
        <td><asp:TextBox runat="server" MaxLength="2" Text='<%# Bind("C4") %>' ID="txb4"  ></asp:TextBox></td>
        <td><asp:TextBox runat="server" MaxLength="2" Text='<%# Bind("C5") %>' ID="txb5"  ></asp:TextBox></td>
        <td class="xl67">&nbsp;</td>

        </tr>

    </ItemTemplate>
</asp:ListView>

在每个TextBox控件中添加CssClass属性,如下所示

<asp:TextBox runat="server" MaxLength="2" ID="txb1" CssClass="myCss"  ></asp:TextBox>

在aspx中添加Js函数

 function DisableInput(){

     var inputs = $('input.myCss[type="text"]');
     inputs.each(function( index ) 
     {
       if( $( this ).text() !='')
       {
        $( this ).attr('disabled',true);
       }
     });

 }

在您的Page_Load事件上添加此代码

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:DisableInput(); ", true);

在您的Page_Load事件上添加此代码

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:disablewithText(); ", true);

在该javaScript函数中,您可以遍历所有文本框并检查是否具有值,因此可以设置disable属性。

我在Page_Load事件中的此代码中在CodeBehind中解决了它

foreach (ListViewItem row in ListView1.Items)
                    {
                        foreach (Control txt in row.Controls)
                        {
                            if (txt is TextBox)
                            {
                                if (((TextBox)txt).Text != "")
                                    ((TextBox)txt).Enabled = false;
                            }
                        }

                    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM