我已经为我的 listView 控件创建了一个自定义项模板,但项命令不会为通过 ITemplate 生成的按钮触发。不仅如此,当您单击任何按钮时,这些项目都会消失。以下是我正在使用的代码,它有问题。
ITemplate 的代码
公共类 FirstItemTemplate : ITemplate {
public void InstantiateIn(System.Web.UI.Control container)
{
var oTR = new HtmlGenericControl("tr");
var oTD1 = new HtmlGenericControl("td");
Button btnEnter = new Button();
btnEnter.ID = "btnEnter";
oTD1.Controls.Add(btnEnter);
oTR.Controls.Add(oTD1);
var oTD2 = new HtmlGenericControl("td");
Label lblProduct = new Label();
lblProduct.ID = "lblProduct";
oTD2.Controls.Add(lblProduct);
oTR.Controls.Add(oTD2);
oTR.DataBinding += new EventHandler(oTR_DataBinding);
container.Controls.Add(oTR);
}
void oTR_DataBinding(object sender, EventArgs e)
{
var container = (HtmlGenericControl)sender;
var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem;
PaperObject pro = (PaperObject)dataItem;
Button btnEnter = (Button)container.FindControl("btnEnter");
Label lblProduct = (Label)container.FindControl("lblProduct");
btnEnter.Text = pro.PaperId.ToString();
btnEnter.CommandName = "Select";
btnEnter.CommandArgument = pro.PaperId.ToString();
lblProduct.Text = pro.Description;
}
}
和数据绑定:
ListView1.ItemTemplate = new FirstItemTemplate(); ListView1.DataSource = p.SelectPaper(); ListView1.DataBind();