I want to create a UserControl in which I will have a checkbox and a textbox. If the checkbox is checked then enable property of textbox is true else it is false.
This is what I have in my page :
<form id="form1" runat="server">
<div>
<custom:NullableTextBox ID="NullableTextBox1" OnCheckedChanged="NullableTextBox1_OnCheckedChanged" runat="server"></custom:NullableTextBox>
</div>
</form>
And this is the class of my UserControl :
public partial class NullableTextBox : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public NullableTextBox()
{
CheckBox1.CheckedChanged += new EventHandler(CKB_CheckedChanged);
this.Controls.Add(CheckBox1);
this.Controls.Add(TextBox1);
}
private CheckBox CheckBox1 = new CheckBox();
private TextBox TextBox1 = new TextBox();
private void CKB_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked)
{
TextBox1.Enabled = true;
}
else
{
TextBox1.Text = string.Empty;
TextBox1.Enabled = false;
}
}
}
Nothing is happening when I check or uncheck the checkbox and I would like it to be updated instantly without doing a postback.