gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:14:24 33 4
gpt4 key购买 nike

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

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

我的代码是:

    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 中的代码不同,但这里有一个完整的代码 list ,以便您可以了解它是如何工作的:

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

关于asp.net - 循环通过repeater控件来获取asp.net中Textbox的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7043829/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com