简体   繁体   中英

Get specific item from Repeater in code behind

I have an ASP.NET WebForms project that displays data read from a database via a Repeater. The code for the repeater looks like this:

<asp:Repeater ID="repRMAproduct" runat="server">
     <ItemTemplate>
          <tr>
               <td>
                    <%# Eval("Description") %>
               </td>
               <td>
                    <%#  Eval("Qty") %>
               </td>
               <td>
                    <asp:TextBox ID="tbNewQty" runat="server"></asp:TextBox>
               </td>
          </tr>
     </ItemTemplate>
</asp:Repeater>

What I want is for the user to enter a new Quantity only on the rows that he wants to enter. Then, when he clicks the save button, I can read these new Quantities along with their associated Descriptions.

In MVC I should build the URL using jQuery and pass it to the Controller. But in WebForms it seems less intuitive. Where do I go from here?

In your save-button-click event handler you can loop all items and get the TextBox with FindControl :

foreach(RepeaterItem item in repRMAproduct.Items)
{
    TextBox tbNewQty = (TextBox) item.FindControl("tbNewQty");
    string newQuantity = tbNewQty.Text.Trim();
    int quantity;
    if(int.TryParse(newQuantity, out quantity))
    {
        SaveNewRmaQuantity(product, quantity);
    };
}

If you'd use a Label for your product-description you can use FindControl("LblDescriptionId") to get it and to pass it's Text to SaveNewRmaQuantity . You can also use invisible controls( Visible=false) , fe a Label , to store the ProductId .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM