Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Repeater ItemCommand not firing

Reply
Thread Tools

Repeater ItemCommand not firing

 
 
Michael
Guest
Posts: n/a
 
      11-15-2004
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....
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ItemCommand event not firing from a dynamic user control,feeling puzzled EvelynAnd Ethan ASP .Net 4 01-09-2006 03:12 AM
Repeater.Itemcommand not firing ilovreko@gmail.com ASP .Net Web Controls 5 12-20-2005 07:37 AM
DataGrid ItemCommand event not firing if not first page =?Utf-8?B?ZGFuYw==?= ASP .Net 3 10-26-2005 05:24 PM
repeater ItemCommand not firing on first click =?Utf-8?B?Q3VydF9DIFtNVlBd?= ASP .Net 2 10-12-2005 03:18 PM
Datagrid ItemCommand Event Not Firing =?Utf-8?B?RGVlcGVzaA==?= ASP .Net 2 10-06-2005 12:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57