Link to home
Start Free TrialLog in
Avatar of tangteng78
tangteng78Flag for Malaysia

asked on

Why itemcommand event not fired on datalist?

Hi,
I have a simple datalist that binds to a datatable. In the datalist, there is a button. It seems that i can't get this button to fire (when clicked) the itemcommand event on the datalist.

My intention is to use the button to get the item ID selected in the datalist.

Please help.

PS: I'd set the EnableEventValidation="False", as i got the error message when click on the button. See below.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  
.aspx.vb
----------
Imports System.Data
 
Partial Class Default5
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        'Nothing fancy, just populating the datalist with rows.
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim dc As DataColumn
 
        dc = New DataColumn
        dc.ColumnName = "id"
        dt.Columns.Add(dc)
        dc = New DataColumn
        dc.ColumnName = "name"
        dt.Columns.Add(dc)
 
        dr = dt.NewRow
        dr("id") = 1
        dr("name") = "andy"
        dt.Rows.Add(dr)
 
        dr = dt.NewRow
        dr("id") = 2
        dr("name") = "ethan"
        dt.Rows.Add(dr)
 
        dr = dt.NewRow
        dr("id") = 3
        dr("name") = "Matt"
        dt.Rows.Add(dr)
 
        Datalist1.DataSource = dt
        Datalist1.DataBind()
 
    End Sub
 
    Protected Sub Datalist1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles Datalist1.ItemCommand
       
 MsgBox("I'd clicked on the datalist button")  '# I"M NOT GETTING TO THIS EVENT ON BUTTON CLICK
 
    End Sub
 
End Class
 
 
.aspx
--------
<%@ Page EnableEventValidation="false" Language="VB" AutoEventWireup="false" CodeFile="Default5.aspx.vb" Inherits="Default5" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    <asp:DataList ID="Datalist1" runat="server" 
            DataKeyField="id" 
            RepeatColumns = "3">
    <HeaderTemplate>
        <strong>Selection Table</strong>
    </HeaderTemplate>
    <ItemTemplate>
        ID: <%#DataBinder.Eval(Container.DataItem, "id")%>
        <br />
        Name: <%#DataBinder.Eval(Container.DataItem, "name")%>
        <br />
        <asp:Button runat="server" CommandName="getID" Text="Get ID" />
        <br /><br />
    </ItemTemplate>
    </asp:DataList>
    
    </div>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You should't call databind on a postback as it will interfere with the event handlers.
I posted a tested and working example...