DataList OnItemCommand is not triggered

Discussion in 'ASP .Net Web Controls' started by Svein Terje Gaup, Jul 12, 2006.

  1. 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
     
    Svein Terje Gaup, Jul 12, 2006
    #1
    1. Advertising

  2. 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:
    > <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
     
    Svein Terje Gaup, Jul 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick White
    Replies:
    1
    Views:
    1,611
    Natty Gur
    Jan 13, 2004
  2. DaWoE

    Datalist onItemCommand not firing

    DaWoE, Jun 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,603
    DaWoE
    Jun 25, 2004
  3. Nevyn Twyll
    Replies:
    8
    Views:
    1,916
  4. bdwgarth
    Replies:
    1
    Views:
    1,049
    =?Utf-8?B?dmluYXk=?=
    Jun 22, 2005
  5. OnItemCommand DataList

    , Nov 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    379
Loading...

Share This Page