简体   繁体   English

使用C#在Asp.Net中执行Text Box_TextChanged事件?

[英]Execute Text Box_TextChanged Event in Asp.Net using C#?

I am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. 我正在尝试对文本框执行验证,例如是否为null,然后显示一条消息以填充空白字段并检查输入文本的长度。 I have written the code for it inside TextBox_TextChanged event but it's not working. 我已经在TextBox_TextChanged事件中为其编写了代码,但无法正常工作。 The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. 该事件没有被触发,用户可以在没有用户名的情况下进行注册,这是我目前的问题。 Do I have to trigger the event manually? 我是否必须手动触发事件? Here is a glimpse of what I am doing: 这是我在做什么的一瞥:

protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
    {
        String firstNameEntered = FirstN_TextBox.Text;
        if (firstNameEntered != null)
        {
            if (firstNameEntered.Length <= 20)
            {
                MessageBox.Show("Inside text box");
            }
            else
            {

            }

        }
        else
        {
            FirstN_TextBox.Focus();
            MessageBox.Show("Please fill the marked field");
        }

change it like this: 像这样更改它:

From: 从:

<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px"   OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>

To: 至:

 <asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px"   OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>

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

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