asp.net中的CheckBox控件(C#)

omtl5h9j  于 5个月前  发布在  .NET
关注(0)|答案(5)|浏览(73)

我在表单中添加了一个复选框。
当复选框被选中时,一些信息(文本框,标签等)应该出现在复选框的正下方。
我该怎么做?
代码:

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

字符串
C#:

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

ckx4rj1h

ckx4rj1h1#

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


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

yxyvkwin

yxyvkwin2#

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

k4aesqcs

k4aesqcs3#

在复选框标记下添加一个新的文本控件,

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

字符串
然后在CheckBox1_CheckedChanged事件中,

lblMsg.Text = "Checked";


和yes设置AutoPostBack属性的复选框为true

ffx8fchx

ffx8fchx4#

我建议你使用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()
唯一的缺点是,您不能动态地构建内容,但在大多数情况下,这实际上不是一个问题

bgibtngc

bgibtngc5#

在修改后的事件中,像这样编写代码:

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

字符串

相关问题