繁体   English   中英

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

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

码:

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

C#

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{

}

我在表单中添加了复选框。

选中复选框后,复选框下方应显示一些信息(TextBox,Label等)。

我怎样才能做到这一点?

不要忘记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" });
}

这允许您添加所需的任何控件。

只需添加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;
}

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

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

然后在你的CheckBox1_CheckedChanged事件中:

lblMsg.Text = "Checked";

并将复选框的AutoPostBack属性设置为true

我建议用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()

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

在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