【问题标题】:Capturing the active control in an AutoPostBack在 AutoPostBack 中捕获活动控件
【发布时间】:2013-11-09 03:35:12
【问题描述】:

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

【问题讨论】:

  • Page_Load 事件中有代码吗?
  • @FelipeOriani - 我将其添加到问题中。

标签: c# asp.net checkbox


【解决方案1】:

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

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;*/

}

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多