无法将“System.Web.UI.WebControls.Label”类型的对象强制转换为“System.IConvertible”类型

时间:2012-05-02 03:32:55

标签: asp.net vb.net

我在转发器中创建学生评估表,应该提交评估响应并更新SQL数据库,但我不断收到标题中提到的错误。我使用VB在ASP.Net中编码。

这是我的代码: Student.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">

    <asp:Wizard ID="StudentEvaluationWizard" runat="server" ActiveStepIndex="0">
        <WizardSteps>
            <asp:WizardStep ID="WizardStep1" runat="server" Title="Course">
                <asp:GridView ID="CourseView" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="SectionID" >
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />
                        <asp:BoundField DataField="EnrollmentID" HeaderText="EnrollmentID" 
                            InsertVisible="False" ReadOnly="True" SortExpression="EnrollmentID" />
                        <asp:BoundField DataField="StudentID" HeaderText="StudentID" 
                            SortExpression="StudentID" />
                        <asp:BoundField DataField="SectionID" HeaderText="SectionID" 
                            SortExpression="SectionID" />
                        <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Section" >
                            <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Section.Course.Name") %>' ></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </asp:WizardStep>

            <asp:WizardStep ID="WizardStep2" runat="server" Title="Evaluation">
                <asp:Repeater ID="EvalRepeater" runat="server" 
                    DataSourceID="EvaluationQuestions">
                <HeaderTemplate><caption><asp:Label ID="EvaluationTitle" runat="server" Text='<%Eval("Evaluation.Evaluation1.") %>' /><caption></HeaderTemplate>

                <ItemTemplate>
                   <tr>
                <td>
                    <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' Visible="false" />
                </td>
                <td>
                    <asp:Label ID="Questions" runat="server" Text='<%# Eval("Question1") %>' />
                </td>
                <td>
                    <%--Move outside of table to make edits--%>
                    <asp:RadioButtonList ID="Question" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Value="1">1</asp:ListItem>
                        <asp:ListItem Value="2">2</asp:ListItem>
                        <asp:ListItem Value="3">3</asp:ListItem>
                        <asp:ListItem Value="4">4</asp:ListItem>
                        <asp:ListItem Value="5">5</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                <asp:TextBox ID="StudentComment" runat="server" />
                </td>
            </tr>
                </ItemTemplate>
        <FooterTemplate>

            </table>
        </FooterTemplate>
                </asp:Repeater>
                   <asp:Button ID="SubmitButton" runat="server" Text="Submit Evaluation" />
            </asp:WizardStep>
        </WizardSteps>
    </asp:Wizard>

    <asp:LinqDataSource ID="CourseSelect" runat="server" ContextTypeName="EvaluationDAL.Model.EvaluationDataDataContext"
        EntityTypeName="Enrollment" TableName="Enrollments" Where="StudentID == @StudentID">
        <WhereParameters>
            <asp:SessionParameter DefaultValue="StudentID" Name="StudentID" SessionField="StudentID"
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>

    <asp:LinqDataSource ID="EvaluationQuestions" runat="server" ContextTypeName="EvaluationDAL.Model.EvaluationDataDataContext"
        EntityTypeName="" TableName="Questions" 
        Where="EvaluationID == Convert.ToInt32(@EvaluationID)">
        <WhereParameters>
            <asp:SessionParameter Name="EvaluationID" SessionField="EvaluationID" 
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
</asp:Content>

Student.aspx.vb(带错误的提交按钮)

 Protected Sub SubmitButton_Click(sender As Object, e As System.EventArgs) Handles SubmitButton.Click

        Dim sr As New StudentEvaluation
        sr.SectionID = EvaluationGlobal.SectionID
        sr.EvaluationDate = DateAndTime.Now


        'Create a new evaluation object
        Dim eval As New StudentEvaluation


        For Each row As RepeaterItem In EvalRepeater.Items
            'Get the question
            Dim questionID As Label = CType(row.FindControl("QuestionID"), Label)
            Dim question1 As Integer = Convert.ToInt32(question1)

            eval.Comments = Convert.ToInt32(row.FindControl("Comments"))

            'Find radio button list with the name Question and convert to radio button list
            Dim rank As RadioButtonList = CType(row.FindControl("Question"), RadioButtonList)

            'Get the infomation if the area is not empty
            If Not String.IsNullOrWhiteSpace(rank.SelectedValue) Then
                Dim ser As New StudentEvaluationResponse
                ser.QuestionID = Convert.ToInt32(row.FindControl("QuestionID"))
                ser.Answer = Convert.ToInt16(row.FindControl("Question"))

                Dim questionRank As Integer = Convert.ToInt32(rank.SelectedValue)
                Dim studentComment As TextBox = CType(row.FindControl("StudentComment"), TextBox)
                Dim comment As String = studentComment.Text
                eval.StudentEvaluationResponses.Add(ser)

            End If
        Next
        If eval.StudentEvaluationResponses.Count > 0 Then
            eval.Insert()
        End If
    End Sub

我完全迷失了如何解决这个问题。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

这看起来错误:

Dim question1 As Integer = Convert.ToInt32(question1)

怀疑你的意思是:

Dim question1 As Integer = Convert.ToInt32(questionID.Text)

同样我怀疑这些:

ser.QuestionID = Convert.ToInt32(row.FindControl("QuestionID"))
ser.Answer = Convert.ToInt16(row.FindControl("Question"))

应使用Text属性:

ser.QuestionID = Convert.ToInt32(CType(row.FindControl("QuestionID"), Label).Text)
ser.Answer = Convert.ToInt16(CType(row.FindControl("Question"), RadioButton).Text)

这样你就试图转换控件的文本,而不是控件本身。