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
400 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 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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