asp.net 中的复选框控件(C#)

问题描述 投票:0回答:6
c# asp.net checkbox
6个回答
10
投票

别忘了

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" });
}

这允许您添加任何您想要的控件。


2
投票

只需添加TextBox、Label等控件并使其不可见即可。 然后在

CheckBox1_CheckedChanged
函数中使它们可见。 这是通过设置 bool
Visible
属性

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

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    textBox.Visible = true;
}

1
投票

在复选框标签下添加新的文字控件,

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

然后在您的 CheckBox1_CheckedChanged 事件中将此设置为:

lblMsg.Text = "Checked";

并且是的,将复选框的

AutoPostBack
属性设置为
true


1
投票

我建议使用 javascript 来完成此操作。您的用户不必“回发”,应用程序的感觉会更好,并且您将减少服务器费用。

使用 jQuery,你可以使用这样的东西:

<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 不是强制性的...您可以使用标准的简单

getElementById()

唯一的缺点是你无法动态构建内容,但在大多数情况下这实际上并不是问题


0
投票

在 Checked Changed Event 中编写如下代码:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
     Label1.Text = "Text";
}

0
投票
489checkbox
Report this ad
5

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)
{
        
}

    c#asp.netcheckbox

Share
Improve this question
Follow
edited Aug 6, 2021 at 8:40
Didier Aupest's user avatar
Didier Aupest
3,25522 gold badges2424 silver badges3737 bronze badges
asked Aug 2, 2011 at 7:35
hicay's user avatar
hicay
7811 gold badge22 silver badges88 bronze badges
Add a comment
5 Answers
Sorted by:
10

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.
Share
Improve this answer
Follow
answered Aug 2, 2011 at 7:44
nthpixel's user avatar
nthpixel
3,05933 gold badges3030 silver badges4242 bronze badges
Add a comment
2

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;
}

Share
Improve this answer
Follow
answered Aug 2, 2011 at 7:38
Petar Ivanov's user avatar
Petar Ivanov
92k1111 gold badges8282 silver badges9595 bronze badges

    you should wrap all your controls in a panel. It will be more simple to change the visibility of a single panel control than a lot of other controls. Moreover, the structural html tags will also be shown/hidden along the server controls – 
    Steve B
    Aug 2, 2011 at 8:12

Add a comment
1

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
Share
Improve this answer
Follow
answered Aug 2, 2011 at 7:40
Waqas's user avatar
Waqas
6,82233 gold badges3434 silver badges5050 bronze badges
Add a comment
1

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
Share
Improve this answer
Follow
answered Aug 2, 2011 at 8:01
Steve B's user avatar
Steve B
37.1k2121 gold badges103103 silver badges176176 bronze badges
Add a comment
0

In Checked Changed Event write your code like this:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
     Label1.Text = "Text";
}

Share
Improve this answer
Follow
answered Aug 2, 2011 at 7:42
thevan's user avatar
thevan
10.1k5454 gold badges138138 silver badges202202 bronze badges
Add a comment
Your Answer

    Links Images Styling/Headers Lists Blockquotes Code HTML Tables
    Advanced help

Thanks for contributing an answer to Stack Overflow!

    Please be sure to answer the question. Provide details and share your research!

But avoid …

    Asking for help, cl`enter code here`arification, or responding to other answers.
    Making statements based on opinion; back them up with references or personal experience.

To learn more, s
© www.soinside.com 2019 - 2024. All rights reserved.