使用DataList1_ItemDataBound的事件丢失问题
{
if (!IsPostBack)
{
dataBind();
}
}
private void dataBind()
{
FactorData factordata = (new FactorSystem()).FactorSecondListSystem(6, 101, 1, 20);
DataList1.DataSource = factordata;
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dv = (DataRowView)e.Item.DataItem;
LinkButton LinkButton1 = (LinkButton)e.Item.FindControl("LinkButton1");
LinkButton1.Text = dv.Row["description"].ToString();
LinkButton1.CommandArgument = dv.Row["factorID"].ToString();
LinkButton1.Click += new EventHandler(LinkButton1_Click);
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton LinkButton1 = (LinkButton)sender;
Response.Write(LinkButton1.CommandArgument);
}
如果是这样在ItemDataBound事件中给LinkButton按钮设定Click时间,则事件回发的时候给DataList数据绑定的dataBind()一定不要判断IsPostBack,否则事件会丢失。解决的问题方法是
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("factorID")%>'
onclick="LinkButton1_Click1"><%# Eval("description")%></asp:LinkButton>
直接在aspx页面绑定就可以了。