Home All Groups Group Topic Archive Search About

How to react to an ImageButon in a DataList ?

Author
19 May 2005 5:15 PM
Gilles Lambert
Hi,

I'm used to LinkButtons with CommandName property (Update, Delete, etc.)
in a DataList to get the correspondind events, but it does'nt work with
an ImageButton. The PostBack seems to appen, but the event handler is
not called. So i'm sure it's possible to use other kind of butons than
LinkButon, typically an ImageButton, but i don't understand why it
does'nt works ?

Thanks and best regards
GL



*** Sent via Developersdex http://www.developersdex.com ***

Author
20 May 2005 2:30 AM
EJD
Hi -
I believe that in order to capture control events when they are
templated items you have to create the event handler for the
"ItemCommand" for the DataList.  After creating the event handler in
the code behind, I edited the page in HTML and added the following
CommandName and CommandArgument for the image button.  I bound the
orderId from the Northwind database orders table so that when the
button is clicked the command argument is actually the orderId.

<asp:datalist id=DataList1 style="Z-INDEX: 101; LEFT: 112px; POSITION:
absolute; TOP: 40px" runat="server" DataMember="Orders"
DataKeyField="OrderID" DataSource="<%# dataSet11 %>">
<ItemTemplate>
    <asp:ImageButton id="ImageButton1" runat="server" ImageUrl="..."
CommandName="transfer" CommandArgument='<%# DataBinder.Eval(Container,
"DataItem.orderid") %>'></asp:ImageButton>
                </ItemTemplate>
</asp:datalist>

In the code behind I handled the click like this:
private void DataList1_ItemCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
              // sling takes the orderId, and it can be used for
whatever...
                string sling = e.CommandArgument.ToString();
    Server.Transfer("aPage.aspx");
}

The same applies to DataGrids as well...and perhaps some other controls
as well...

HTH, and good luck!
EJD
Author
20 May 2005 1:45 PM
Gilles Lambert
Everthing is OK with a LinkButton, but nothing to do with an
ImageButton. So, i've found a solution with an simple image inside à
LinkButton and it works !

<asp:LinkButton id="Linkbutton2" runat="server" Width="50px"
CommandName="Delete">
<IMG alt="" src="images/btn_delete.gif" border="0"></asp:LinkButton>

Thanks ;-)
Gilles


*** Sent via Developersdex http://www.developersdex.com ***
Author
20 May 2005 2:34 AM
EJD
Oh, and another thing.  I've noticed that from time to time after
modifying a DataGrid or DataList that sometimes that events that you
had created for the control using the designer or whatever just seem to
stop working.  As in the code is still there in the code behind, but
the event handler has lost its link to the control.  I don't know if
that applies here...

EJD