Solved

Why itemcommand event not fired on datalist?

Posted on 2009-04-08
3
1,542 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:tangteng78
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 24098672
try this:

aspx:
<asp:DataList ID="Datalist1" runat="server" DataKeyField="id" RepeatColumns="3">
<HeaderTemplate>
<strong>Selection Table</strong>
</HeaderTemplate>
<ItemTemplate>
ID:
<%#Eval("id")%>
<br />
Name:
<%#Eval("name")%>
<br />
<asp:Button ID="Button1" runat="server" CommandName="getID" CommandArgument='<%#Eval("id")%>'
Text="Get ID" />
<br />
<br />
</ItemTemplate>
</asp:DataList>


code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack = True Then
'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 If

End Sub

Protected Sub Datalist1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles Datalist1.ItemCommand
Select Case e.CommandName
Case "getID"
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "msg", "alert('ID is: " & e.CommandArgument & "');", True)
End Select
End Sub
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 24098721
You should't call databind on a postback as it will interfere with the event handlers.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 24098781
I posted a tested and working example...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help displaying 2 models in one view in my MVC.Net application with C# code behind 5 34
Runtime Error 2 28
SQL Exceptions 3 38
Need a starter for ETL protocol? 4 42
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now