asp .net repeater itemcommand command name common repeater control item operations

This is only one small snippet illustrating some functionality. I will add to this blog from my previous usage with Repeater control over time or per request.

In many deployments, I prefer to use Repeater or ListView rather than Grids. The code pattern is the same, but the output is slightly different as well as some of the inherent features/functionality.

Notice the example below simulates table output similar to a Grid, or it may also be used for non-table type of output. Repeater is therefore IMO more adaptable to different needs, and allows more re-usability code patterns across projects.

page side:

<asp:Repeater ID="rptRobotsInFactories" runat="server" OnItemCommand="rptRobotsInFactories_ItemCommand">
<td>factory name</td>
<td>robot name</td>
<td>factory location</td>
<td>manufacture date</td>
<td colspan="2">commands</td>
<%# Eval("factoryname")%>
<asp:TextBox ID="tbRobotName" runat="server" Text='<%# Eval("robotname")%>'></asp:TextBox>
<%# Eval("factorylocation")%>
<%# Eval("manufacturedate")%>
<asp:LinkButton ID="lbtnRename" runat="server"> CommandName="rename" CommandArgument='<%# Eval("robotname")%>'>rename</asp:LinkButton>
<asp:LinkButton ID="lbtnDeelte" runat="server"> CommandName="delete" CommandArgument="">delete</asp:LinkButton>
<!-- insert pager here if you like-->

code behind:

//you can also intercept item databind as i've listed in another previous article on nested repeaters

 protected void rptRobotsInFactories_ItemCommand(object sender, RepeaterCommandEventArgs e)
        if (e.CommandName == "rename")
            string newname = ((TextBox)rptRobotsInFactories.Items[e.Item.ItemIndex].FindControl("tbFileName")).Text;
            //string newname = ((TextBox)e.Item.FindControl("tbFileName")).Text; //equal to the above
            //string index = (string)e.CommandArgument; //useful to pass in index values also. can split the values passed in or pass in class obj
            //var rif = ((List<robotinfactory>)rptRobotsInFactories.DataSource)[e.Item.ItemIndex];
//var rif = Session_RobotsInFactories().Select(r=>; //compare to using viewstate/page repeater above
            string oldname = (string)e.CommandArgument;
            if ((newname.ToLower().Trim().Replace(" ","") != "") && (newname!=oldname))
else if (e.CommandName=="delete") {
            robotinfactory rif = ((List<robotinfactory>)rptRobotsInFactories.DataSource)[e.Item.ItemIndex];

//added below funcs for clarity
public class robotinfactory() {
public string factoryname {get;set;}
public string robotname {get;set;}
public string factorylocation {get;set;}
public string manufacturedate {get;set;}

protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
//these would normally load from your LINQ dbml SP, view, select statement, stored procedure etc
var rifs = new List<robotinfactory>();

rifs.Add(new rif {

rifs.Add(new rif {
factorylocation="not florida",

rptRobotsInFactories.DataSource = rifs;

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on July 1, 2013, in Programming & Development and tagged , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: