Solved

ButtonLink in Datagrid passes ID to method

Posted on 2006-11-14
3
218 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:colonialiu20
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
Xeavn earned 200 total points
ID: 17942282
When you say Method, do you mean like a function? If so I think I can show you how to do #1.

<ItemTemplate>
<tr class="dgitem" onmouseover="l(this,1);" onmouseout="l(this,0);" onclick="chk(this);" RunAt="Server">
                <td id="MultiSelectCell" runat="server"><asp:CheckBox Id="SelectCB" RunAt="Server" /></td>
      <td style="padding-right:0.5em;"><span><%# DataBinder.Eval(Container.DataItem, "MTHD_REF") %></span></td>
      <td style="padding-right:0.5em;"><span><%# DataBinder.Eval(Container.DataItem, "PRM_ABBREV") %></span></td>
      <td style="padding-right:0.5em;"><span><%# DataBinder.Eval(Container.DataItem, "MTHD_DESCR") %></span></td>
      <td id="DeleteCell" visible="false" runat="server"><asp:LinkButton id="DeleteButton" causesvalidation="false" commandargument='<%# DataBinder.Eval(Container.DataItem, "MTHD_ID") %>' commandname="delete" text="<img src='images/delete.ico' />" runat="server" /></td>
</tr>
</ItemTemplate>

Here is an example of a fairly simple ItemTemplate where on the last column I have a DeleteButton LinkButton which has CommandArgument set to be an id. Then in the code behind I can access that click by handling the DataGrid ItemCommand Event. You could then call a function with the id, or do whatever you want with it after you retrieve it from the Event Arguments.

Protected Sub dgCommandList(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgList.ItemCommand
   Dim intItemIndex As Integer
   intItemIndex = e.Item.ItemIndex + (rptrList.CurrentPageIndex) * rptrList.PageSize
   Select Case e.CommandName
      Case "delete"
            Dim intMethodID As Integer
                      intMethodID = Convert.ToInt32(e.CommandArgument)
                                Response.Write(intMethodID)
      Case Else
         Response.Write("Unhandled Event - " & dgList.ID & " - " & e.CommandName & " - " & e.CommandArgument.ToString)
      End Select
End Sub 'dgCommandList
0
 
LVL 4

Expert Comment

by:Xeavn
ID: 17942305
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.
0
 

Author Comment

by:colonialiu20
ID: 17947223
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now