2013-01-11 96 views
0

我想從5個文本框的值中取得迴應,然後將它們放入一個字符串中,並最終將它們存儲在數據庫中,但是我無法讓程序構建字符串, 這是我迄今爲止建立字符串的代碼。從文本框和Concat拉入字符串w /分隔符

VB.Code

 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> 
+1

你會得到一個異常,如果是,哪個和哪裏? –

+0

QuestionANS =的NullReference,我添加了一個手錶,並沒有任何值從窗體傳回,但似乎正確地引用了 – user1902540

+0

你在哪裏試圖找到控件以及這些控件在哪裏?你已經展示了一些aspx,但是不清楚這些文本框是否與'GridView'中的'NamingContainer'不同。 –

回答

2

我假設TexBoxes在不同的NamingContainer就像一個FormViewGridView。那麼這就解釋了爲什麼你會在QuestionANS = PassA.Text上得到NullRefernceException

編輯:既然你現在已經表明,它是在一個ItemTemplate一個DataList

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