在datalist控件的模版中加入一个button,怎么通过单击botton获取所选项各属性的值。 希望最好有案例代码!

非常感谢!!!急!!
2024-11-25 22:42:42
推荐回答(2个)
回答1:

对于DataList,通常使用ItemCommand事件,请参考

.aspx

onitemcommand="DataList1_ItemCommand">

'>





.aspx.cs

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
BindData();
}
}

private void BindData()
{
List list = new List();
list.Add(new User { Id = 1, Name = "aa" });
list.Add(new User { Id = 2, Name = "bb" });
list.Add(new User { Id = 3, Name = "cc" });
list.Add(new User { Id = 4, Name = "dd" });
DataList1.DataSource = list;
DataList1.DataKeyField = "Id";
DataList1.DataBind();
}

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
lblMsg.Text = (e.Item.FindControl("Label1") as Label).Text;
}
BindData();
}
}

public class User
{
public int Id { get; set; }
public string Name { get; set; }
}

回答2:

在他的“编辑模板”中点击 然后在里面添加按钮