1

在 C# 环境中使用 ASP.Net,我有以下代码:

<table>
      <tr>
        <td width="15"><asp:CheckBox ID="chkUsers" runat="server" Text="" AutoPostBack = "True" oncheckedchange="chkUsers_OnCheckedChange" />
        </td>
        <td width="260">
            Active Users:
        </td>
        <td width="120">
            <asp:DropDownList ID="cboActiveUsers" runat="server" Height="19px" 
                AutoPostBack="true" onselectedindexchanged="cboActiveUsers_SelectedIndexChanged">
                <asp:ListItem>Y</asp:ListItem>
                <asp:ListItem>N</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td width="15"><asp:CheckBox ID="chkAccount" runat="server" Text="" AutoPostBack = "True" oncheckedchange="chkAccount_OnCheckedChange" />
        </td>
        <td width="260">
            Account Name:
        </td>
        <td width="120">
            <asp:DropDownList ID="cboAccounts" runat="server" Height="19px" 
                AutoPostBack="true" onselectedindexchanged="cboAccounts_SelectedIndexChanged">
                <asp:ListItem>Y</asp:ListItem>
                <asp:ListItem>N</asp:ListItem>
            </asp:DropDownList>
        </td>
      </tr>
    </table>

我正在尝试使用复选框作为切换。首先,让我说 OnCheckedChange 部分永远不会运行。不知道为什么。无论如何,在我的回发中(在代码隐藏中)我试图找出哪个复选框是刚刚单击的复选框,所以我知道要启用和禁用哪些控件。我想说的是(明显的航空代码):

If chkUsers is the checkbox that was just clicked
{
                cboActiveUsers.Enabled = true;
                ddlAuditor.Enabled = true;
                cboAccounts.Enabled = false;
}
If chkAccount is the checkbox that was just clicked
{
                cboActiveUsers.Enabled = false;
                ddlAuditor.Enabled = false;
                cboAccounts.Enabled = true;
}

现在,我的代码如下所示:

protected void CheckboxStatus()
{
    if (chkAccount.Checked == true)
    {
        cboActiveUsers.Enabled = false;
        ddlAuditor.Enabled = false;
        cboAccounts.Enabled = true;
        chkUsers.Checked = false;

    }
    if (chkUsers.Checked == true)
    {
        cboActiveUsers.Enabled = true;
        ddlAuditor.Enabled = true;
        cboAccounts.Enabled = false;
        chkAccount.Checked = false;
    }
}

这在检查 chkAccount 时工作正常,但是当我去切换时,它只有在我首先取消选中 chkAccount 时才会起作用。这很有意义,因为这是它检查的第一个复选框。我希望能够来回单击一个或另一个,并相应地更改启用的字段。

有接盘侠吗?

添加:这是我的 Page_Load:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        LoadSubjects();
        LoadStatusCodes();
    chkUsers.Checked = true;
    chkAccount.Checked = false;

    }
    CheckboxStatus();
}
4

1 回答 1

0

您的逻辑看起来不错,但在Page_Load事件中,您应该只在第一个 loa 上进行修改。我对您的代码进行了一些更改,请查看下面的评论,例如:

public void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadSubjects();
        LoadStatusCodes();

        chkUsers.Checked = true;
        chkAccount.Checked = false;

        CheckboxStatus();
    }

    // here, every postback will execute
    // that's the reason you are getting every postback the called method
}

protected void chkUsers_OnCheckedChange(object sender, EventArgs e)
{
    CheckboxStatus();
}

protected void cboAccounts_SelectedIndexChanged(object sender, EventArgs e)
{
    CheckboxStatus();
}

protected void CheckboxStatus()
{       
    if (chkAccount.Checked)
    {
        cboActiveUsers.Enabled = false;
        ddlAuditor.Enabled = false;
        cboAccounts.Enabled = true;
        chkUsers.Checked = false;
    }

    // you could do it.. to toogle the enabled controls, instead on in checked
    /* 
    cboActiveUsers.Enabled = !chkAccount.Checked;
    ddlAuditor.Enabled = !chkAccount.Checked;
    cboAccounts.Enabled = chkAccount.Checked;
    chkUsers.Checked = chkAccount.Checked;
    */

    if (chkUsers.Checked == true)
    {
       cboActiveUsers.Enabled = true;
       ddlAuditor.Enabled = true;
       cboAccounts.Enabled = false;
       chkAccount.Checked = false;
    }    

    /*
    cboActiveUsers.Enabled = chkUsers.Checked;
    ddlAuditor.Enabled = chkUsers.Checked;
    cboAccounts.Enabled = !chkUsers.Checked;
    chkAccount.Checked = !chkUsers.Checked;*/

}
于 2013-11-08T19:35:12.073 回答