How to react to an ImageButon in a DataList ?

G

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
 
E

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
 
E

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
 
G

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top