文本框使用textchanged事件锁定 [英] Textbox locks up using textchanged event

查看:102
本文介绍了文本框使用textchanged事件锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2015,webform,C#

我有一个文本框,用户输入一个数值,在标签显示时会触发以下代码





I'm using Visual Studio 2015, webform, C#
I've got a textbox which the user enters a numeric value, upon tabbing out it fires the following code


protected void txtAsset_TextChanged(object sender, EventArgs e)
    {
        lblSuccessful.Text = string.Empty;
        txtAsset.Focus();
        string input = txtAsset.Text;
        if (!Regex.IsMatch(input, @"^[0-9]\d*"))
        {
            lblSuccessful.CssClass = "ErrorMessage";
            lblSuccessful.Text = "You have input invalid criteria";
            txtAsset.Text = string.Empty;
            txtAsset.Focus();
        }
        else
        {
          Execute Retrieval of record
        }





它的工作原理很完美,但是当用户输入另一个数字时,你可以在文本框中输入它锁定的数字,你不能删除它或输入任何内容。



自动回发功能已开启。



任何帮助表示赞赏



我尝试过:



我已经删除了验证但它仍然发生,这是一个Microsoft Bug



It works perfectly however, when the user types another number in sometimes you can type into the textbox othertimes it locks up, you can't delete the value or type anything.

Auto postback is on.

Any assistance appreciated

What I have tried:

I've removed the validation and it still happens, is this a Microsoft Bug

推荐答案

查看代码:每次更改txtAsset TextBox Text属性时,都会触发TextChanged事件。你在活动中做了什么?更改同一TextBox的Text属性。这会导致另一个事件被触发。哪个更改了文本,这会导致一个事件,哪个...你得到了图片。把更改带到文本框,你的问题就会消失。
Look at your code: every time the txtAsset TextBox Text property is changed, you get a TextChanged event fired. And what do you do inside the event? Change the Text property of the same TextBox. Which causes another event to be fired. Which changes the Text, which causes an event, which ... you get the picture. Take the change to the textbox out, and your problem will disappear.



非常感谢你的回复,我没有关注你。



我的事件只会在文本框中跳出时触发,如果验证成功,则输入代码从数据库中检索记录,删除了大部分代码并删除了验证。仍会导致问题。
Hi Many thanks for your reply, I don't follow you.

My event only fires upon tabbing out of the textbox, if the validation is successful it enters the code to retrieve the record from the database, have stripped out most of the code and removed the validation and it still causes issues.


在事件内部代码的开头设置断点并逐步执行代码。你会看到到底发生了什么。



如果您不熟悉调试器,这里有一段视频可以让您快速入门:使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



以下是修复StackOverflow异常的方法:

Set a breakpoint at the start of the code inside the event and step through the code. You will see exactly what is happening.

If you are not familiar with the debugger, here is a video that will get you started quickly: Basic Debugging with Visual Studio 2010 - YouTube[^]

And here is how to fix the StackOverflow exception:
private bool isChanging;

protected void txtAsset_TextChanged(object sender, EventArgs e)
{
    // are we in the middle of chaning the txtAsset.Text property?
    // Yes, don't process the change that the code is in the middle
    //      of changing
    if (isChanging) return;
    
    // No? Okay, set the falg and make the change...
    isChanging = true;

    lblSuccessful.Text = string.Empty;
    txtAsset.Focus();
    string input = txtAsset.Text;
    if (!Regex.IsMatch(input, @"^[0-9]\d*"))
    {
        lblSuccessful.CssClass = "ErrorMessage";
        lblSuccessful.Text = "You have input invalid criteria";
        
        // this next line fires the txtAsset.TextChanged event...
        txtAsset.Text = string.Empty;
        
        txtAsset.Focus();
    }
    else
    {
      Execute Retrieval of record
    }
    
    //We are all done...
    isChanging = false;
}


这篇关于文本框使用textchanged事件锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆