用DataList 本身的onitemcommand事件。
代码实例:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("ID", typeof(int)));
data.Columns.Add(new DataColumn("Description", typeof(string)));
DataRow row = data.NewRow();
row.ItemArray = new object[] { 2, "Description2" };
data.Rows.Add(row);
DataRow row2 = data.NewRow();
row2.ItemArray = new object[] { 1, "Description1" };
data.Rows.Add(row2);
this.DataList1.DataSource = data;
this.DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
DataList1_ItemCommand(object source, DataListCommandEventArgs e)
把单击事件里面的代买写在上面这个方法里面,即可
关键是淡季事件取值的顺序搞清楚就行了
ImageButton img = (ImageButton)DataList1.SelectedItem.FindControl("ImageButton1");
改为:ImageButton img=(ImageButton)sender;
ImageButton img = sender as ImageButton;
if(img != null)
{
string state = img.CommandArgument;
//装在Session里面
Session["state"] = state;
}
看看这样