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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

867 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

17 Experts available now in Live!

Get 1:1 Help Now