我正在开展一个小项目,并且遇到了一些复选框问题。我通过代码动态添加行和列,为ASP表添加了许多复选框。除了当我根据加载预先存在的记录检查它们时,这一切都正常。由于某些原因,当将新行添加到表中时,某些复选框将设置为TRUE。这是代码的一小部分:
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.Visible = 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
任何见解都会很棒。
谢谢,
答案 0 :(得分:0)
由于必须触发ASP.NET复选框的checked equals false newChk.Checked = True
的默认值。您需要检查Array.IndexOf(arClass, newChk.ID) >= 0
的值,是否已完成此操作?
此外,由于newChk
只是持有对象的引用(而不是对象本身),newCol.Controls.Add(newChk)
可能只是添加对列的引用(因此所有复选框都是相同的!)