0

当用户单击 ASP 页面中的复选框时,我只想看到一个 Html 表(id​​='xx')。一旦用户取消勾选复选框,表格应该再次不可见。

<table>
    <tr>
         <td colspan='2'>
               <table id='xx'>
                      <tr>
                          <td colspan='2'>
                                Student Information :
                           </td>
                           </tr>
                           <tr>
                           <td>
                             <asp:Label ID="Label1" runat="server" Text="Select Student name :"></asp:Label>
                           </td>
                           <td>
                              <asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
                                   </asp:DropDownList>
                           </td>
                       </tr>
    ...

我试过这段代码:

   protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

        if (CheckBox1.Checked == true)
        {
            // need a way to hide the Table id='xx' 
        }
        else {
            DropDownList1.Visible = true; // This is also not working
        }
    }

请帮我。

4

3 回答 3

3

回发时对 CheckBox 控件使用AutoPostBack="true"

于 2012-05-20T07:33:37.933 回答
1

试试这个...

<table>
    <tr>
        <td colspan='2'>
            <asp:CheckBox runat="server" ID="CheckBox1" Text="check" Checked="true" AutoPostBack="true"
                OnCheckedChanged="CheckBox1_CheckedChanged" />
            <table id='xx' runat="server">
                <tr>
                    <td colspan='2'>
                        Student Information :
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Select Student name :"></asp:Label>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    this.xx.Visible = CheckBox1.Checked;
}
于 2012-05-20T07:29:21.287 回答
1

你在这里有两个选择...

  1. 使用 runat = "server" 制作表服务器控件
  2. 在 Check/UnCheck CheckBox 控件上,将显示属性设置为 True/False。

选项 1 服务器端的句柄

<table id='xx' runat = "server">

现在您可以访问如下表控件

xx.Visible  = true/false;

选项 2 在客户端处理

<asp:CheckBox onclick="return SelectChk(this);" ID="chk" ></asp:CheckBox>

JavaScript 函数

<script language="javascript" type="text/javascript">
    function SelectChk(CtlId) {
        var IsChecked = document.getElementById(CtlId.id).checked;
        if (IsChecked) {
            document.getElementById(<%=xx.ClientID%>).style.display = 'block';
        }
        else{
            document.getElementById(<%=xx.ClientID%>).style.display = 'none';
        }
    }
</script>
于 2012-05-20T07:38:01.233 回答