Solved

ButtonLink in Datagrid passes ID to method

Posted on 2006-11-14
3
221 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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