简体   繁体   English

Asp.net中的CheckBox控件(c#)

[英]CheckBox control in Asp.net (c#)

Code: 码:

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

c# C#

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{

}

I added checkbox to my form. 我在表单中添加了复选框。

When Checkbox checked, some information(TextBox, Label etc.) should be appear right under the checkbox. 选中复选框后,复选框下方应显示一些信息(TextBox,Label等)。

How can i do this? 我怎样才能做到这一点?

Don't forget autopostback = true 不要忘记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. 这允许您添加所需的任何控件。

Just add TextBox, Label etc. controls and make them invisible. 只需添加TextBox,Label等控件并使它们不可见。 Then in the CheckBox1_CheckedChanged function make them visible. 然后在CheckBox1_CheckedChanged函数中使它们可见。 This is done by setting the bool Visible property 这是通过设置bool Visible属性来完成的

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

Add a new literal control under your checkbox tags, 在复选框标记下添加新的文字控件,

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

then in your CheckBox1_CheckedChanged event to this: 然后在你的CheckBox1_CheckedChanged事件中:

lblMsg.Text = "Checked";

and yes set the AutoPostBack property of your checkbox to true 并将复选框的AutoPostBack属性设置为true

I would suggest doing this using javascript. 我建议用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: 使用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 is not mandatory... you can use standard simple getElementById() . jQuery不是必需的......你可以使用标准的简单getElementById()

The only drawback, is that you cannot dynamically build the content, but in most case it's a not actually a matter 唯一的缺点是,你无法动态构建内容,但在大多数情况下,它实际上并不是一个问题

In Checked Changed Event write your code like this: 在Checked Changed Event中,编写如下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM