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"> <HeaderTemplate> <table> <tr> <td>factory name</td> <td>robot name</td> <td>factory location</td> <td>manufacture date</td> <td colspan="2">commands</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Eval("factoryname")%> <asp:TextBox ID="tbRobotName" runat="server" Text='<%# Eval("robotname")%>'></asp:TextBox> </td> <td> <%# Eval("factorylocation")%> </td> <td> <%# Eval("manufacturedate")%> </td> <td> <asp:LinkButton ID="lbtnRename" runat="server"> CommandName="rename" CommandArgument='<%# Eval("robotname")%>'>rename</asp:LinkButton> </td> <td> <asp:LinkButton ID="lbtnDeelte" runat="server"> CommandName="delete" CommandArgument="">delete</asp:LinkButton> </td> </tr> </ItemTemplate> </HeaderTemplate> <FooterTemplate> <!-- insert pager here if you like--> </table> </FooterTemplate> </asp:Repeater>
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=>r.id==Convert.ToInt32(e.CommandArgument)); //compare to using viewstate/page repeater above string oldname = (string)e.CommandArgument; if ((newname.ToLower().Trim().Replace(" ","") != "") && (newname!=oldname)) { //dowork_rename(oldname,newname); } } else if (e.CommandName=="delete") { robotinfactory rif = ((List<robotinfactory>)rptRobotsInFactories.DataSource)[e.Item.ItemIndex]; //dowork_delete(rif.id); } } //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 { factoryname="a", robotname="bob", factorylocation="florida", manufacturedate="7/1/2013" }); rifs.Add(new rif { factoryname="b", robotname="sam", factorylocation="not florida", manufacturedate="7/4/2013" }); rptRobotsInFactories.DataSource = rifs; rptRobotsInFactories.DataBind(); } }
Posted on July 1, 2013, in Programming & Development and tagged .net, asp, c#, command, command name, commandname, item, itemcommand, repeater. Bookmark the permalink. Leave a comment.
Leave a comment
Comments 0