Solved

ButtonLink in Datagrid passes ID to method

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

20 Experts available now in Live!

Get 1:1 Help Now