循环通过repeater控件来获取asp.net中Textbox的值
我正在尝试循环遍历我的中继器控件并获取文本框值。
但是,我收到错误:
{“对象引用未设置为对象的实例。”}
我的代码是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
try
问题是您正在尝试访问它未找到的 TextBox 的“.Text”属性。 TextBox 本身是没有引用的对象。
顺便说一句,实际文本框(存在并被发现的文本框)的 .Text 属性不能是“Nothing”。它只能是 String.Empty 或有效字符串。
编辑了我的代码行
抱歉,我的 VB 生锈了。
最终编辑
啊啊啊!我是瞎子。我不敢相信我没有看到这个。原始代码有两个问题。这是第二个问题的答案:
更改
为
ITEM 必须找到控件,而不是中继器本身!
我创建了一个小型网络应用程序,只是为了检查我是否抓取了文本框的文本,最后发现了上面的问题。我的代码与您在 aspx 中的代码不同,但这里有一个完整的代码清单,以便您可以看到它是如何工作的:
vb code
aspx code
在 System.Diagnostics.Debug 窗口中生成以下输出:
Item
Frank
AlternatingItem
Dave
Item
Muhammad
线程 0x321c 已退出,代码为 0 (0x0)。
线程 0x39b8 已退出,代码为 0 (0x0)。
try
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
to
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
aspx code
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).
您必须将其正确转换为
Textbox
例如,这是 VB.NET 代码:
You have to properly cast it as
Textbox
e.g.Here is VB.NET code:
“确认用户是否输入了至少一个数量,
我不会使用任何东西——不确定这是否会导致问题,但通常我看到的是对象,而不是属性。 String.IsNullOrNothing() 用于检查字符串是否为 null 或空 ("")。
您无需担心文本框是否存在,因为如果它存在于中继器的一行中,那么它将存在于所有行中。我想如果您在设计时不确定“txtBox”是什么,您可以检查它是否为“无”……但除此之外,没有必要。
您绝对应该使用强制转换(CTYPE())。我认为如果您想要的只是 .text,您可能可以不使用它,但 CTYPE 可以让您访问文本框的所有属性(不仅仅是它的继承属性),而且您可能需要执行复选框或其他控件,在某些时候您几乎必须 CTYPE 才能到达 .ischecked 等。
'Confirm if user has entered atleast one quantity
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.
我做了一个通用方法来设置属性可见,我想你可以以它为例
I made a generic method for set the property visible, I think you can take it as an example