【问题标题】:itemCommand event not firing in ListView with custom ITemplateitemCommand 事件未在使用自定义 ITemplate 的 ListView 中触发
【发布时间】:2009-03-13 15:46:50
【问题描述】:

我已经为我的 listView 控件创建了一个自定义 itemtemplate,但是 item 命令不会为通过 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();

【问题讨论】:

    标签: .net itemplate


    【解决方案1】:

    我遇到了同样的麻烦,只是我的情况略有不同。我正在使用数据网格来提供使用一系列查询的向下钻取。在第一个钻取(链接点击)到第二个报告时它工作正常,但在第二到第三个报告中它只是刷新页面而不触发点击事件,但在页面刷新后它可以工作,这意味着用户必须点击两次才能获得第三份报告。

    这是我所拥有的:

    Private Class DrilldownTemplate : Implements ITemplate
        Private _fieldName As String
    
        Sub New(ByVal fieldName As String)
            _fieldName = fieldName
        End Sub
    
        Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
            Dim linkbtn As LinkButton = New LinkButton
            AddHandler linkbtn.DataBinding, AddressOf BindHyperLinkColumn
            container.Controls.Add(linkbtn)
        End Sub
    
        Public Sub BindHyperLinkColumn(ByVal sender As Object, ByVal e As EventArgs)
            Dim linkbtn As LinkButton = CType(sender, LinkButton)
            Dim container As DataGridItem = CType(linkbtn.NamingContainer, DataGridItem)
    
            With linkbtn
                .CommandName = "Drilldown|" & _fieldName
                .CommandArgument = Convert.ToString(DataBinder.Eval((CType(container, DataGridItem)).DataItem, _fieldName))
                .Text = Convert.ToString(DataBinder.Eval((CType(container, DataGridItem)).DataItem, _fieldName))
            End With
        End Sub
    End Class
    

    【讨论】:

    • 只是想发布我的问题的解决方案。在将它添加到容器之前,我为 linkbtn 添加了一个 id,即linkbtn.ID = "lbDrilldown",以及作为数据网格父级的动态添加的 div。确保检查要动态添加控件的任何位置并确保为它们分配 ID,即使您不打算处理来自它们的事件或尝试在事件中定位它们等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多