Search code examples
c#asp.netcheckbox

CheckBox control in asp.net (C#)


I added a checkbox to my form.

When the checkbox is checked, some information (TextBox, Label etc.) should appear right under the checkbox.

How can I do this?

Code:

<asp:CheckBox ID="CheckBox1" runat="server" 
        oncheckedchanged="CheckBox1_CheckedChanged" />

C#:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
        
}

Solution

  • Don't forget autopostback = true

    <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
    <asp:Panel runat="server" ID="panel1"></asp:Panel>
    

    -

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        panel1.Controls.Add(new Label { Text = "Text goes here" });
    }
    

    This allows you to add any control you want.