Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Thu, 23-Oct-2014 Authors
All Time Authors
Sourav.Kayal
39750
SheoNarayan
38050
Niladri.biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads
 Article

Getting selected record key value from ASP.NET Data Controls

SheoNarayan
Posted by under ASP.NET category on for Beginner level | Views : 73253 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
1 vote(s)
This article shows how to get Selected record key value from GridView, DataList, FormView, DetailsView, ListView, and Repeater controls.

 Download source code for Getting selected record key value from ASP.NET Data Controls


Introduction
While working with Data controls in ASP.NET like GridView, ListView, DataList, DetailsView, FormView, and Repeater controls; very oftenly we need to get the select record key value in code file. In this article, I have shown how to get selected record key value of all these Data Controls. To show how to get selected key value, I have created a sample DataTable as DataSource and populated all these data controls.


Populating Data Controls

I have defined a class level (member variable) DataTable and using it to populate all Data controls. This DataTable has two column, ID and Description.

/// <summary>

/// Fill data into DataTable, DataSource for this demo

/// </summary>

private void FillDataIntoDataSource()

{

dTable.Columns.Add(new DataColumn("ID", typeof(int)));

dTable.Columns.Add(new DataColumn("Description", typeof(string)));

for (int i = 1; i <= 10; i++)

{

i++;

dTable.Rows.Add(i, "Record Number " + i);

}

}





Now, lets see how to get selecetd key value from each of these controls one by one.

Getting Selected Key Value from GridView control

<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="true" DataKeyNames="ID"

OnSelectedIndexChanging="GridViewGetSelectingValue">

</asp:GridView>

I have dragged GridView control and sepecified its DataKeyNames property as ID, set AutoGenerateSelectButton as true and specified OnSelectedIndexChanging event so that when user clicks Select link, this event will fire. Lets see the code for this event.

protected void GridViewGetSelectingValue(object sender, GridViewSelectEventArgs e)

{

string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();

lblMessage.Text = "GridView Selected ID : " + id;

}

To get the the selected record key value, use DataKeys property of the GridView and pass NewSelectedIndex of the GridViewSelectEventArgs.

Getting Selected Key Value from DataList control

<asp:DataList ID="DataList1" runat="server" DataKeyField="ID"

onselectedindexchanged="DataList1_SelectedIndexChanged">

<HeaderTemplate><table border="1"></HeaderTemplate>

 

<ItemTemplate>

<tr><td><%# Eval("ID") %></td><td><%# Eval("Description") %></td>

<td><asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" Text="Select" /></td>

</tr>

</ItemTemplate>

 

<FooterTemplate>

</table>

</FooterTemplate>

</asp:DataList>

I have dragged a DataList control and specified Header, Item and Footer template and have also specified DataKeyField value as ID. To fire OnSelectedIndexChanged event I have placed a link button and specified CommandName as Select.

protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)

{

string id = DataList1.DataKeys[DataList1.SelectedIndex].ToString();

lblMessage.Text = "DataList Selected ID : " + id;

}

To get the selected record key value, use DataKeys property of the DataList and pass SelectedIndex of the ListView as parameter.


Getting Selected Key Value from FormView Control

Hardly, you need to gethe selected record key value for FormView however, if you need to get the selected key value from FormView, you can use following approach.

 

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" AllowPaging="true"

OnItemCommand="FormViewGetSelectedID" OnPageIndexChanging="FormViewChangePage" >

<HeaderTemplate><table border="1"></HeaderTemplate>

 

<ItemTemplate>

<tr><td><%# Eval("ID") %></td>

<td><%# Eval("Description") %></td>

<td><asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" Text="Select" /></td>

</tr>

</ItemTemplate>

 

<FooterTemplate>

</table>

</FooterTemplate>

</asp:FormView>

Specify DataKeyNames property as ID. As there is no event like SelectedIndexChange for the FormView so use OnItemCommand event to do that.  To fire OnItemCommand, I have placed a LinkButton and set its CommandName to Select and specified OnItemCommand event as FormViewGetSelectedID.

protected void FormViewGetSelectedID(object sender, FormViewCommandEventArgs e)

{

if (e.CommandName.Equals("Select"))

{

string id = FormView1.SelectedValue.ToString();

lblMessage.Text = "FormView Selected ID : " + id;

}

}

As this event whenever any command button will be clicked on FormView, so check for the command name. To get the selected value from the FormView, simply use the SelectedValue property of the FormView control.

Getting Selected Key Value from ListView

<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID"

onselectedindexchanging="ListView1_SelectedIndexChanging">

<LayoutTemplate>

<table border="1">

<tr><th>ID</th><th>Description</th><th>Select</th></tr>

<asp:Panel ID="ItemPlaceHolder" runat="server"></asp:Panel>

</table>

</LayoutTemplate>

 

<ItemTemplate>

<tr><td><%# Eval("ID") %></td><td><%# Eval("Description") %></td>

<td><asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" Text="Select" /></td>

</tr>

</ItemTemplate>

 

</asp:ListView>

I have dragged a ListView control from the ToolBox and specified LayoutTemplate and ItemTemplate. I have also specified DataKeyNames property as ID. To fire OnselectedIndexChanging event, I have placed a LinkButton and set its CommandName as select.

protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)

{

string id = ListView1.DataKeys[e.NewSelectedIndex].Value.ToString();

lblMessage.Text = "ListView Selected ID : " + id;

}

 

To get the selected key value from the ListView, use DataKeys property and pass NewSelectedIndex of the ListViewSelectEventArgs.

Getting Selected Key Value from DetailsView

Same as FormView, you may not need this so oftenly but you get the Selected key value like this.

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataKeyNames="ID"

OnPageIndexChanging="DetailsViewChangePage" OnItemCommand="DetailsViewGetSelectedID"

AutoGenerateRows="false" AllowPaging="true">

 

<Fields>

<asp:BoundField DataField="ID" HeaderText="ID" />

<asp:BoundField DataField="Description" HeaderText="Description" />

 

<asp:TemplateField HeaderText="Select"><ItemTemplate>

<asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" Text="Select" />

</ItemTemplate></asp:TemplateField>

</Fields>

 

</asp:DetailsView>

I have specified DataKeyNames property as ID. As there is no event like OnSelectIndexChange so I have used OnItemCommand event to do that. To fire OnItemCommand event of the DetailsView, I have placed a link button and specified CommandName as select.

protected void DetailsViewGetSelectedID(object sender, DetailsViewCommandEventArgs e)

{

if (e.CommandName.Equals("Select"))

{

string id = DetailsView1.SelectedValue.ToString();

lblMessage.Text = "DetailsView Selected ID : " + id;

}

}

As this event will fire whenever any command button will be clicked, so first check it if the clicked button is Select. To get the selected key value use SelectedValue property of the DetailsView.

Getting Selected Value from Repeater Control

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="RepeaterGetSelectedID">

<HeaderTemplate><table border="1"></HeaderTemplate>

<ItemTemplate>

<tr><td>

<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID") %>' />

<%# Eval("ID") %>

</td><td><%# Eval("Description") %></td>

<td><asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" Text="Select" /></td>

</tr>

</ItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:Repeater>

As there is no property called DataKeyNames in the Repeater control as in case of all other data controls, so I have placed a hidden control and set its value property as ID, I will use this control to get the ID in the code file. There is no event like OnSelectedIndexChange so I will use OnItemCommand event to do that. To fire the OnItemCommand event, I have placed a link button and specified CommandName as Select.

protected void RepeaterGetSelectedID(object source, RepeaterCommandEventArgs e)

{

if (e.CommandName.Equals("Select"))

{

HiddenField hiddenID = (HiddenField) e.Item.FindControl("HiddenID");

string id = hiddenID.Value;

lblMessage.Text = "Repeater Selected ID : " + id;

}

}

As this event will fire whenever any button (if any other available) will be clicked on the Repeater control, so first check if the CommandName of the clicked button is Select. If it is, then find the HiddenField control and get its value.


Conclusion

In this article, I tried to show how to get the selected record key value from almost all Data controls available in ASP.NET. Please note that different events of these data controls may have differnt way to get it.

Hope this helps, feel free to download the sample. Thanks for reading.

Page copy protected against web site content infringement by Copyscape
About the Author

SheoNarayan

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Microsoft_MVP,Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com

Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan
Login to vote for this post.
Found interesting? Add this to:


Comments or Responses

Posted by: Poster on: 10/16/2008

Thank you very much, your article is always very useful. This one is like a repository for those who frequently use Data controls.

Thanks you and keep it up!!!

Login to post response

Comment using Facebook(Author doesn't get notification)