How do I wire up a button in a datalist template?

M

moondaddy

using vb.net 1.1 I have a datalist and in it's item template I have 2
buttons, 'edit' and 'ShipToThisAddress'. The edit button works fine and it
opens up the edit template as it works with the edit command. But the how
do I wire ShipToThisAddress button? Its going to submit the address ID to
the database and do a redirect to another page. In the process of doing
this, it will need to get the row index in the datalist so I can get the
address ID from the datalist's keys collection.

Thanks.
 
M

moondaddy

OK I partially solved my problem, but I didn't answer my question. In
btnShipTo I used the item command by inputting 'item' in the command name
property for btnShipTo in the designer. Then in the codebehind I used the
following event to get the items key as a result of the button's the click
event and do something with it.

Private Sub dlAddresses_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
dlAddresses.ItemCommand
Dim key As String = dlAddresses.DataKeys.Item(e.Item.ItemIndex)
Console.WriteLine(key)
End Sub

HOWEVER

suppose this command was already being used for something like Selecting the
list item as the online documentation suggests. Then I would have to wire
up btnShipTo from scratch. How would I do that?
 
B

Bruno Sirianni

If you fill CommandName and CommandArgument of LinkButton you can use
ItemCommand event for handle event end get address ID for redirect!

In this way you can write code similar this :

[c# - CodeBehind]
private void DataList1_ItemCommand(object source, DataListCommandEventArgs
e)
{
if (e.CommandName.ToString() == "SkipTo")
{
string addressId = e.CommandArgument.ToString();
}
}

[ASP:NET]
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lbtSkip" Runat=server CommandName="SkipTo"
CommandArgument="<%# DataBinder.Eval(Container.DataItem, "AddressID")
%>"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
 
M

moondaddy

Thanks that worked great. I didn't realize that you have multiple buttons
use the item command event. Then in the item command event use a select
case for the command argument to handle each button differently.

--
(e-mail address removed)
Bruno Sirianni said:
If you fill CommandName and CommandArgument of LinkButton you can use
ItemCommand event for handle event end get address ID for redirect!

In this way you can write code similar this :

[c# - CodeBehind]
private void DataList1_ItemCommand(object source, DataListCommandEventArgs
e)
{
if (e.CommandName.ToString() == "SkipTo")
{
string addressId = e.CommandArgument.ToString();
}
}

[ASP:NET]
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lbtSkip" Runat=server CommandName="SkipTo"
CommandArgument="<%# DataBinder.Eval(Container.DataItem, "AddressID")
%>"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top