473,508 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Repeater ItemCommand not firing

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.RepeaterCommandEventHand ler(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....
}
}
}
Nov 18 '05 #1
0 6112

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9791
by: Mark Fox | last post by:
Hello, I have a repeater and in each itemtemplate I have a radiobuttonlist. I am attempting to figure out how on postback I could iterate through the rows displayed by the repeater and for...
2
3179
by: Jorge Ayala | last post by:
Well I'm trying to catch and act upon a button event that is placed within the item template of a repeater control. Yet the code I'm using isn't working. What I've seen out there to explain how...
2
11989
by: Stan | last post by:
I cannot make the link buttons fire ItemCommand from repeater control. Here is the code: <asp:repeater id=rptLetters runat="server"> <itemtemplate> <asp:linkbutton id="lnkLetter"...
0
1396
by: Sparhawk | last post by:
I got a weird problem with a repeater. The Repeater has an ImageButton in its ItemTemplate. The Repeater has an ItemCommand defined. If the user clicks one of the buttons, nothing happens. If...
0
3955
by: Patrik Persson | last post by:
Hello all I have got a problem with a nested repeater and the ItemCommand Event. I am adding handler for the ItemCommand and ItemBound. The ItemBound works perfect but I cant seem to get the...
3
5661
by: Leigh Webber | last post by:
I have an HTMLAnchor control on my aspx page. When it's not inside a repeater, it works fine. When I put it inside a repeater control, the handler never gets fired. I have a handler for the...
1
2929
by: Rob Rutherford | last post by:
I have a Repeater on an ASP.NET page. If I click a Button in the Repeater, the Repeater's ItemCommand event fails to fire. If anyone can explain why it doesn't fire I'd be grateful. Here's the...
2
4859
by: Curt_C [MVP] | last post by:
I've got a Repeater and within it a LinkButton. The LinkButton has an CommandName="Test" In the Repeater's ItemCommand event I want to check for this command name but the problem I'm having is...
2
1979
by: =?Utf-8?B?Um9nZXIweDE=?= | last post by:
Language: c#,.net 2 This looks simple in concept: I have a page that needs to display a userid, a date/time to start vacation, a date/time to end the vacation, and two buttons: one says...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.