3

I have a Results.aspx page that displays the resulting records queried using a SqlDataSource object via a ListView. I want to add a "View" button that will appear next to each record, and when clicked will take me to a separate page that will display details about that record. How do I accomplish this?

Edit

I have tried what you said, citronas and here's what I've come up with:

<td>
    <asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ViewButtonClick" CommandArgument='<%# Eval("ServiceId") %>'>View</asp:LinkButton>
</td>

And here is the method that I want to be called:

protected void ViewButtonClick(object sender, CommandEventArgs e)
    {
        var serviceId = Convert.ToInt32(e.CommandArgument);

        ServiceToView = DataAccessLayer.Service.Select(new Service { ServiceId = serviceId });
        Server.Transfer("~/ViewService.aspx");
    }

Unfortunately nothing actually happens...am I missing something?

Edit -- Fixed

I was missing something! I had CommandName equal to my method name instead of OnCommand. I took out CommandName, kept the argument bit and replaced CommandName with OnCommand. Everything works now, but what would I ever need CommandName for?

mleko
  • 11,650
  • 6
  • 50
  • 71
LunchMarble
  • 5,079
  • 9
  • 64
  • 94

2 Answers2

1

You can add a LinkButton into the ItemTemplate of the ListView.
Bind the value that identifies each record to the CommandArgument of the LinkButton.
Subscribe to the Command-Event of the LinkButton. There you have access to CommandEventArgs.CommandArgument

citronas
  • 19,035
  • 27
  • 96
  • 164
  • 1
    @Storm Kiernan:The OnCommand is the where you specify the serverside method that gets called if somebody clicks on the button. The CommandName is explained best by taking a look at the GridView. There are different sorts of 'actions' that you can do, like "Select", "Add", "Edit, "Delete". The GridView offers a RowCommand-Event where you use a switch-case do call other methds accordingly to the specified CommandName. You can also specify your own CommandName, as you have tried before. This makes sense if you add custom buttons to a GridView, like "Send E-Mail to customer" – citronas Feb 25 '11 at 05:25
0

What you wound up doing worked Storm. I decided to go with Citronas' suggestion and share my answer.

FIRST: On the aspx I added a LinkButton to my ItemTemplate with my own CommandName and CommandArgument. I passed my item's ID as a CommandArgument so I could later use it inside my sub.

<asp:LinkButton ID="lnkBtnAnswers" runat="server" CommandName="Answers"
     CommandArgument='<%# Eval("ID")%>'>Answers</asp:LinkButton>

SECOND: On the codebehind I created a sub that would be called whenever the user conducted an action. As Citronas mentioned normally you use "Select", "Add", "Edit", or "Delete" here. I decided to create "answers".

Note: Handles MyControl.ItemCommand is very important here as it is what subscribes you to the command event.

Protected Sub lvQuestions_Command(sender As Object, e As CommandEventArgs) Handles lvQuestions.ItemCommand
    If e.CommandName.ToLower() = "answers" Then
          hfSelectedQuestionID.Value = e.CommandArgument
    End If
End Sub

Done! Now since every command goes through the new sub, it is important to check for the right commandName so you can conduct the appropriate action. Don't forget to use the CommandArgument to your advantage.

Cesar
  • 2,229
  • 1
  • 22
  • 20