itemCommand 事件未在具有自定义 ITemplate 的 ListView 中触发

发布于 2024-07-14 13:52:01 字数 1475 浏览 6 评论 0原文

我已经为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-07-21 13:52:01

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

这是我所拥有的:

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

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:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文