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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 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