循环通过转发器控件来获取asp.net中的Textbox值

Fra*_*ank 8 vb.net asp.net repeater

我试图循环我的转发器控件并获取文本框值.
但是,我收到一个错误:

{"你调用的对象是空的."}

我的代码是:

    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
Run Code Online (Sandbox Code Playgroud)

更新: 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>
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 14

尝试

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
Run Code Online (Sandbox Code Playgroud)

问题是您正在尝试访问它找不到的TextBox的".Text"属性.TextBox本身是没有引用的对象.

顺便说一句,实际文本框(存在和找到的文本框)的.Text属性不能是"Nothing".它只能是String.Empty或有效的字符串.

编辑了我的代码行

对不起,我的VB生锈了.

最后编辑

AARGH!我瞎了.我不敢相信我没有看到这一点.原始代码存在两个问题.这是第二个问题的答案:

更改

txtField = rptRequestForm.FindControl("txtBox")
Run Code Online (Sandbox Code Playgroud)

txtField = item.FindControl("txtBox")
Run Code Online (Sandbox Code Playgroud)

ITEM必须找到控件,而不是转发器本身!

我创建了一个小型Web应用程序,只是为了检查我是否抓住了文本框的文本,最后发现了上面的问题.我的代码与你在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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在System.Diagnostics.Debug窗口中生成以下输出:

项目

坦率

AlternatingItem

戴夫

项目

穆罕默德

线程0x321c已退出,代码为0(0x0).

线程0x39b8已退出,代码为0(0x0).