为什么我的asp:Checkbox选中后返回false?

时间:2019-03-01 17:58:58

标签: c# asp.net checkbox

在我的asp:net应用程序中,我以相同的方式设置了2个复选框。一种有效:

ASPX

<asp:CheckBox ID="uxOwnershipCheckBox" runat="server" OnCheckedChanged="uxOwnershipCheckBox_CheckedChanged" Checked="false" AutoPostBack="true"/>

C#

protected void uxOwnershipCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        if (uxOwnershipCheckBox.Checked)
        {
            DataTable ownershipDT = _dtMgr.GetTicketByStatus_Everyone(uxStatusDropdownList.SelectedValue);
            uxTktGridView.DataSource = ownershipDT;
            uxTktGridView.DataBind();
            uxTicketCounter.Text = ownershipDT.Rows.Count.ToString();
        }
        else
        {
            DataTable ownershipDT = _dtMgr.GetTicketByStatus(uxStatusDropdownList.SelectedValue, Session["UserNameSession"].ToString());
            uxTktGridView.DataSource = ownershipDT;
            uxTktGridView.DataBind();
            uxTicketCounter.Text = ownershipDT.Rows.Count.ToString();
        }
    }

另一个总是将uxCloseDateCheckbox.Checked的值显示为false;即使已对其进行了检查。因此,条件语句中的内容永远不会被解雇!我在这里做错了什么?

ASPX

<asp:CheckBox runat="server" style="padding-right:1px; float:right; margin-right:170px;" ID="uxCloseDateCheckbox" Text="Closure Date" TextAlign="Right" OnCheckedChanged="uxCloseDateCheckbox_CheckedChanged" Checked="false" AutoPostBack="true"/>

C#

protected void uxCloseDateCheckbox_CheckedChanged(object sender, EventArgs e)
    {
        if (uxOwnershipCheckBox.Checked)
        {
            DateTime dateTicketClosed = DateTime.ParseExact(uxDateTimeLocalTextbox.Text, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
        }
    }

1 个答案:

答案 0 :(得分:2)

看起来好像您在_CheckChanged方法中调用了错误的复选框。

protected void uxCloseDateCheckbox_CheckedChanged(object sender, EventArgs e)
{
    if (uxOwnershipCheckBox.Checked) //This is where your issue is..
    {
        DateTime dateTicketClosed = DateTime.ParseExact(uxDateTimeLocalTextbox.Text, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    }
}

应该是:

protected void uxCloseDateCheckbox_CheckedChanged(object sender, EventArgs e)
{
    if (uxCloseDateCheckbox.Checked)
    {
        DateTime dateTicketClosed = DateTime.ParseExact(uxDateTimeLocalTextbox.Text, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    }
}