1

我正在尝试根据用户在文本框中输入的内容填充转发器上的标签。但是,在文本框中输入的值没有被拾取,因此当我单击要在标签中显示文本的按钮时没有任何反应。

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

中继器的其余部分工作正常,只是填充标签不起作用。这是我第一次发问题,希望有人能帮忙。我尽我所能搜索这个问题,但似乎没有任何效果。

谢谢。

4

1 回答 1

0

你需要注意两件事:

1.在page_Load中你需要确保你没有在PostBack中绑定repeater:

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 按钮的 click 方法中,不要绑定中继器(如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
于 2013-10-06T16:38:43.230 回答