DataList OnItemCommand is not triggered

S

Svein Terje Gaup

Hi,

I am trying to use a LinkButton inside a DataList control in ASP.NET
2.0. The problem is that the eventhandler for the "OnItemCommand" event
is never run.

I am using the DataList control inside a Web User Control. My
Control-directive says AutoEventWireup="true":
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="EstimateList.ascx.cs"
Inherits="GKEstimat.Units.EstimateList" %>

This is my control:
<asp:DataList ID="dlEstimates" runat="server"
OnItemCommand="dlEstimates_OnItemCommand" DataKeyField="ProsjektID"

ItemStyle-CssClass="listRow"
AlternatingItemStyle-CssClass="listRowAlt" >
<ItemTemplate>
<tr>
<td><asp:LinkButton CommandName="Edit" runat="server"
Text="Edit" /></td>
<td class="list"><%#DataBinder.Eval(Container.DataItem,
"ProsjektNr")%></td>
<td><%#DataBinder.Eval(Container.DataItem,
"ProsjektNavn")%></td>
<td class="list"><%#DataBinder.Eval(Container.DataItem,
"Prosjektleder")%></td>
<td><%#DataBinder.Eval(Container.DataItem,
"ProsjektlederNavn")%></td>
<td><%#DataBinder.Eval(Container.DataItem,
"StatusKode")%></td></tr>
</ItemTemplate>
</asp:DataList>

Here's the code:

using System;
//several "using" statements here removed from post to newsgroup

namespace GKEstimat.Units
{
public partial class EstimateList : System.Web.UI.UserControl
{
protected const string PRETEXT = "NO-GK-";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}

protected void dlEstimates_OnItemCommand(object sender,
DataListCommandEventArgs e)
{
Trace.Write("EVENTHANDLER FOR OnItemCommand WAS HIT!");
}

protected void btnFilterByProsjektleder_Click(object sender,
EventArgs e)
{
FillList(PRETEXT + txtFilterByProsjektleder.Text,
EntityType.Prosjektleder);
}

protected void btnFilterByAvdeling_Click(object sender,
EventArgs e)
{
FillList(txtFilterByAvdeling.Text, EntityType.Avdeling);
}

private void FillList(string SearchText, EntityType entityType)
{
try
{
DataTable dt = DbHelper.ListProsjekt(SearchText,
entityType);

dlEstimates.DataSource = dt;

dlEstimates.DataBind();

DataBind();
}
catch (Exception ex)
{
LogHelper.Write(ex.Message,
LogCategoryEnum.Configuration,System.Diagnostics.TraceEventType.Error);
bool rethrow = ExceptionPolicy.HandleException(ex,
ExceptionHelper.Policy);
if (rethrow)
throw;
}
}
}
}

On the page there are two buttons outside the DataList that triggers
databinding the list. As you can see, I've not put any code into the
eventhandler yet. I'm trying get the execution to stop on a breakpoint
inside the eventhandler, but so far with no success.

Hope someone can spot what I'm doing wrong, and why the OnItemCommand
event is never run.

Sincerely
Svein Terje Gaup
 
S

Svein Terje Gaup

Now I found my mistake.

I am using LoadControl to dynamically load user controls into an other
user control. In my code, I called DataBind() after LoadControl. As so
many other postings have said, DataBind cancels all events. The
solution was to put DataBind inside an if(IsPostBack){...}.

Sincerely
Svein Terje Gaup
 

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