Asp.net

循環通過中繼器控制項以獲取 asp.net 中文本框的值

  • March 31, 2015

我正在嘗試遍歷我的轉發器控制項並獲取文本框值。

但是,我收到一個錯誤:

{“你呼叫的對像是空的。”}

我的程式碼是:

   Dim txtField As TextBox
   Dim j As Integer = 0

  'Confirm if user has entered atleast one quantity
   For Each item In rptRequestForm.Items
       txtField = rptRequestForm.FindControl("txtBox")
       If txtField.Text <> Nothing Then
           j += 1
       Else

       End If
   Next

更新: aspx程式碼是:

       <td><asp:Repeater ID="rptRequestForm" runat="server">
           <HeaderTemplate>
                   <table border="0" width="100%">
                       <tr>
                           <td style="width:50%" class="TextFontBold"><asp:Label runat="server" ID="Label1" Text="Product"></asp:Label></td>
                           <td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label2" Text="Quantity"></asp:Label></td>
                           <td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label3" Text="Price (ea.)"></asp:Label></td>
                       </tr>
                   </table>
           </HeaderTemplate>
               <ItemTemplate>
                   <table border="0" width="100%">
                       <tr>
                           <td style="width:50%" class="TextFont"><span><%#Trim(Eval("Product_Title"))%></span></td>
                           <td style="width:25%"><asp:TextBox ID="txtBox" runat="server" Width="30%" onblur="Javascript:numberonly(this)"></asp:TextBox></td>
                           <td style="width:25%" class="TextFont"><span><%#Trim(FormatCurrency(Eval("Price")))%></span></td>
                       </tr>
                   </table>
               </ItemTemplate>
           </asp:Repeater>

嘗試

Dim someString as String = "Not set"  <-- used later to hold the values of the string
Dim txtField As TextBox    
Dim j As Integer = 0   
'Confirm if user has entered atleast one quantity    
For Each item In rptRequestForm.Items        
  txtField = item.FindControl("txtBox")        
  If Not IsNothing(txtField) Then      ' <--- this is the line I changed       
    j += 1  
    someString = txtField.Text ' <--  once you've checked and know that the textbox exists, you just grab the value like so. 
    ' do whatever you like with the contents of someString now.     
  Else        
  End If    
Next

問題是您試圖訪問它沒有找到的 TextBox 的“.Text”屬性。TextBox 本身是沒有引用的對象。

順便說一句,實際文本框(存在並被發現)的 .Text 屬性不能是“Nothing”。它只能是 String.Empty 或有效字元串。

編輯了我的程式碼行

對不起,我的 VB 生鏽了。

最終編輯

啊!我瞎了。我不敢相信我沒有看到這個。原始程式碼有兩個問題。這是第二個問題的答案:

改變

txtField = rptRequestForm.FindControl("txtBox")

txtField = item.FindControl("txtBox")

ITEM 必須找到控制項,而不是中繼器本身!

我創建了一個小型網路應用程序,只是為了檢查我是否抓取了文本框的文本,最後發現了上面的問題。我的程式碼與您在 aspx 中的程式碼不同,但這裡有一個完整的程式碼清單,以便您了解它是如何工作的:

VB程式碼

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Dim t As New System.Data.DataTable

       t.Columns.Add("Name")

       Dim newRow(1) As Object

       t.Rows.Add(New Object() {"Frank"})
       t.Rows.Add(New Object() {"Dave"})
       t.Rows.Add(New Object() {"Muhammad"})

       rptRequestForm.DataSource = t
       rptRequestForm.DataBind()

       Dim txtField As TextBox
       Dim j As Integer = 0   'Confirm if user has entered atleast one quantity    
       For Each item As RepeaterItem In rptRequestForm.Items
           txtField = item.FindControl("txtBox")
           If Not IsNothing(txtField) Then     ' <--- this is the line I changed            
               j += 1
               System.Diagnostics.Debug.WriteLine(item.ItemType.ToString())
               System.Diagnostics.Debug.WriteLine(txtField.Text)
           Else
               System.Diagnostics.Debug.WriteLine(item.ItemType.ToString())
           End If
       Next
End Sub

aspx 程式碼

<asp:Repeater ID="rptRequestForm" runat="server">
       <HeaderTemplate>
           Hello!
       </HeaderTemplate>
       <ItemTemplate>
           <asp:TextBox ID="txtBox" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox>
           <br />
       </ItemTemplate>
</asp:Repeater>

在 System.Diagnostics.Debug 視窗中產生以下輸出:

物品

坦率

交替項目

戴夫

物品

穆罕默德

執行緒 0x321c 以程式碼 0 (0x0) 退出。

執行緒 0x39b8 以程式碼 0 (0x0) 退出。

引用自:https://stackoverflow.com/questions/7043829