Link to home
Start Free TrialLog in
Avatar of colonialiu20
colonialiu20Flag for United States of America

asked on

ButtonLink in Datagrid passes ID to method

I use VB.NET

I have a datagrid which has been filled.  I want to have a column which contains a LinkButton.  When pressed that Link Button causes a Panel to appear that allows me to add records.

I know how to cause a Link Button to make the panel appear, disapear, etc.
But I want that Link Button to pass an ID from that datagrid record to that Panel (to a method for use in a database)

I know I can create a Hyperlink button to pass the ID in a URL, but I can't get that to work in this case.

I need an example of one of the following:

1) (Preferred) Link Button in DataGrid passes an ID to a method. Let's say the method just does a response.write of the ID.

2) Hyperlink passes BOTH the ID of a record in addition to an ID already in the URL.

Seems simple but I'm at a loss for the right solution.
Bonus points if both are answered and explained.
ASKER CERTIFIED SOLUTION
Avatar of Xeavn
Xeavn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Xeavn
Xeavn

Also fell free to ignore the pageing stuff.

intItemIndex = e.Item.ItemIndex + (rptrList.CurrentPageIndex) * rptrList.PageSize

should just be

intItemIndex = e.Item.ItemIndex

if you aren't doing any sort of paging.
Avatar of colonialiu20

ASKER

You gave me what I needed.
Essentially I was looking for this syntax:

Sub DataGrid (ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgList.ItemCommand

and seeing the command argument could be retrieved with e.CommandArgument assuming e was a 'DataGrid'CommandEventArgs

Within the datagrid 'dgDistrictStudents' I have a column which includes the following:

        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:LinkButton ID="addStudentToSlot" runat="server" CommandArgument='<%# Container.DataItem("StuStudentID") %>' Text="| Add" />
            </ItemTemplate>
        </asp:TemplateColumn>


*In Code Behind*
    Sub addStudent_Click(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles dgDistrictStudents.ItemCommand
        Dim intItemID As Integer
        intItemID = e.Item.ItemIndex
        Response.Write(e.CommandArgument)
    End Sub

Thanks for the help!