Checkbox.checked change when adding new dynamic row
I am working on a little project and am having some issues with a checkbox. I am adding a number of checkboxes to an ASP table via code by adding rows and columns dynamically. This all works fine except for when I go to check them based on loading a preexisting record. For some reason some of the checkboxes are getting set to TRUE when the new row is added to the table. Here is a little snippet of the code:
newRow = New TableRow
newRow.ID = "Class_R" + X.ToString.Trim
newRow.Visible = True
newRow.Width = Unit.Percentage(100)
If CD.HasRows Then
While CD.Read() And (Not all_done)
If Y <= 3 Then
newCol = New TableCell
newCol.ID = newRow.ID + "Class_C" + Y.ToString.Trim
newCol.Visible = True
newCol.Width = Unit.Percentage(33)
newChk = New CheckBox
newChk.ID = "chkClass_" + CD("class_id").ToString.Trim
newChk.Text = CD("class_desc").ToString.Trim
newChk.Visible = True
newChk.Checked = False
If Not CD("class_desc").ToString.Trim.ToUpper = "OTHER" Then
newChk.Attributes.Add("onclick", "javascript:chkClass_clicked(this)")
Else
newChk.Attributes.Add("onclick", "javascript:chkClass_Other_clicked(this)")
End If
If Array.IndexOf(arClass, newChk.ID) >= 0 Then
newChk.Checked = True
Else
newChk.Checked = False
End If
newCol.Controls.Add(newChk)
If CD("class_desc").ToString.Trim.ToUpper = "OTHER" Then
newTxt = New TextBox
newTxt.ID = "txtClass_" + CD("class_id").ToString.Trim
newTxt.Attributes.Add("onblur", "javascript:update_class_txt_value(this)")
newChk.Checked = IIf(hidClass_chk.Value = "True", True, False)
If newChk.Checked = False Then
newTxt.Attributes.Add("style", "display:none")
Else
newTxt.Attributes.Add("style", "display:inline")
End If
newTxt.Text = hidClass_txt.Value
newCol.Controls.Add(newTxt)
End If
newRow.Cells.Add(newCol)
Y += 1
Else
tblClass.Rows.Add(newRow)
X += 1
newRow = New TableRow
newRow.ID = "Class_R" + X.ToString.Trim
newRow.Visi开发者_运维问答ble = True
newRow.Width = Unit.Percentage(100)
Y = 1
newCol = New TableCell
newCol.ID = newRow.ID + "Class_C" + Y.ToString.Trim
newCol.Visible = True
newCol.Width = Unit.Percentage(33)
newChk = New CheckBox
newChk.ID = "chkClass_" + CD("class_id").ToString.Trim
newChk.Text = CD("class_desc").ToString.Trim
newChk.Visible = True
newChk.Checked = False
If Not CD("class_desc").ToString.Trim.ToUpper = "OTHER" Then
newChk.Attributes.Add("onclick", "javascript:chkClass_clicked(this)")
Else
newChk.Attributes.Add("onclick", "javascript:chkClass_Other_clicked(this)")
End If
If Array.IndexOf(arClass, newChk.ID) >= 0 Then
newChk.Checked = True
Else
newChk.Checked = False
End If
newCol.Controls.Add(newChk)
If CD("class_desc").ToString.Trim.ToUpper = "OTHER" Then
newTxt = New TextBox
newTxt.ID = "txtClass_" + CD("class_id").ToString.Trim
newTxt.Attributes.Add("onblur", "javascript:update_class_txt_value(this)")
newChk.Checked = IIf(hidClass_chk.Value = "True", True, False)
If newChk.Checked = False Then
newTxt.Attributes.Add("style", "display:none")
Else
newTxt.Attributes.Add("style", "display:inline")
End If
newTxt.Text = hidClass_txt.Value
newCol.Controls.Add(newTxt)
End If
newRow.Cells.Add(newCol)
Y += 1
End If
*'This is the line causing the checked state to change'*
tblClass.Rows.Add(newRow)
End While
Any insight would be great.
Thanks,
Since an ASP.NET checkbox's default value for checked equals false newChk.Checked = True
must be getting fired. You'll need to check the value of Array.IndexOf(arClass, newChk.ID) >= 0
, have you done this?
In addition since newChk
is simply holding a reference to an object (not the object itself) newCol.Controls.Add(newChk)
may be only adding the reference to the column (hence all off the checkboxes are the same!)
精彩评论