0

我正在尝试从 5 个文本框值中获取响应,然后将它们放入一个字符串中,最终将它们存储在数据库中,但我无法让程序构建字符串,这是我迄今为止构建字符串的代码.

VB.代码

        Dim QuestionANS As String
        ' Get values from the text boxes
        Dim PassA As TextBox = CType(form1.FindControl("A"), TextBox)
        Dim PassB As TextBox = CType(form1.FindControl("B"), TextBox)
        Dim PassC As TextBox = CType(form1.FindControl("C"), TextBox)
        Dim PassD As TextBox = CType(form1.FindControl("B"), TextBox)
        Dim PassE As TextBox = CType(form1.FindControl("E"), TextBox)
        Dim PassF As TextBox = CType(form1.FindControl("F"), TextBox)

    QuestionANS = PassA.Text + "|" + PassB.Text + "|" + PassC.Text + "|" + PassD.Text + "|" + PassE.Text + "|" + PassF.Text

ASP 代码

  <asp:DataList runat="server" ID="Questions">   
            <ItemTemplate>
               <table cellpadding="2px" cellspacing="0" border="0" style="page-break- inside:avoid;">
                <tr>
                   <td valign="top" style="padding-right:18px;"><asp:Label               runat="server" ID="QuestionNumber" /></td>

                    <td><asp:Image runat="server" style = "padding: 15px 0px 25px 0px;"  ID="Image" /></td>

                </tr>

                <tr>
                    <td></td>
            <td style="padding-top:10px;padding-bottom:15px;"></td>

                    <td style="padding-top:10px;padding-bottom:15px;"></td>
                    <tr>

                        <td style="width:50px;"><a>A:</a></td><td><asp:TextBox id="A" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>B:</a></td> <td><asp:TextBox id="B"  columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>C:</a></td><td><asp:TextBox id="C" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>D:</a></td><td><asp:TextBox id="D" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>E:</a></td><td><asp:TextBox id="E" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>F:</a></td><td><asp:TextBox id="F" columns="30" runat="server" /></td>
                    </tr>
                </tr>

                    </table>
            <asp:HiddenField runat="server" ID="QuestionID" />
        </ItemTemplate>
    </asp:DataList>
4

1 回答 1

2

我假设在 a orTexBoxes中是不同的。那么这将解释为什么你得到一个on 。NamingContainerFormViewGridViewNullRefernceExceptionQuestionANS = PassA.Text

编辑:因为您现在已经表明它在 aItemTemplateDataList

For Each item As DataListItem In Questions.Items
    Dim PassA As TextBox = DirectCast(item.FindControl("A"), TextBox)
Next

或在ItemDataBound事件中:

Protected Sub Item_Bound(sender As Object, e As DataListItemEventArgs) Handles Questions.ItemDataBound
    Select Case e.Item.ItemType
        Case ListItemType.Item, ListItemType.AlternatingItem
            Dim PassA As TextBox = DirectCast(e.Item.FindControl("A"), TextBox)
    End Select
End Sub
于 2013-01-11T15:57:49.223 回答