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

发布于 2024-11-29 10:50:46 字数 2078 浏览 2 评论 0原文

我正在尝试循环遍历我的中继器控件并获取文本框值。
但是,我收到错误:

{“对象引用未设置为对象的实例。”}

我的代码是:

    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>

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

{"Object reference not set to an instance of an object."}

my code is:

    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

UPDATE: aspx code is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的笑 2024-12-06 10:50:46

try

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 code

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 code

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

Item

Frank

AlternatingItem

Dave

Item

Muhammad

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

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

try

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

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.

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.

Edited my line of code

Sorry, my VB is rusty.

Final edit

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:

Change

txtField = rptRequestForm.FindControl("txtBox")

to

txtField = item.FindControl("txtBox")

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

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 code

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 code

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

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

Item

Frank

AlternatingItem

Dave

Item

Muhammad

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

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

孤檠 2024-12-06 10:50:46

您必须将其正确转换为 Textbox 例如,

TextBox txtField = (TextBox)rptRequestForm.FindControl("txtBox") // C# code

这是 VB.NET 代码:

Dim txtField As TextBox = CType(rptRequestForm.FindControl("txtBox"), TextBox)

You have to properly cast it as Textbox e.g.

TextBox txtField = (TextBox)rptRequestForm.FindControl("txtBox") // C# code

Here is VB.NET code:

Dim txtField As TextBox = CType(rptRequestForm.FindControl("txtBox"), TextBox)
暮年 2024-12-06 10:50:46
Dim myText as string
Dim j As Integer = 0

“确认用户是否输入了至少一个数量,

For Each myItem as repeateritem In rptRequestForm.Items
    If NOT string.isnullorempty(CTYPE(myItem.FindControl("txtBox"),textbox).text) then
        j += 1
    End If
Next

我不会使用任何东西——不确定这是否会导致问题,但通常我看到的是对象,而不是属性。 String.IsNullOrNothing() 用于检查字符串是否为 null 或空 ("")。

您无需担心文本框是否存在,因为如果它存在于中继器的一行中,那么它将存在于所有行中。我想如果您在设计时不确定“txtBox”是什么,您可以检查它是否为“无”……但除此之外,没有必要。

您绝对应该使用强制转换(CTYPE())。我认为如果您想要的只是 .text,您可能可以不使用它,但 CTYPE 可以让您访问文本框的所有属性(不仅仅是它的继承属性),而且您可能需要执行复选框或其他控件,在某些时候您几乎必须 CTYPE 才能到达 .ischecked 等。

Dim myText as string
Dim j As Integer = 0

'Confirm if user has entered atleast one quantity

For Each myItem as repeateritem In rptRequestForm.Items
    If NOT string.isnullorempty(CTYPE(myItem.FindControl("txtBox"),textbox).text) then
        j += 1
    End If
Next

I wouldn't use nothing -- not sure that causes a problem or not, but usually I see that for objects, not properties. String.IsNullOrNothing() is made for checking strings for null or empty ("").

You don't need to worry about whether or not the textbox exists, because if it exists in one row of the repeater, it will exist in all rows. I guess you could check it for 'nothing' if you weren't sure what "txtBox" was at design time...but otherwise, not necessary.

You should definately use the cast (CTYPE()). I think you might be able to get away with not using it if all you want is .text, but the CTYPE gives you access to all of the textbox's properties (not just it's inherited properties), and also, you might need to do checkboxes or other controls at some point where you pretty much have to CTYPE in order to get to .ischecked, etc.

能否归途做我良人 2024-12-06 10:50:46

我做了一个通用方法来设置属性可见,我想你可以以它为例

Sub SetVisibleControlRepeater(ByRef repetidor As Repeater, ByVal idControl As String, ByVal esVisible As Boolean)

        For Each item As RepeaterItem In repetidor.Items

            Dim boton As System.Web.UI.WebControls.Button = CType(item.FindControl(idControl), Button)

            boton.Visible = esVisible

        Next

    End Sub

I made a generic method for set the property visible, I think you can take it as an example

Sub SetVisibleControlRepeater(ByRef repetidor As Repeater, ByVal idControl As String, ByVal esVisible As Boolean)

        For Each item As RepeaterItem In repetidor.Items

            Dim boton As System.Web.UI.WebControls.Button = CType(item.FindControl(idControl), Button)

            boton.Visible = esVisible

        Next

    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文