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

  • Thread starter Thread starter moondaddy
  • Start date Start date
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.
 
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?
 
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>
 
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>
 
Back
Top