Solved

How to fire ItemCommand Event on programatically added Datalist

Posted on 2009-04-12
6
1,101 Views
Last Modified: 2013-12-17
Hi,
I have a user control with a datalist and a textbox. I programatically added the user control in my page_load. The idea is when i click on a button in the datalist, the textbox will reflect the id on the selected datalist item.

The problem is, i can't seem to get the itemcommand events to be fired. Please advise.
.asc.vb

========
 

Partial Class UserCtrl

    Inherits System.Web.UI.UserControl
 

    Private _username As String

    Public Event getItemOnCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
 

    Protected Sub Datalist1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
 

        If e.CommandName = "getID" Then

            TextBox1.Text = "You have clicked on the datalist button"

        End If
 

        RaiseEvent getItemOnCommand(Me, e)
 

    End Sub
 
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 

        If Not Page.IsPostBack 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
 
 

.asc

============

<%@ Control Language="VB" AutoEventWireup="true" CodeFile="UserCtrl.ascx.vb" Inherits="UserCtrl" %>
 

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 

<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 runat="server" CommandName="getID" Text="Get ID" CommandArgument='<%#Eval("id")%>' />

    <br /><br />

</ItemTemplate>

</asp:DataList>
 

.aspx.vb

============
 

Imports System.Data

Imports System.Collections.Generic
 

Partial Class Default4

    Inherits System.Web.UI.Page
 

    Private Sub UserControlDatalist_getItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
 

        MsgBox("this is user control")

        Dim dummy As UserCtrl = sender

        Dim txt As TextBox = dummy.FindControl("TextBox1")
 

        Response.Write(txt.Text)
 

    End Sub
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
 

        Dim userCtrl As UserCtrl = LoadControl("UserCtrl.ascx")
 

        PlaceHolder1.Controls.Add(userCtrl)
 

        AddHandler userCtrl.getItemOnCommand, AddressOf UserControlDatalist_getItemCommand
 

    End Sub
 
 

End Class
 

.aspx

=======

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default4.aspx.vb" Inherits="Default4" %>

<%@ Register TagPrefix="My" TagName="UserCtrl" Src="UserCtrl.ascx" %> 
 

<!--Now we can access the UserInfoBoxControl class like if it were a regular class, 

which also means that we can typecast the UserControl returned by the LoadControl method to this type-->

<%@ Reference Control="UserCtrl.ascx" %>
 

<!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 id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:PlaceHolder runat="server" ID="ph1" /><br />

        <asp:PlaceHolder runat="server" ID="ph2" />

        <asp:Button runat="server" ID="btnAddBtn" Text="Add Button" CommandName="Button" /> 

        <asp:Button runat="server" ID="btnAddText" Text="Add Text" CommandName="TextBox" />

        <asp:Button runat="server" ID="btnAddDataList" Text="Add DataList" CommandName="DataList" />

    </div>

    <div>

    

    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

    

    </div>

    </form>

</body>

</html>

Open in new window

0
Comment
Question by:tangteng78
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:anuragal
ID: 24128252
Have you registered the Datalist1.ItemCommand on UserCtrl.ascx to call your function Datalist1_ItemCommand??
 
Anurag
0
 

Author Comment

by:tangteng78
ID: 24128322
From the code snippet attached i did this:
     Protected Sub Datalist1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)

        If e.CommandName = "getID" Then
            TextBox1.Text = "You have clicked on the datalist button"
        End If

        RaiseEvent getItemOnCommand(Me, e)

    End Sub

Not sure if that's what you meant by register? If it's not, can you share with me how to register the events?
 
0
 
LVL 11

Expert Comment

by:anuragal
ID: 24128840
On control UserCtrl.ascx you have to register the ItemCommand like
AddHandler userCtrl.ItemCommand, AddressOf Datalist1_ItemCommand
check whether you have already done that if not then do it.
 
Anurag
0
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.

 

Author Comment

by:tangteng78
ID: 24129205
Added in in the page_load in the ascx.vb. Stil the same, not working.

PS: I created a separate user control and replace the datalist with just a button. And it works. Seems to me that the datalist just not working with the same  button implementation

Please advise.


0
 

Author Comment

by:tangteng78
ID: 24129421
Well, i finally able to get it to work. I added in the "Handles Datalist1.ItemCommand" to the Sub Datalist1_ItemCommand in the asc.vb.

But my question is, if i do the same on the Button1_click event, the button will be fired twice. I have to commented out the "Handles Button1.Click" for it to work correctly (fire only 1 time).

Any explaination?

    Protected Sub Datalist1_ItemCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles Datalist1.ItemCommand
        _username = e.CommandName.ToString
        If e.CommandName = "getID" Then
            'MsgBox(e.CommandArgument.ToString)
            TextBox1.Text = "You have clicked on the datalist button" + e.CommandArgument.ToString
        End If

        RaiseEvent getItemOnCommand(Me, e)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) ' Handles Button1.Click
        _username = TextBox1.Text
        RaiseEvent getClick(Me, e)
    End Sub
0
 
LVL 11

Accepted Solution

by:
anuragal earned 500 total points
ID: 24129788
check your designer.cs file one handler should already added in that, because of that this event is fired twice.
 
Anurag
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

22 Experts available now in Live!

Get 1:1 Help Now