暴露并使用OnTextChange事件处理程序 [英] Exposing and then using OnTextChange Event handler

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

问题描述

相关问题:在自定义TextBox上添加自定义OnTextChange事件处理程序

在相关问题中,我问我如何在自定义文本框控件中公开OnTextChange,并通过以下方式解决:

In the related question I asked how I could expose OnTextChange in my custom textbox control and we resolved it by:

public event EventHandler TextChanged
{
    add { customTextBox.TextChanged += value; }
    remove { customTextBox.TextChanged -= value; }
}

当控件实现时,我正在尝试使用TextChanged事件: / p>

I am trying use TextChanged event like this when the control is implemented:

    <uc:CustomTextBox ID="customTextBox"
                      runat="server"
                      OnTextChanged="CustomTextBox_OnTextChanged">
    </uc:CustomTextBox>

运行这个时候似乎没有出现以下情况:

This never seems to hit the following when running:

    protected void CustomTextBox_OnTextChanged(System.EventArgs e)
    {
     // Do something here
    }

或者点击:

    protected void CustomTextBox_OnTextChanged(object sender, EventArgs e)
    {
     // Do something here
    }

我做错了什么,我错过了什么,这是最好的方式或常用的做法,我想在这里做的一切吗?

What am I doing wrong, what am I missing out and is this the best way or common practice way to do everything I am trying to do here?

推荐答案

您需要设置TextBox的 AutoPostBack = True 属性。

You need to set AutoPostBack=True property of TextBox.

如果您正在设计Web用户控件,那么只需定义公共属性即可在用户控件的code中设置 CustomTextBox 代码隐藏:

If you are designing a web user control then simply define public property to set True/False value of CustomTextBox in user control's code-behind:

public   bool AutoPostBack
    {
        get
        {
            return CustomTextBox.AutoPostBack;
        }
        set
        {
            CustomTextbox.AutoPostBack = value;
        }
    }

如果您正在开发自定义Web控件,那么您可以覆盖自定义的 AutoPostBack 属性。如果您不想自定义 AutoPostBack 属性,那么不要覆盖它。

If you are developing a custom web control then you can to override the AutoPostBack property for the customization. If you don't want to customize AutoPostBack property then don't override it.

如果您覆盖AutoPostBack属性,请调用超级类的默认实现。

In case you override AutoPostBack property, please invoke the super class's default implementation.

public override  bool AutoPostBack
    {
        get
        {
            return base.AutoPostBack;
        }
        set
        {
            base.AutoPostBack = value;
        }
    }

这篇关于暴露并使用OnTextChange事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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