itemCommand 事件未在具有自定义 ITemplate 的 ListView 中触发
我已经为 listView 控件创建了一个自定义 itemtemplate,但通过 ITemplate 生成的按钮不会触发 item 命令。 不仅如此,当您单击任何按钮时,这些项目都会消失。 以下是我正在使用的代码,有问题。
的代码
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();
I have created a custom itemtemplate for my listView control, but the item command does not fire for the buttons generated through the ITemplate. NOt only this, the items disappear when you click on any button. Following is the code i am using, is something wrong with it.
The code for ITemplate
public class 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;
}
}
and the databind:
ListView1.ItemTemplate = new FirstItemTemplate();
ListView1.DataSource = p.SelectPaper();
ListView1.DataBind();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的麻烦,只是我的情况略有不同。 我使用数据网格通过一系列查询来提供深入分析。 在第一次向下钻取(链接单击)到第二个报告时,它工作正常,但在第二个到第三个报告中,它只是刷新页面而不触发单击事件,但在页面刷新后,它可以工作,这意味着用户必须单击两次才能获取第三份报告。
这是我所拥有的:
I'm having the same trouble except I'm in a slightly different scenario. I'm using a datagrid to provide a drilldown using a series of queries. On the first drilldown (link click) to the second report it works fine, but on the 2nd to 3rd report it's just refreshing the page and not firing the click event, but after the page refreshes it works which means users have to click twice to get to the 3rd report.
Here's what I have: