itemCommand event not firing in ListView with custom ITemplate

2.2k Views Asked by At

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();

1

There are 1 best solutions below

1
On

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