在 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();
}