In this post I will show a simple implementation on OnItemCommand of the repeater control.
This is suitable when you render items in a repeater and want to edit individual items
Consider this sample class
public class Products { public string ProductCode {get;set;} public string ProductName {get;set;} public decimal ProductPrice {get;set;} }
You can display objects from this class very easily using any database call or Linq
In your repeater, you have a button to ‘Edit’ next to each product, which will allow admin users to change prices
So, the repeater might looks like this
<asp:Repeater ID="RepeaterListOfProducts" runat="server" OnItemDataBound="RepeaterListOfProducts_OnItemDataBound" OnItemCommand="RepeaterListOfProducts_OnItemCommand"> <HeaderTemplate><ul></HeaderTemplate> <ItemTemplate> <li> <label> <asp:Literal ID="LiteralProdcutCode" runat="server"></asp:Literal> <asp:Literal ID="LiteralProdcutName" runat="server"></asp:Literal> <asp:Literal ID="LiteralProductPrice" runat="server" ></asp:Literal> </label> <asp:Button ID="ButtonEditProduct" runat="server" Text="Edit" CommandName="Edit" /> </li> </ItemTemplate> <FooterTemplate></ul></FooterTemplate> </asp:Repeater>
In code behind, you have a function to populate products something like
RepeaterListOfProducts.DataSource = db.GetAllProducts(); RepeaterListOfProducts.DataBind();
In OnItemDataBound, I will assign ProductCode (assuming that is primary key/unique value per product) to the CommandArgument
for the button, so each button will have different argument when it is clicked
Sample code may look like this
protected void RepeaterListOfProducts_OnItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Product product = (Product)e.Item.DataItem; Literal productCode = (Literal)e.Item.FindControl("LiteralProdcutCode"); productCode.Text = product.ProdcutCode; Literal productName = (Literal)e.Item.FindControl("LiteralProdcutName"); productName.Text = product.ProdcutName; Literal productPrice = (Literal)e.Item.FindControl("LiteralProductPrice"); productPrice.Text = product.ProductPrice; Button update = (Button)e.Item.FindControl("ButtonEditProduct"); update.CommandArgument = product.ProductCode; } }
Now comes the interesting part, in you OnItemCommand, I will take the command button argument and setup a predefined text box (in a differnt panel) with the value
and let the user update the price in a panel
protected void RepeaterListOfUsers_OnItemCommand(object sender, RepeaterCommandEventArgs e) { if (e.CommandName == "Edit") { LiteralUpdateProductCode.Text = e.CommandArgument.ToString(); TextBoxUpdatePrice.Text = ((Literal)e.Item.FindControl("LiteralProductPrice")).Text; //add your custom logic here } }
The above codes takes the price and code from the repeater items and allows user to update with your custom logic.
In the panel, I could also have another button, which will perform my custom logic.
This code can be extended to include ‘Delete’ operation as well.
Thanks