Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
{
        
}
like image 672
hicay Avatar asked Aug 02 '11 07:08

hicay


4 Answers

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.

like image 150
nthpixel Avatar answered Sep 17 '22 20:09

nthpixel


Just add TextBox, Label etc. controls and make them invisible. Then in the CheckBox1_CheckedChanged function make them visible. This is done by setting the bool Visible property

<asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" />
<asp:TextBox ID="textBox" runat="server" Visible=false />

and

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    textBox.Visible = true;
}
like image 21
Petar Ivanov Avatar answered Sep 16 '22 20:09

Petar Ivanov


Add a new literal control under your checkbox tags,

<asp:Literal id="lblMsg" runat="server"/>

then in your CheckBox1_CheckedChanged event to this:

lblMsg.Text = "Checked";

and yes set the AutoPostBack property of your checkbox to true

like image 27
Waqas Avatar answered Sep 20 '22 20:09

Waqas


I would suggest doing this using javascript. Your users won't have to "postback" and the feeling on the application will be better, and you will reduce the server charge.

Using jQuery, you can use something like this:

<script language="javascript" type="text/javascript">
 $(document).ready(function(){
      $("#chk").onclick(function() {
          $("#content").toggle();
      });
  });
</script>
<input type="Checkbox" id="chk"/>
<div id="content" style="display:none">
       <asp:TextBox runat="Server" id="oneOfYourControls" />
</div>

jQuery is not mandatory... you can use standard simple getElementById().

The only drawback, is that you cannot dynamically build the content, but in most case it's a not actually a matter

like image 44
Steve B Avatar answered Sep 20 '22 20:09

Steve B