Search code examples
asp.netrepeaterlinkbutton

ASP .NET Repeater Control containing LinkButton


I am displaying a list of accounts objects in a repeater. Each object contains three fields namely studentID, studentName and studentAge. I have wrapped it with a link button. As a result the each individual item which is displayed is clickable. I have also added a click handler to the link button. The problem is that how am I supposed to know which student was clicked? Because originally I want to navigate to the next page and display that particular student's details. Once I know which student is clicked then I can store his details in a session object and then navigate to the new page and get it back.

Thanks for your help.


Solution

  • In your case, it would be better to use a normal hyperlink, pass the ID as query string, and have the new page get the details.

    <asp:HyperLink runat="server" 
                   NavigateUrl='<%# Eval("studentID", "StudentDetails.aspx?id={0}") %>'
                   Text="Details" />
    

    Update:

    To get the studentID on the same page, use the ItemCommand event instead and have the LinkButton as follows:

    <asp:LinkButton runat="server"
                    CommandName="StudentDetails"
                    CommandArgument='<%# Eval("studentID") %>' />
    

    The studentID will be stored into the CommandArgument property, which value will be passed to the ItemCommand event handler.