使用TextBox TextChanged事件在asp.net中启用或禁用按钮控件

使用TextBox TextChanged事件在asp.net中启用或禁用按钮控件,asp.net,vb.net,Asp.net,Vb.net,我使用asp TextBox控件及其TextChanged事件,我的目标是在用户输入文本时捕获文本。如果输入了一个或多个字符,我希望在用户不必离开TextBox控件的情况下启用button控件 我在aspx页面上的文本框的源代码是 <asp:TextBox ID="NewSpendingCategoryTextBox" MaxLength="12" runat="server" AutoPostBack="True" OnTextChanged="NewSpendi

我使用asp TextBox控件及其TextChanged事件,我的目标是在用户输入文本时捕获文本。如果输入了一个或多个字符,我希望在用户不必离开TextBox控件的情况下启用button控件

我在aspx页面上的文本框的源代码是

<asp:TextBox ID="NewSpendingCategoryTextBox" MaxLength="12" runat="server" 
     AutoPostBack="True" 
     OnTextChanged="NewSpendingCategoryTextBox_TextChanged" 
     ViewStateMode="Enabled" >
</asp:TextBox>
我在代码隐藏页面上的源代码是

Protected Sub NewSpendingCategoryTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles NewSpendingCategoryTextBox.TextChanged
    Dim strSpendingCategoryTextBox As String = Nothing
    strSpendingCategoryTextBox = NewSpendingCategoryTextBox.Text
    If strSpendingCategoryTextBox.Length <= 0 Then
        Me.NewSpendingCategoryInsertButton.Enabled = False
    Else 'strSpendingCategoryTextBox.Length > 0
        Me.NewSpendingCategoryInsertButton.Enabled = True
    End If 'strSpendingCategoryTextBox.Length <= 0
End Sub
因此,似乎我必须使用javascript来启用或禁用插入按钮。有人能指导我如何在表中获取元素吗?这张桌子也放在一块嵌板上

下面是aspx代码

<asp:Panel ID="AddSpendingCategoryPanel" Visible="false" runat="server">
    <table class="AddNewTable">
        <tbody>
            <tr>
                <td>
                    <asp:Label ID="lblSpend" runat="server"  
                        Text="Spending Category:">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtSpend" MaxLength="12"  
                        runat="server" 
                        AutoPostBack="True"
                        OnTextChanged="txtSpend_TextChanged"
                        OnKeyDown="return CheckSpendTextBoxValue()"
                        ViewStateMode="Enabled" >
           </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button CssClass="frmbtn" ID="btnInsertSpend"  
                        runat="server" Text="Insert" />
                </td>
                <td>
                    <asp:Button CssClass="frmbtn" ID="btnCancelSpend"  
                        runat="server" Text="Cancel"
                        CausesValidation="False" />
                </td>
            </tr>
        </tbody>
    </table>
</asp:Panel>
在OnKePress事件中运行此代码或考虑JavaScript。文本框在使用Tab或Enter之前不会触发文本更改事件

简化布尔检查

Me.NewSpendingCategoryInsertButton.Enabled = (NewSpendingCategoryTextBox.Text.Length <> 0)

我不确定你会怎么做。但是ASP.NET代码是在承载网页的服务器上执行的

我强烈建议在可以在客户端运行的JavaScript上这样做。希望这篇文章对你有用


实际上,我应该澄清一下,我的目标是在文本框中的文本更改时触发TextChanged事件。现在它只有在控件失去焦点时才会触发。考虑到我可能必须使用Javascript来启用或禁用按钮,有人能指导我如何检索表中的元素以及面板中的元素吗?