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

x
?
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
Medium Priority
?
410 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
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Kaushal Arora earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

916 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