ASP.NET 在页面上放置动态控件会导致事件反向混乱

发布于 2024-08-05 10:17:25 字数 1512 浏览 3 评论 0原文

当我将文本框反向放置在页面上时,我遇到了这个奇怪的问题。整个事件系统都乱了。更改一个文本框会在所有文本框上触发 TextChange。我可以通过首先将控件放入列表中,然后在反向迭代列表时调用 add 来解决此问题。但我只是想知道为什么会失败。这是一些代码(.net 2.0)

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitFields();
    }

    private void InitFields()
    {
        int nrFields;
        //We have a static textbox called nrElements, this determines the number
        //of fields to initialize
        if (int.TryParse(nrElements.Text, out nrFields))
        {
            //Put all the dynamic fields on the screen in reverse order
            foreach(Control t in GetDynamicFields(nrFields))
            {
                //Calling Controls.Add works fine
                //Calling Controls.AddAt messes up the events
                //Try changing different textboxes
                plhFields.Controls.AddAt(0, t);
            }
        }
    }

    private IEnumerable<Control> GetDynamicFields(int nrFields)
    {
        for (int i = 0; i < nrFields; i++)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = string.Format("dynTextBox{0}", i.ToString());
            txtBox.AutoPostBack = true;
            txtBox.TextChanged += t_TextChanged;
            yield return txtBox;
        }
    }

    private void t_TextChanged(object sender, EventArgs e)
    {
        TextBox txtBox = sender as TextBox;
        if (txtBox != null)
            txtBox.Text = txtBox.Text + "Changed ";
    }
}

I have this weird problem when putting textboxes on the page in reverse. The whole event system is messed up. Changing one textbox fires TextChange on all textboxes. I can fix this by putting the controls in a list first and then call add while iterating trough the list in reverse. But i just want to know why this fails. Heres some code (.net 2.0)

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitFields();
    }

    private void InitFields()
    {
        int nrFields;
        //We have a static textbox called nrElements, this determines the number
        //of fields to initialize
        if (int.TryParse(nrElements.Text, out nrFields))
        {
            //Put all the dynamic fields on the screen in reverse order
            foreach(Control t in GetDynamicFields(nrFields))
            {
                //Calling Controls.Add works fine
                //Calling Controls.AddAt messes up the events
                //Try changing different textboxes
                plhFields.Controls.AddAt(0, t);
            }
        }
    }

    private IEnumerable<Control> GetDynamicFields(int nrFields)
    {
        for (int i = 0; i < nrFields; i++)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = string.Format("dynTextBox{0}", i.ToString());
            txtBox.AutoPostBack = true;
            txtBox.TextChanged += t_TextChanged;
            yield return txtBox;
        }
    }

    private void t_TextChanged(object sender, EventArgs e)
    {
        TextBox txtBox = sender as TextBox;
        if (txtBox != null)
            txtBox.Text = txtBox.Text + "Changed ";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

触ぅ动初心 2024-08-12 10:17:25

尝试在 Page_PreInit 事件而不是 Page_Load 上调用 InitFields()。

或者另一种方法是重写 CreateChildControls() 方法 (MSDN 文章),如果您使用 CreateChildControls(),则需要在 Page_Load 方法上调用 EnsureChildControls(),以确保在尝试访问具有以下功能的任何控件之前已调用 CreateChildControls() 方法:是在该方法中创建的。

Try calling InitFields() on the Page_PreInit event rather than Page_Load.

Or an alternative would be to override the CreateChildControls() method (MSDN Article), if you use CreateChildControls() you'll need to call EnsureChildControls() on the Page_Load method to make sure the CreateChildControls() method has been called before you try to access any controls which have been created within that method.

不喜欢何必死缠烂打 2024-08-12 10:17:25

始终将动态控件放在 OnInit 事件中。然后视图状态序列化器/反序列化器将起作用。而且您必须为每个请求添加控件,而不仅仅是在 !IsPostBack 中。

Always put dynamic controls in OnInit event. Then viewstate serializer/deserializer will work. And you have to add controls on every request, not just in !IsPostBack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文