Problem with OnItemCommand event and Loadtemplate

G

Guest

Hi all,
in my previous post I've wrong typed some tems.. this is the corrected post.

in a aspx page I have a repeater like this:
<asp:repeater id=repeaterResults runat="server"
OnItemCommand="RepeaterResult_Event">
<HeaderTemplate>
<table width="515" border="0" cellpadding="3" cellspacing="0">
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
<SeparatorTemplate>
<tr><td colspan="4"><hr></td></tr>
</SeparatorTemplate>
</asp:repeater>

in the code behind file I dinamically load the Itemtemplate with this code:

repeaterResults.DataSource = objPds;
string templateName = "templates/BSResultTextTemplate.ascx";
repeaterResults.ItemTemplate = Page.LoadTemplate(templateName);
repeaterResults.DataBind();

Always in the same code behind file, I've defined the Method specified in
the OnItemCommand property of repeater:

public void RepeaterResult_Event(object sender, RepeaterCommandEventArgs e)
{
string s = " SSS ";
}

below you can see the content of BSResultTextTemplate.ascx :
<tr>
<td class="nerotesti" width="155"><asp:LinkButton id="CmdVisualizzaReperto"
runat="server" CommandName="ItemCommand" CommandArgument=<%#
DataBinder.Eval(Container,"DataItem.ID") %> ><%#
DataBinder.Eval(Container,"DataItem.ID") %></asp:LinkButton></td>
<td class="nerotesti" width="160"><%#
DataBinder.Eval(Container,"DataItem.Culture") %></td>
<td class="nerotesti" width="160"><%#
DataBinder.Eval(Container,"DataItem.Subject") %></td>
<td class="nerotesti" width="60"> </td>
</tr>

Now the problem.... the template is loaded successfully but when I click on
one of the linkbutton generated, the method RepeaterResult_Event is not
fired...
on the other side if I insert the code of template directly in the repeater
definitition and don't load an external template when I click on one of the
linkbutton generated all work fine and the method RepeaterResult_Event is
invoked.

Please have anyone a solution for this?

Thank you.
Antonio.
 
G

Guest

Thank you Vadivel for your answer but it didn't worked,
as you told me I've inserted this line of code inside InitializeComponent:
this.repeaterResults.ItemCommand += new
System.Web.UI.WebControls.RepeaterCommandEventHandler(this.RepeaterResult_Event);

and I've removed the OnItemCommand property from the aspx file

where is my mistake?

Thank you.
Antonio.
 
G

Guest

I didn't receive any error, simply the event is not fired... now i've worked
to create a new page with only the essential to reproduce the behaviour:

//****************BEGIN OF TEST.ASPX***************
<%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false"
Inherits="ArtPast.Test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</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=repeaterResults runat="server">
<HeaderTemplate>
<table width="515" border="0" cellpadding="3" cellspacing="0">
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
<SeparatorTemplate>
<tr>
<td colspan="4">
<hr>
</td>
</tr>
</SeparatorTemplate>
</asp:repeater>
<asp:Label id=Label1 style="Z-INDEX: 101; LEFT: 616px; POSITION: absolute;
TOP: 40px" runat="server" Width="144px">Label</asp:Label>
</form>

</body>
</HTML>
//****************END OF TEST.ASPX***************

//****************BEGIN OF TEST.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;

namespace ArtPast
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Repeater repeaterResults;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
LoadResults();
}
}

#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();
base.OnInit(e);
}

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

}
#endregion

private void LoadResults()
{
ArrayList lots = new ArrayList();
lots.Add(new Lot(1,"First"));
lots.Add(new Lot(2,"Second"));
lots.Add(new Lot(3,"Third"));
lots.Add(new Lot(4,"Fourth"));
PagedDataSource objPds = new PagedDataSource();
objPds.AllowPaging = true;
objPds.PageSize = 5;
objPds.DataSource = lots;
objPds.CurrentPageIndex = 0;

repeaterResults.DataSource = objPds;
string templateName = "";
templateName = "templates/BSResultTextTemplateTest.ascx";
repeaterResults.ItemTemplate = Page.LoadTemplate(templateName);
repeaterResults.DataBind();
}

private void repeaterResults_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
Label1.Text = "It worked!";
}

protected class Lot
{
private int iD;
private string description;
public Lot(int ID, string Description)
{
iD = ID;
description = Description;
}
public int ID
{
get {return iD;}
set {iD = value;}
}
public string Description
{
get {return description;}
set {description = value;}
}
}
}
}
//****************END OF TEST.ASPX.CS***************

//****************BEGIN OF bSResultTextTemplateTest.ascx***************
<tr>
<td width="155"><asp:LinkButton id="CmdVisualizzaReperto" runat="server"
CommandName="ItemCommand" CommandArgument=<%#
DataBinder.Eval(Container,"DataItem.ID") %> ><%#
DataBinder.Eval(Container,"DataItem.ID") %></asp:LinkButton></td>
<td width="160"><%# DataBinder.Eval(Container,"DataItem.ID") %></td>
<td width="160"><%# DataBinder.Eval(Container,"DataItem.Description") %></td>
<td width="60"> </td>

</tr>
//****************END OF BSResultTextTemplateTest.ascx***************
 
V

Vadivel Kumar

What i understood from your code is
your should not specify ItemCommand event in aspx file
it should be in your code-behind file.

Create a reference variable for your repeater (repeaterResults) in your cs
file, and go to InitializeComponent method and add ItemCommand event for
this control. It should work.

Do post if you've more problem.

Vadivel Kumar
http://blogs.wdevs.com/varahe
 
V

Vadivel Kumar

What is the error you're getting?
Can you post the code..
I think that is the lesser way to solve the problem :)
 
Joined
Apr 16, 2008
Messages
1
Reaction score
0
did you ever figure this out?

I'm having the same issue. The problem isn't with the reference in the aspx OnItemCommand or in the Init method in the code behind.

Example

foo.aspx
<asp:Repeater id=rptFoo runat=server />

itemTemplate.ascx
<ItemTemplate>
<asp:LinkButton id=lbFoo CommandName="click me" CommandArgument="1" Text="button click" />
</ItemTemplate>


If I were to take the contents of itemTemplate.ascx and place them on the repeater on foo.aspx, the events for the repeater fire as expected. The problem arises when I rip out the ItemTemplate and put it either in a custom class which inherits the ITemplate interface or in a user control.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top