By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Dynamic Template - linking Button events

P: n/a
I'm using a repeater with a dynamic template. I don't know the fields to display (or how many) until runtime. I have everything working except for linking Button events to the repeaters ItemCommand (see below).

I've found plenty of examples for doing it using <ItemTemplate> in the aspx file, but I'm stumped when it comes to doing it dynamically at run time.

///////////////////////////
.apsx file:
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="fges.admin.test" %>
<html>
<head></head>
<body>
<h2>Test</h2>
<form id="Form1" method="post" runat="server">
<asp:Repeater id="List2" runat="server"/>
</form>
</body
</html>

///////////////////////////
.cs file:
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 = new XTemplate(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<tr class=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

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 += new System.Web.UI.WebControls.RepeaterCommandEventHand ler(this.DispatchItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
}
}

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>fPBc7YtwpEegPxKJJxWuTg==</Id>
Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.