循环通过中继器控件以获取asp.net中Textbox的值 [英] Looping through a repeater control to get values of Textbox in asp.net

查看:74
本文介绍了循环通过中继器控件以获取asp.net中Textbox的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to loop through my repeater control and get the textbox values.
However, I am getting an error:

{对象引用未设置为对象的实例."}

我的代码是:

    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本身是没有引用的对象.

The problem is that you're trying to access the ".Text" property of a TextBox that it didn't find. The TextBox itself is the object to which there is no reference.

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

Incidentally, the .Text property of an actual Textbox (one that exists and was found) can't be "Nothing". It can only be String.Empty or a valid string.

编辑了我的代码行

对不起,我的VB生锈了.

Sorry, my VB is rusty.

最终编辑

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

AARGH! I'm blind. I can't believe I didn't see this. There were TWO problems withthe original code. This is the answer to the second issue:

更改

txtField = rptRequestForm.FindControl("txtBox")

txtField = item.FindControl("txtBox")

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

The ITEM has to find the control, not the repeater itself!

我创建了一个小型Web应用程序,目的只是检查我是否正在抓取文本框的文本,并最终发现了上述问题.我的代码与您在aspx中的代码不同,但是下面是完整的代码清单,以便您查看其工作方式:

I created a small web app just to check to see if I was grabbing the textbox's text and finally found the issue above. my code is NOT the same as yours in the aspx, but here's a complete code listing so that you can see how it works:

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窗口中产生以下输出:

produces the following output in the System.Diagnostics.Debug window:

项目

坦率

AlternatingItem

AlternatingItem

戴夫

项目

穆罕默德

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

The thread 0x321c has exited with code 0 (0x0).

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

The thread 0x39b8 has exited with code 0 (0x0).

这篇关于循环通过中继器控件以获取asp.net中Textbox的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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