gwydion93
gwydion93

Reputation: 1923

Why is my asp:Checkbox returning false when its checked?

In my asp:net app, I have 2 checkboxes set up the same way. One works fine:

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

The other one always shows uxCloseDateCheckbox.Checked value as false; even when its been checked. Therefore, the stuff in the conditional statement never gets fired! What am I doing incorrectly here?

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

Upvotes: 0

Views: 838

Answers (1)

Drewskis
Drewskis

Reputation: 449

Looks as though you're calling the wrong checkbox in the _CheckChanged method.

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

Should be:

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

Upvotes: 2

Related Questions