从asp.net中的repeater控件获取数据绑定值
作为我的问题的后续循环通过repeater控件获取asp.net中Textbox的值 如果用户在文本框中输入了数量,我想从中继器控件中获取相应的 ProductID 值:
<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="Supply Name"></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"><asp:Label ID="lblProductName" Text="<%#Trim(Eval("Product_Title"))%>" ><%#Trim(Eval("Supplie_title"))%></asp:Label></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>
我的代码隐藏是:
For Each item As RepeaterItem In rptRequestForm.Items
txtField = item.FindControl("txtBox")
If Not IsNothing(txtField) Then
j += 1
strText = strText & ", " & txtField.Text
End If
Next
我正在使用 FindControl 循环遍历文本框(以查看用户是否输入了有效数量)。如何获取对应的ProductID的值?
As a follow up to my question Looping through a repeater control to get values of Textbox in asp.net
If user has entered a a quantity in the textbox, I want to get the corresponding ProductID value from the repeater control:
<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="Supply Name"></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"><asp:Label ID="lblProductName" Text="<%#Trim(Eval("Product_Title"))%>" ><%#Trim(Eval("Supplie_title"))%></asp:Label></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>
My codebehind is:
For Each item As RepeaterItem In rptRequestForm.Items
txtField = item.FindControl("txtBox")
If Not IsNothing(txtField) Then
j += 1
strText = strText & ", " & txtField.Text
End If
Next
I am using FindControl to loop through the textbox (to see if user has entered a valid quantity). How can I get the value of the corresponding ProductID ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 ItemTemplate 中添加一个标签,如下所示:
在后面的代码中,找到如下所示的标签:
Add a label to your ItemTemplate, like this:
And in your code behind, find it like this:
您担心获得没有数量的产品名称,然后在分配之前进行评估。例如(基于@James的代码),您只需添加
IF Quantity > 0 THEN ...
在执行string ProductName = ...
之前,我会对该代码进行一些更改...
1)检查该对象之前是否存在您试图将其转换为文本框。例如,
IF NOT item.FindControl("txtBox") is NOTHING...
2) 在尝试
convert.toint32()
之前检查 null 条件。例如,IF NOT string.isnullorempty(txtField)...
3) 当您执行 Quantity 变量赋值时,您不需要 FindControl,因为您已经找到它(并赋值给它)到您的 txtField 变量)。所以只需
int Quantity = Convert.ToInt32(txtField.Text);
You're worried about getting product names where there is no quantity, then just evaluate before you assign. For example (based on @James' code), you just add
IF Quantity > 0 THEN ...
before you dostring productName = ...
A couple of changes I'd make to that code, though...
1) check for the object to exist before you attempting to convert it to a textbox. For example,
IF NOT item.FindControl("txtBox") is NOTHING...
2) check for a null condition before you attempt
convert.toint32()
. for example,IF NOT string.isnullorempty(txtField)...
3) When you do the Quantity variable assignment, you don't need FindControl, because you already found it (and assigned it to your txtField variable). So just
int Quantity = Convert.ToInt32(txtField.Text);
您可以将
ProductId
存储在转发器 ItemTemplate 的隐藏字段
中。然后您可以以与使用FindControl
访问数量相同的方式访问产品 IDYou can store the
ProductId
in ahidden field
in the repeater ItemTemplate. Then You can access the Product Id in the same way, as you are accessing the quantity usingFindControl