在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时才能正常工作。这是完全合理的,因为这是它检查的第一个复选框。我希望能够来回点击其中一个,并相应地更改启用的字段。
任何参赛者?
ADD:这是我的Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadSubjects();
LoadStatusCodes();
chkUsers.Checked = true;
chkAccount.Checked = false;
}
CheckboxStatus();
}
答案 0 :(得分: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;*/
}