Solved

How Do I Programatically Bind a ItemTemplate's LinkButton Control To The Primary Key Column In a GridView Control?

Posted on 2010-09-08
5
407 Views
Last Modified: 2012-05-10
When I click on a link button next to each row in the GridView it should show the details of that entry in a DetailsView. For this purpose I added an ItemTemplate to GridView which further has a LinkButton in it.. So i need the complete code as to how i can link this LinkButton next to each row of the GridView to the primary key column of the Table displayed in GridView control. Please specify complete source code or any link related to this topic.Thanks :). Code will be most appreciated
0
Comment
Question by:starlite551
[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
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Kaushal Arora earned 500 total points
ID: 33626129
You can do it in two ways:

1.

You can do it using the DataKeyNames collection of the gridview and the CommandArgument attribute of the linkbutton.

//How to Use DataKeyNames Collection
http://www.codedigest.com/Articles/ASPNET/212_How_to_edit_and_Delete_records_in_a_gridview_using_datakeys.aspx

//How to Use the CommandArgument Attribute of Linkbutton
http://stackoverflow.com/questions/389403/asp-net-gridview-rowindex-as-commandargument

Now after studying these two you can use both to get what you want.
You can ssign your primary key as datakeyname and give the index property of the row in the commandargument attribute to get the desired primary key of that particular row.

2.

You can directly bind your primary key in the CommandArgument of the Linkbutton and can access it in any of the gridview event preferable in the SelectedIndexChanging Event

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanging.aspx


Hope it helps you.
0
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33626308
In the second method you cannot use the selectedindexchanging event. i have written it by mistake. You can use the row updating event in that method. i am attaching the code for the 1st method.

Hope it helps.
Default2.aspx
Default2.aspx.cs
0
 
LVL 12

Author Comment

by:starlite551
ID: 33628703
Hey Thats good but I wanted the solution using a LinkButton if u have that.. Can u plz post the LinkButton version of the code..??
0
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33633657
I have used the linkbutton in the code i have given you. Kindly check that.
0
 
LVL 12

Author Comment

by:starlite551
ID: 33633961
ok i didn`t notice that one.. thanks :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

615 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