mbcharney mbcharney - 1 year ago 58
Vb.net Question

Checkbox won't stay unchecked or vice versa

I have looked many different solutions but almost all of them are not what I am trying to do.

I have an aspx page that has 5 checkboxes on it. I am pulling data from an SQL DB and setting the initial on/off state of the checkbox based on the retrieved value. This works fine.

The problem starts when I try to check or uncheck a checkbox. If it is checked it will uncheck for a split second and then the check returns or vice versa (unchecked to checked state).

Here are the checkboxes:

<asp:CheckBox ID="ckbIVPO" runat="server" Text=" Invoice Value & P.O.#" OnCheckedChanged="ckbIVPO_CheckedChanged" AutoPostBack="true" />
<asp:CheckBox ID="ckbQuNotes" runat="server" Text=" Quality Notes" OnCheckedChanged="ckbQuNotes_CheckedChanged" AutoPostBack="true" />
<asp:CheckBox ID="ckbShipTerms" runat="server" Text=" Ship To & Shipping Terms" OnCheckedChanged="ckbShipTerms_CheckedChanged" AutoPostBack="true" />
<asp:CheckBox ID="ckbMatSurFin" runat="server" Text=" Material/Surface/Finish" OnCheckedChanged="ckbMatSurFin_CheckedChanged" AutoPostBack="true" />
<asp:CheckBox ID="ckbFabFasExt" runat="server" Text=" Fabrication & Fastner Extras" OnCheckedChanged="ckbFabFasExt_CheckedChanged" AutoPostBack="true" />

The OnCheckedChange event for each one is basically the same (only control ID name changes):

If [control ID].Checked = checked Then
[control ID].Checked = False
[Control ID].checked = True
End IF

I also found an article here on SO that talked about using hidden text boxes and setting the checkbox based on the value from it. But I can't seem to get that to work. I think I am missing something there.

Here is that code:

If txbivpo.Text = "1" Then
ckbIVPO.Checked = True
ckbIVPO.Checked = False
End If
If txbshtrm.Text = "1" Then
ckbShipTerms.Checked = True
ckbShipTerms.Checked = False
End If
If txbqunotes.Text = "1" Then
ckbQuNotes.Checked = True
ckbQuNotes.Checked = False
End If
If txbmatsurfin.Text = "1" Then
ckbMatSurFin.Checked = True
ckbMatSurFin.Checked = False
End If
If txbfabfasext.Text = "1" Then
ckbFabFasExt.Checked = True
ckbFabFasExt.Checked = False
End If

I just can't seem to get this to work. I am sure that I am missing something but I am not sure what it is. I am also not set in stone as to doing it this way. I am using VB because I am not very good with C#. (Actually I flat out suck at C#!) I am using the hidden text boxes to pull the data for the update query if the user changes something and I need to update the SQL.

Can anyone help me or point me to a clear example of how to simply have the check box change state, and stay, and then be able to update the hidden text box so the query will update correctly?

Answer Source

This is a guess since the code for setting the correct textbox from the DB is not shown. But I think you have to wrap that piece of code in a IsPostBack check:

If Not IsPostBack Then
End If

What probably happens is when a checkbox is clicked a PostBack is performed. In that PostBack , presumably in Page_load, the default values are retrieved from the database and will overwrite the ones the user clicked.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download