DataList doesn't fire events

Discussion in 'ASP .Net Datagrid Control' started by Neil Jarman, Aug 17, 2005.

  1. Neil Jarman

    Neil Jarman Guest

    Hi,

    I'm new to this today, and I've got some test code (see below.) The data
    loads fine.

    I can't understand why any of the events fire. Once the page loads, clicking
    on thew button from the ItemTemplate does not fire the OnItemCommand event.
    It just fires a page reload - as a postback.

    I guess I'm missing something really obvious and important here?

    Any help greatfully received,

    Cheers,

    NEIL




    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"
    Debug="true" %>
    <%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"
    Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,c
    ulture=neutral" %>
    <MM:DataSet
    id="DataSet1"
    runat="Server"
    IsStoredProcedure="false"
    ConnectionString='<%#
    System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING
    _SBC_SQL") %>'
    DatabaseType='<%#
    System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABA
    SETYPE_SBC_SQL") %>'
    CommandText='<%# "SELECT iContactID, sPostCode, dDateCreated, sFamilyName,
    sEmailAddress FROM tblTivyContactList WHERE (sPostCode LIKE " + Chr(39) +
    "YO11%" + Chr(39) + ") ORDER BY dDateCreated DESC" %>'
    Debug="true"
    ><EditOps><EditOpsTable Name="" />

    <Parameter Name="iContactID" Type="VarChar" />
    <Parameter Name="sFamilyName" Type="VarChar" />
    <Parameter Name="sEmailAddress" Type="VarChar" />
    <Parameter Name="" Type="BigInt" IsPrimary="true" />
    </EditOps></MM:DataSet>
    <MM:pageBind runat="server" PostBackBind="true" />
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="Visitors/TivyInternal.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    Message: <asp:label id="lblMessage" runat="server" />
    <form runat="server">

    <asp:datalist ID="aspDL" runat="server"
    DataSource="<%# DataSet1.DefaultView %>"
    DataKeyField="iContactID"
    RepeatLayout="Flow"
    OnItemCommand="DoItemSelect"
    OnEditCommand="DoItemEdit"
    OnUpdateCommand="DoItemUpdate"
    OnDeleteCommand="DoItemDelete"
    OnCancelCommand="DoItemCancel"
    autogeneratecolumns="false">

    <HeaderTemplate>
    List of contacts in YO11
    </HeaderTemplate>

    <ItemTemplate>
    <asp:Button CommandName="Select" Text="Select" runat="server" />
    PostCode: <b><%# DataSet1.FieldValue("sPostCode", Container) %></b>
    </ItemTemplate>

    <SelectedItemTemplate>
    <asp:Button CommandName="Edit" Text="Edit" runat="server" />
    PostCode: <b><%# Container.DataItem("sPostCode") %></b><br />
    SurName: <%# DataSet1.FieldValue("sFamilyName", Container) %> &nbsp;
    Email: <%# DataSet1.FieldValue("sEmailAddress", Container) %><br />
    </SelectedItemTemplate>

    <EditItemTemplate>
    <asp:Button CommandName="Update" Text="Update" runat="server" />
    <asp:Button CommandName="Delete" Text="Delete" runat="server" />
    <asp:Button CommandName="Cancel" Text="Cancel" runat="server" />
    PostCode: <b><%# Container.DataItem("sPostCode") %></b><br />
    Surname: <asp:textbox id="txtSurname" Text="<%#
    DataSet1.FieldValue("sFamilyName", Container) %>"> &nbsp;
    Email: <asp:textbox id="txtEmail" Text="<%#
    DataSet1.FieldValue("sEmailAddress", Container) %>"><br />
    </EditItemTemplate>

    </asp:datalist>
    </form>
    <!--------------------------------------------------------------------------
    ->
    <script language="vb" runat="server">

    Sub Page_Load()
    if not page.ispostback then
    lblMessage.text = now()
    aspDL.DataBind()
    else
    'aspDL.selectedindex = 7 'test
    'aspDL.DataBind()
    end if
    End Sub

    Sub DoItemSelect(objSource As object, objArgs As DataListCommandEventArgs)
    lblMessage.text = "in DoItemInfo"
    if objArgs.CommandName = "Select" then
    lblMessage.text = "In DoItemInfo"
    aspDL.selectedindex = objArgs.item.itemindex
    aspDL.DataBind()
    end if
    End Sub

    sub DoItemEdit(objSource as object, objArgs as DataListCommandEventArgs)
    lblMessage.text = "in DoItemEdit"
    aspDL.selectedindex = -1
    aspDL.EditItemIndex = objArgs.item.itemindex
    aspDL.DataBind()
    end sub

    sub DoItemUpdate(objSource as object, objArgs as DataListCommandEventArgs)
    lblMessage.text = "in DoItemUpdate"
    end sub

    sub DoItemDelete(objSource as object, objArgs as DataListCommandEventArgs)
    lblMessage.text = "in DoItemDelete"
    end sub

    sub DoItemCancel(objSource as object, objArgs as DataListCommandEventArgs)
    lblMessage.text = "in DoItemCancel"
    end sub
    </script>

    <!--------------------------------------------------------------------------
    ->
    </body>
    </html>
     
    Neil Jarman, Aug 17, 2005
    #1
    1. Advertising

  2. Neil Jarman

    Teemu Keiski Guest

    Hi,

    have you made sure you don't databind the dataList on every postback in
    Page_Load? E.g do you have binding inside If Not Page.IsPostBack check (
    if(!Page.IsPostBack){...} in C# )

    If you rebind the DataList (or any databound control) accidentally in
    Page_Load on every request that will "clear" the events to be raised and
    cause them not to be raised.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "Neil Jarman" <> wrote in message
    news:ddvss2$pfq$1$...
    > Hi,
    >
    > I'm new to this today, and I've got some test code (see below.) The data
    > loads fine.
    >
    > I can't understand why any of the events fire. Once the page loads,

    clicking
    > on thew button from the ItemTemplate does not fire the OnItemCommand

    event.
    > It just fires a page reload - as a postback.
    >
    > I guess I'm missing something really obvious and important here?
    >
    > Any help greatfully received,
    >
    > Cheers,
    >
    > NEIL
    >
    >
    >
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <%@ Page Language="VB" ContentType="text/html"

    ResponseEncoding="iso-8859-1"
    > Debug="true" %>
    > <%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"
    >

    Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,c
    > ulture=neutral" %>
    > <MM:DataSet
    > id="DataSet1"
    > runat="Server"
    > IsStoredProcedure="false"
    > ConnectionString='<%#
    >

    System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING
    > _SBC_SQL") %>'
    > DatabaseType='<%#
    >

    System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABA
    > SETYPE_SBC_SQL") %>'
    > CommandText='<%# "SELECT iContactID, sPostCode, dDateCreated, sFamilyName,
    > sEmailAddress FROM tblTivyContactList WHERE (sPostCode LIKE " + Chr(39) +
    > "YO11%" + Chr(39) + ") ORDER BY dDateCreated DESC" %>'
    > Debug="true"
    > ><EditOps><EditOpsTable Name="" />

    > <Parameter Name="iContactID" Type="VarChar" />
    > <Parameter Name="sFamilyName" Type="VarChar" />
    > <Parameter Name="sEmailAddress" Type="VarChar" />
    > <Parameter Name="" Type="BigInt" IsPrimary="true" />
    > </EditOps></MM:DataSet>
    > <MM:pageBind runat="server" PostBackBind="true" />
    > <html xmlns="http://www.w3.org/1999/xhtml">
    >
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    > <link href="Visitors/TivyInternal.css" rel="stylesheet" type="text/css" />
    > </head>
    > <body>
    > Message: <asp:label id="lblMessage" runat="server" />
    > <form runat="server">
    >
    > <asp:datalist ID="aspDL" runat="server"
    > DataSource="<%# DataSet1.DefaultView %>"
    > DataKeyField="iContactID"
    > RepeatLayout="Flow"
    > OnItemCommand="DoItemSelect"
    > OnEditCommand="DoItemEdit"
    > OnUpdateCommand="DoItemUpdate"
    > OnDeleteCommand="DoItemDelete"
    > OnCancelCommand="DoItemCancel"
    > autogeneratecolumns="false">
    >
    > <HeaderTemplate>
    > List of contacts in YO11
    > </HeaderTemplate>
    >
    > <ItemTemplate>
    > <asp:Button CommandName="Select" Text="Select" runat="server" />
    > PostCode: <b><%# DataSet1.FieldValue("sPostCode", Container) %></b>
    > </ItemTemplate>
    >
    > <SelectedItemTemplate>
    > <asp:Button CommandName="Edit" Text="Edit" runat="server" />
    > PostCode: <b><%# Container.DataItem("sPostCode") %></b><br />
    > SurName: <%# DataSet1.FieldValue("sFamilyName", Container) %>

    &nbsp;
    > Email: <%# DataSet1.FieldValue("sEmailAddress", Container) %><br />
    > </SelectedItemTemplate>
    >
    > <EditItemTemplate>
    > <asp:Button CommandName="Update" Text="Update" runat="server" />
    > <asp:Button CommandName="Delete" Text="Delete" runat="server" />
    > <asp:Button CommandName="Cancel" Text="Cancel" runat="server" />
    > PostCode: <b><%# Container.DataItem("sPostCode") %></b><br />
    > Surname: <asp:textbox id="txtSurname" Text="<%#
    > DataSet1.FieldValue("sFamilyName", Container) %>"> &nbsp;
    > Email: <asp:textbox id="txtEmail" Text="<%#
    > DataSet1.FieldValue("sEmailAddress", Container) %>"><br />
    > </EditItemTemplate>
    >
    > </asp:datalist>
    > </form>
    >

    <!--------------------------------------------------------------------------
    > ->
    > <script language="vb" runat="server">
    >
    > Sub Page_Load()
    > if not page.ispostback then
    > lblMessage.text = now()
    > aspDL.DataBind()
    > else
    > 'aspDL.selectedindex = 7 'test
    > 'aspDL.DataBind()
    > end if
    > End Sub
    >
    > Sub DoItemSelect(objSource As object, objArgs As DataListCommandEventArgs)
    > lblMessage.text = "in DoItemInfo"
    > if objArgs.CommandName = "Select" then
    > lblMessage.text = "In DoItemInfo"
    > aspDL.selectedindex = objArgs.item.itemindex
    > aspDL.DataBind()
    > end if
    > End Sub
    >
    > sub DoItemEdit(objSource as object, objArgs as DataListCommandEventArgs)
    > lblMessage.text = "in DoItemEdit"
    > aspDL.selectedindex = -1
    > aspDL.EditItemIndex = objArgs.item.itemindex
    > aspDL.DataBind()
    > end sub
    >
    > sub DoItemUpdate(objSource as object, objArgs as DataListCommandEventArgs)
    > lblMessage.text = "in DoItemUpdate"
    > end sub
    >
    > sub DoItemDelete(objSource as object, objArgs as DataListCommandEventArgs)
    > lblMessage.text = "in DoItemDelete"
    > end sub
    >
    > sub DoItemCancel(objSource as object, objArgs as DataListCommandEventArgs)
    > lblMessage.text = "in DoItemCancel"
    > end sub
    > </script>
    >
    >

    <!--------------------------------------------------------------------------
    > ->
    > </body>
    > </html>
    >
    >
     
    Teemu Keiski, Aug 20, 2005
    #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. Jeremy
    Replies:
    0
    Views:
    388
    Jeremy
    Jul 2, 2004
  2. KK
    Replies:
    0
    Views:
    435
  3. Neil Jarman

    DataList doesn't fire events

    Neil Jarman, Aug 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    520
    Teemu Keiski
    Aug 20, 2005
  4. Crazy Cat
    Replies:
    3
    Views:
    377
    Crazy Cat
    Oct 14, 2008
  5. KK
    Replies:
    0
    Views:
    154
Loading...

Share This Page