无法将类型为"System.Web.UI.WebControls.Label"的对象转换为类型为"System.IConvertible"的对象 [英] Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.IConvertible'

查看:61
本文介绍了无法将类型为"System.Web.UI.WebControls.Label"的对象转换为类型为"System.IConvertible"的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在转发器中创建了一个学生评估表,该表单应该提交评估响应并更新SQL数据库,但是我一直收到标题中提到的错误.我正在使用VB在ASP.Net中进行编码.

I'm creating a student evaluation form in a repeater that should submit the evaluation responses and update the SQL database, but I keep getting the error mentioned in the title. I'm coding in ASP.Net using VB.

这是我的代码:Student.aspx

Here's my code: 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(带有错误的提交"按钮)

Student.aspx.vb (Submit button with the error)

 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

我完全不知道如何解决此问题.任何帮助都会很棒.

I'm completely lost on how to fix this. Any help would be awesome.

推荐答案

以以下内容开头似乎是错误的:

This looks wrong to start with:

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)

这样,您尝试转换控件的文本,而不是控件本身.

That way you're trying to convert the text of the control, not the control itself.

这篇关于无法将类型为"System.Web.UI.WebControls.Label"的对象转换为类型为"System.IConvertible"的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆