Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ButtonLink in Datagrid passes ID to method

Posted on 2006-11-14
3
Medium Priority
?
227 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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