Repeater ItemCommand not firing

M

Michael

I've seen several posts relating to this problem, and tried
EnableViewState="False", but it does not work for me. A twist in my
problem is that I am adding the templates for the repeater dynamically
(something I need to do for my application). The problem is that when
I click on the button in the repeater the ItemCommand event does not
seem to be fired. Can anyone tell me what I am doing wrong?

//////////////WebForm1.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Repeater id="Repeater1" runat="server" EnableViewState="False"
OnItemCommand="Repeater1_ItemCommand"></asp:Repeater>
</form>
</body>
</HTML>



///////////////////////////WebForm1.aspx.cs :
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{

/// <summary>
/// Summary description for CustomTemplate.
/// </summary>
private class CustomTemplate : ITemplate
{
ListItemType m_templateType;
DataTable m_DataTable;

public CustomTemplate(ListItemType type)
{
m_templateType = type;
}

public CustomTemplate(ListItemType type, DataTable data)
{
m_templateType = type;
m_DataTable = data;
}

public void InstantiateIn(System.Web.UI.Control container)
{
RepeaterItem item = (RepeaterItem) container;

Literal lc = new Literal();
Control ctrl = new Control();
TableRow tr;
DataTable dt;
DataRow row;
TableCell tc;
Button btn;

switch( m_templateType )
{
case ListItemType.Header:
StringBuilder sb = new StringBuilder();
sb.Append("<TABLE border=1><TR>");
sb.Append("</TR>");

//place holder for Edit Button
sb.Append("<TH>");
sb.Append("</TH>");

for(int x=0;x<m_DataTable.Columns.Count;x++)
{
sb.Append("<TH>");
sb.Append(m_DataTable.Columns[x].ColumnName);
sb.Append("</TH>");
}

lc.Text = sb.ToString();
ctrl = lc;

break;
case ListItemType.Item:
tr = new TableRow();

//Add the Edit Button
btn = new Button();
btn.Text = "Edit";
btn.ID = "btnEdit";
btn.CommandName = "Edit";
tc = new TableCell();
tc.Controls.Add(btn);
tr.Cells.Add(tc);

dt = m_DataTable;
row = dt.Rows[item.ItemIndex];
for(int x=0;x<dt.Columns.Count;x++)
{
tc = new TableCell();
tc.Text = row[dt.Columns[x].ColumnName].ToString();
tr.Cells.Add(tc);
}
tr.DataBinding += new EventHandler(TemplateDataBinding);
ctrl = tr;
break;
case ListItemType.AlternatingItem:
tr = new TableRow();
dt = m_DataTable;
row = dt.Rows[item.ItemIndex];

//Add the Edit Button
btn = new Button();
btn.Text = "Edit";
btn.ID = "btnEdit";
btn.CommandName = "Edit";
tc = new TableCell();
tc.Controls.Add(btn);
tr.Cells.Add(tc);

for(int x=0;x<dt.Columns.Count;x++)
{
tc = new TableCell();
tc.BackColor = Color.LightBlue;
tc.Text = row[dt.Columns[x].ColumnName].ToString();
tr.Cells.Add(tc);
}
ctrl = tr;
tr.DataBinding += new EventHandler(TemplateDataBinding);
break;
case ListItemType.EditItem:
tr = new TableRow();
dt = m_DataTable;
row = dt.Rows[item.ItemIndex];

//add the cell for the buttons
tc = new TableCell();
tr.Cells.Add(tc);

for(int x=0;x<dt.Columns.Count;x++)
{
tc = new TableCell();
tc.BackColor = Color.LightGray;
tc.Text = row[dt.Columns[x].ColumnName].ToString();
tr.Cells.Add(tc);
}
ctrl = tr;
break;
case ListItemType.Footer:
lc.Text = "</TABLE>";
ctrl = lc;
break;
}

container.Controls.Add(ctrl);
}

private void TemplateDataBinding(object sender, System.EventArgs e)
{
TableRow row = (TableRow) sender;
RepeaterItem container = (RepeaterItem) row.NamingContainer;
}
} //CustomTemplate




protected System.Web.UI.WebControls.Repeater Repeater1;
protected DataTable m_dataTable;

private void Page_Load(object sender, System.EventArgs e)
{
if(! this.IsPostBack)
{
m_dataTable = GetData();
ConfigureRepeater();
DataBind();
}
}

private void Page_Init()
{
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
Repeater1.EnableViewState = false;
base.OnInit(e);
}

private void ConfigureRepeater()
{
Repeater1.HeaderTemplate = new CustomTemplate(ListItemType.Header,
m_dataTable);
Repeater1.FooterTemplate = new CustomTemplate(ListItemType.Footer,
m_dataTable);
Repeater1.ItemTemplate = new CustomTemplate(ListItemType.Item,
m_dataTable);
Repeater1.AlternatingItemTemplate = new
CustomTemplate(ListItemType.AlternatingItem, m_dataTable);

Repeater1.DataSource = m_dataTable;
Repeater1.DataBind();
Repeater1.Visible = true;
Repeater1.EnableViewState = false;
}

protected DataTable GetData()
{
DataTable data = new DataTable("Sample");
DataColumn dc = new DataColumn("Txt");
data.Columns.Add(dc);

DataRow dr;

dr = data.NewRow();
dr[0] = "Hello";
data.Rows.Add(dr);

dr = data.NewRow();
dr[0] = "There";
data.Rows.Add(dr);

dr = data.NewRow();
dr[0] = "World";
data.Rows.Add(dr);


return data;
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Repeater1.ItemCommand += new
System.Web.UI.WebControls.RepeaterCommandEventHandler(this.Repeater1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public void Repeater1_ItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
int i=0;
i++; //dummy line to put break point on....
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top