Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > DataList OnItemCommand is not triggered

Reply
Thread Tools

DataList OnItemCommand is not triggered

 
 
Svein Terje Gaup
Guest
Posts: n/a
 
      07-12-2006
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:
<aspataList 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>
</aspataList>

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.T raceEventType.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

 
Reply With Quote
 
 
 
 
Svein Terje Gaup
Guest
Posts: n/a
 
      07-12-2006
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

Svein Terje Gaup wrote:
> 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:
> <aspataList 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>
> </aspataList>
>
> 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.T raceEventType.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


 
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
OnItemCommand DataList rn5a@rediffmail.com ASP .Net 0 11-22-2006 08:46 PM
OnItemCommand in DataList, how to manipulate controls in Selected Row bdwgarth ASP .Net 1 06-22-2005 07:03 PM
Setting up a datalist control - Item_DataBound for a datalist in a datalist Nevyn Twyll ASP .Net 8 09-09-2004 10:13 PM
Datalist onItemCommand not firing DaWoE ASP .Net 1 06-25-2004 01:41 PM
datagrid OnItemCommand handler not executing Rick White ASP .Net 1 01-13-2004 05:41 AM



Advertisments