P
Pat Sagaser via .NET 247
I'm trying to add LinkButtons to a Repeater control using adynamic template. The docs state that you should be able tobubble the click event to the containing Repeater. There areplenty of examples in the documentation for doing this using an<ItemTemplate> tag, but I haven't found any indication for howyou would do this in a dynamic template (implementing theITemplate interface).
I'm adding the LInkButton in the TemplateDataBinding procedure(see below). I can't get the
Note that I have various reasons for not using a DataGrid orDataList control.
///////////////////////////
.cs file extract:
using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace fges.admin
{
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater List2;
private ArrayList fields = new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
fields.Add("Field1"); //sample fields
fields.Add("Field2");
fields.Add("Field3");
List2.HeaderTemplate = new XTemplate(ListItemType.Header,fields);
List2.ItemTemplate = new XTemplate(ListItemType.Item,entity.Fields);
List2.AlternatingItemTemplate = newXTemplate(ListItemType.AlternatingItem, entity.Fields);
List2.FooterTemplate = new XTemplate(ListItemType.Footer,fields);
List2.EnableViewState = true;
//Load data correspondig to the fields array
//List2.DataSource = entity.GetAllRecords();
List2.DataBind();
}
}
protected void DispatchItemCommand(Object sender,RepeaterCommandEventArgs e)
{
switch(((LinkButton)e.CommandSource).CommandName)
{
case "Update":
//DoItemUpdate(e);
break;
}
}
private class XTemplate : ITemplate
{
private ListItemType templateType;
private ArrayList fields;
public XTemplate(ListItemType type, ArrayList Fields)
{
templateType = type;
fields = Fields;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
HtmlTableRow row = new HtmlTableRow();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "<table class=adminTable>\n<trclass=adminTableHeader>";
foreach (string f in fields)
{
lc.Text += "<th>" + f + "</th>";
}
//Add action column
lc.Text += "<th class=action>Action</th></tr>\n";
container.Controls.Add(lc);
break;
case ListItemType.Item:
row.DataBinding += new EventHandler(TemplateDataBinding);
container.Controls.Add(row);
break;
case ListItemType.AlternatingItem:
row.Attributes.Add("class", "adminAltRecord");
row.DataBinding += new EventHandler(TemplateDataBinding);
container.Controls.Add(row);
break;
case ListItemType.Footer:
container.Controls.Add(new LiteralControl("</table>\n"));
break;
}
}
private void TemplateDataBinding(object sender,System.EventArgs e)
{
HtmlTableRow row = (HtmlTableRow) sender;
RepeaterItem container = (RepeaterItem) row.NamingContainer;
string id = ((DataRowView)container.DataItem)["ID"].ToString();
LinkButton update = new LinkButton();
update.CommandName = "UpdateItem";
update.Text = "Update";
update.ID = id;
//Questions is how to get this button to
//invoke the List2.ItemCommand event
//I've also tried update.Command += newCommandEventHandler(SomeHandler);
//but it doesn't work (SomeHandler never gets called)
foreach (string f in fields)
{
HtmlTableCell cell = new HtmlTableCell();
cell.InnerHtml = ((DataRowView)container.DataItem)[f].ToString();
row.Cells.Add(cell);
}
HtmlTableCell cell2 = new HtmlTableCell();
cell2.Controls.Add(update);
row.Cells.Add(cell2);
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.List2.ItemCommand += newSystem.Web.UI.WebControls.RepeaterCommandEventHandler(this.DispatchItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
I'm adding the LInkButton in the TemplateDataBinding procedure(see below). I can't get the
Note that I have various reasons for not using a DataGrid orDataList control.
///////////////////////////
.cs file extract:
using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace fges.admin
{
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater List2;
private ArrayList fields = new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
fields.Add("Field1"); //sample fields
fields.Add("Field2");
fields.Add("Field3");
List2.HeaderTemplate = new XTemplate(ListItemType.Header,fields);
List2.ItemTemplate = new XTemplate(ListItemType.Item,entity.Fields);
List2.AlternatingItemTemplate = newXTemplate(ListItemType.AlternatingItem, entity.Fields);
List2.FooterTemplate = new XTemplate(ListItemType.Footer,fields);
List2.EnableViewState = true;
//Load data correspondig to the fields array
//List2.DataSource = entity.GetAllRecords();
List2.DataBind();
}
}
protected void DispatchItemCommand(Object sender,RepeaterCommandEventArgs e)
{
switch(((LinkButton)e.CommandSource).CommandName)
{
case "Update":
//DoItemUpdate(e);
break;
}
}
private class XTemplate : ITemplate
{
private ListItemType templateType;
private ArrayList fields;
public XTemplate(ListItemType type, ArrayList Fields)
{
templateType = type;
fields = Fields;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
HtmlTableRow row = new HtmlTableRow();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "<table class=adminTable>\n<trclass=adminTableHeader>";
foreach (string f in fields)
{
lc.Text += "<th>" + f + "</th>";
}
//Add action column
lc.Text += "<th class=action>Action</th></tr>\n";
container.Controls.Add(lc);
break;
case ListItemType.Item:
row.DataBinding += new EventHandler(TemplateDataBinding);
container.Controls.Add(row);
break;
case ListItemType.AlternatingItem:
row.Attributes.Add("class", "adminAltRecord");
row.DataBinding += new EventHandler(TemplateDataBinding);
container.Controls.Add(row);
break;
case ListItemType.Footer:
container.Controls.Add(new LiteralControl("</table>\n"));
break;
}
}
private void TemplateDataBinding(object sender,System.EventArgs e)
{
HtmlTableRow row = (HtmlTableRow) sender;
RepeaterItem container = (RepeaterItem) row.NamingContainer;
string id = ((DataRowView)container.DataItem)["ID"].ToString();
LinkButton update = new LinkButton();
update.CommandName = "UpdateItem";
update.Text = "Update";
update.ID = id;
//Questions is how to get this button to
//invoke the List2.ItemCommand event
//I've also tried update.Command += newCommandEventHandler(SomeHandler);
//but it doesn't work (SomeHandler never gets called)
foreach (string f in fields)
{
HtmlTableCell cell = new HtmlTableCell();
cell.InnerHtml = ((DataRowView)container.DataItem)[f].ToString();
row.Cells.Add(cell);
}
HtmlTableCell cell2 = new HtmlTableCell();
cell2.Controls.Add(update);
row.Cells.Add(cell2);
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.List2.ItemCommand += newSystem.Web.UI.WebControls.RepeaterCommandEventHandler(this.DispatchItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
}
}