Related Question: Adding custom OnTextChange event handler on custom TextBox
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; }
}
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
}
Or hit:
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?
You need to set AutoPostBack=True
property of TextBox.
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;
}
}
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.
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;
}
}
In order for OnTextChanged
to fire, you need to specify AutoPostBack="true"
on the TextBox.
In the ASPX markup:
<uc:CustomTextBox ID="customTextBox" runat="server" OnTextChanged="CustomTextBox_OnTextChanged" AutoPostBack="true"></uc:CustomTextBox>
In the code-behind of the CustomTextBox user control:
public bool AutoPostBack
{
get
{
//the textbox in the user control
return customTextBox.AutoPostBack;
}
set
{
customTextBox.AutoPostBack = value;
}
}
精彩评论