我试图在用户在文本框中输入的内容中填充转发器上的标签。但是,在文本框中输入的值不会被选中,因此当我单击要取消标签中文本的按钮时,没有任何反应。
我的VB代码在这里:
Protected Sub cmdShowCost_Click(sender As Object, e As EventArgs) Handles cmdShowCost.Click
rptProducts.DataBind()
Dim someString As String
Dim txtField As TextBox
Dim lblField As Label
Dim j As Integer = 0
For Each item In rptProducts.Items
txtField = CType(item.FindControl("txtAmountToBuy"), TextBox)
lblField = CType(item.FindControl("lblCost"), Label)
If Not IsNothing(txtField) Then
j = j + 1
someString = txtField.Text
lblField.Text = someString
Else
lblField.Text = 0
End If
Next
End Sub
我的html在这里:
<div>
<table>
<asp:Repeater ID="rptProducts" runat="server" >
<HeaderTemplate>
<tr>
<td>
Product Name
</td>
</td>
<td>
Price per Kg
</td>
<td>
Kg in Stock
</td>
<td>
Action
</td>
<td>
Amount to Buy
</td>
<td>
Cost
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("Product")%>
</td>
<td>
<%#Eval("PricePerKg")%>
</td>
<td>
<%#Eval("QuantityKg")%>
</td>
<td>
<a href='amount.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem, "ProductID")%>&Stock=<%#Eval("QuantityKg")%>'>Details...</a>
</td>
<td>
<asp:TextBox ID="txtAmountToBuy" runat="server"/>
</td>
<td>
<asp:Label runat="server" ID="lblCost" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
</table>
</div>
转发器的其余部分工作正常,它只是填充不起作用的标签。 这是我第一次发帖提问,希望有人可以提供帮助。我尽力搜索这个问题,但似乎没什么用。
感谢。
答案 0 :(得分:0)
你需要照顾两件事:
1.在page_Load中你需要确保你没有在PostBack中绑定转发器:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
rptProducts.DataSource = GetProducts() 'Or whatever datasource you have
rptProducts.DataBind()
End If
End Sub
2.在cmdShowCost按钮的单击方法中,不要绑定转发器(如ajakblackgoat所述):
Protected Sub cmdShowCost_Click(sender As Object, e As EventArgs) Handles cmdShowCost.Click
'rptProducts.DataBind()
Dim someString As String
Dim txtField As TextBox
Dim lblField As Label
Dim j As Integer = 0
For Each item In rptProducts.Items
txtField = CType(item.FindControl("txtAmountToBuy"), TextBox)
lblField = CType(item.FindControl("lblCost"), Label)
If Not IsNothing(txtField) Then
j = j + 1
someString = txtField.Text
lblField.Text = someString
Else
lblField.Text = 0
End If
Next
End Sub