Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ButtonLink in Datagrid passes ID to method

Posted on 2006-11-14
3
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
Xeavn earned 800 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

704 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