asp.net怎么从DataList中取一个值?

2024-12-17 01:42:27
推荐回答(4个)
回答1:

用DataList 本身的onitemcommand事件。
代码实例:


' CommandName="Edit" runat="server" />




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);
}

回答2:

DataList1_ItemCommand(object source, DataListCommandEventArgs e)
把单击事件里面的代买写在上面这个方法里面,即可
关键是淡季事件取值的顺序搞清楚就行了

回答3:

ImageButton img = (ImageButton)DataList1.SelectedItem.FindControl("ImageButton1");
改为:ImageButton img=(ImageButton)sender;

回答4:

ImageButton img = sender as ImageButton;
if(img != null)
{
string state = img.CommandArgument;

//装在Session里面
Session["state"] = state;
}

看看这样