ASP.net web TextChangedEvent未触发

时间:2018-02-24 16:25:15

标签: c# html asp.net web textbox

我试图建立一个网站,其中包含一些有趣的功能,需要使用文本框进行有效的TextChanged事件。这就是我试图做的事情:

文本框html代码

C#嵌入代码

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.TextChanged += TextBox1_TextChanged;
}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    DateTime time = new DateTime();

    try
    {
        int textBoxValue = Convert.ToInt32(TextBox1.Text);
        Label2.Text = "valid" + time.ToString("hh:mm:ss");
    }
    catch
    {
        Label2.Text = "test";
        TextBox1.Text = "1";
    }
}

https://pastebin.com/mAyKMLE5还包含一些HTML代码。

不知何故,我认为它根本没有注册任何textChangedEvents。我认为页面必须重新加载才能注册它?无论如何,我希望有人可以帮助我。如果您需要有关此问题的其他信息,我很乐意给予。

提前致谢, DutchJelly

2 个答案:

答案 0 :(得分:2)

如果TextBox上已不存在AutoPostBack = true,则还需要以编程方式添加TextBox1.TextChanged += TextBox1_TextChanged; TextBox1.AutoPostBack = true;

println

答案 1 :(得分:0)

<asp:TextBox ID="TextBox1" runat="server" Style="margin-top: 0px" Width="32px" OnTextChanged="TextBox1_TextChanged" AutoPostBack="True" Font-Size="Small" Height="16px">1</asp:TextBox>  

在文本框中添加AutoPostBack="True"