【问题标题】:Checkbox.checked change when adding new dynamic row添加新动态行时的 Checkbox.checked 更改
【发布时间】:2011-04-26 16:54:43
【问题描述】:

我正在做一个小项目,但在使用复选框时遇到了一些问题。我通过动态添加行和列的代码向 ASP 表添加了许多复选框。这一切都很好,除非我根据加载预先存在的记录来检查它们。由于某种原因,当将新行添加到表中时,某些复选框被设置为 TRUE。这是代码的小sn-p:

    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

任何见解都会很棒。

谢谢,

【问题讨论】:

    标签: javascript asp.net html vb.net


    【解决方案1】:

    由于 ASP.NET 复选框的默认值已选中等于 false newChk.Checked = True 必须被触发。您需要检查Array.IndexOf(arClass, newChk.ID) &gt;= 0 的值,您这样做了吗?

    此外,由于newChk 只是持有对对象的引用(而不是对象本身)newCol.Controls.Add(newChk) 可能只是添加对列的引用(因此所有复选框都相同!)

    【讨论】:

    • 谢谢。我确实检查了数组,此时它是空的。复选框正在正确创建。我可以通过使用 FindControl("chkClass_3") 等来检查任何复选框的状态......并且有问题的复选框显示为 TRUE。
    • 我还想注意一件事。在执行这行代码之前,复选框的选中状态为 FALSE..."tblClass.Rows.Add(newRow)"
    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 2013-08-23
    • 2015-03-13
    • 2019-11-17
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多