无法将类型'System.Web.UI.Control'转换为'System.Web.UI.UserControl

时间:2014-10-31 20:34:39

标签: asp.net vb.net

我的代码是这样的 ![在此输入图像说明] [1]

但我得到一个Null异常错误:(

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim textquestionselected As New List(Of Integer)
        Dim imagequestionselected As New List(Of Integer)
        Dim text_question_id As Integer
        Dim image_question_id As Integer
        For Each gridviewrow As GridViewRow In GridView1.Rows
            If (CType(gridviewrow.FindControl("CheckBox1"), CheckBox).Checked) Then

                text_question_id = Convert.ToInt16(CType(gridviewrow.FindControl("small_int_question_id"), Label).Text)
                textquestionselected.Add(text_question_id)
            End If
        Next
        For Each gridviewrow1 As GridViewRow In GridView2.Rows
            If (CType(gridviewrow1.FindControl("CheckBox2"), CheckBox).Checked) Then

                image_question_id = CType(gridviewrow1.FindControl("small_int_question_id"), Label).Text
                imagequestionselected.Add(image_question_id)
            End If
        Next

enter image description here

我使用绑定的Field而不是Label ..但是同样的错误

1 个答案:

答案 0 :(得分:1)

你必须检查它的行类型;它可能是页眉或页脚行。检查RowType属性以确保它是DataRow。

if (row.RowType == DataControlRowType.DataRow)
{
   //Check control 
}

同样根据您的主题,确保您要转换的类型相同。看到标记的片段真的很有帮助。

相关问题