You can declare the button using the property CommandName
and then use the ItemCommand
event controller.
Source: http://msdn.microsoft.com/es-es/library/es4e4e0e(v=vs.100).aspx
Here, a working example (Note that I'm using an UpdatePanel
. Due the content of the page is going to be change, if you do not use it you will get a server error. More info here):
Test.aspx
<asp:UpdatePanel ID="upDataList1" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lUID" runat="server" Text='<%# Eval("UID") %>' />
</td>
<td>
<asp:TextBox ID="tbName" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:TextBox ID="tbDescription" runat="server" Text='<%# Eval("description") %>' />
</td>
<td>
<asp:Button ID="bModify" runat="server" Text="Modify" CommandName="Modify" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
Test.aspx.cs
public partial class Test : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
var x = db.sp_GetAllProducts();
DataList1.DataSource = x;
DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(Object sender, DataListCommandEventArgs e) {
String a = ((TextBox) e.Item.FindControl("tbName")).Text;
String b = ((TextBox) e.Item.FindControl("tbDescription")).Text;
((Label) e.Item.FindControl("lUID")).Text = a + " " + b;
}
}
public class db {
public String UID { get; set; }
public String name { get; set; }
public String description { get; set; }
public db(String UID, String name, String description) {
this.UID = UID;
this.name = name;
this.description = description;
}
public static List<db> sp_GetAllProducts() {
List<db> list = new List<db>();
list.Add(new db("1", "1a", "1b"));
list.Add(new db("2", "2a", "2b"));
list.Add(new db("3", "3a", "3b"));
list.Add(new db("4", "4a", "4b"));
list.Add(new db("5", "5a", "5b"));
list.Add(new db("6", "6a", "6b"));
return list;
}
}