Click here to Skip to main content
15,895,799 members
Articles / Web Development / ASP.NET

How to retrieve multiple datakeys from a FormView control

Rate me:
Please Sign up or sign in to vote.
2.29/5 (6 votes)
22 Jul 2008CPOL 27.9K   7  
This article will show how to retrieve multiple datakeys from a FormView control.

Introduction

This article will show how to retrieve multiple datakey names from a FormView control. I could not find any sample code on the Internet which would show the simplest way to accomplish this task. The following simple code will retrieve multiple datakeys from a FormView.

Background

Basically, you setup a FormView control with multiple datakeys. Now the question is, how do you retrieve multiple keys in your code-behind for accomplishing tasks such as updating?

And on to the sample code...

First, you would define your FormView control with the DataKeyNames containing the multiple keys:

ASP.NET
<asp:FormView ID="frvTesting" runat="server" DataKeyNames="UserGroup,UserSubGroup" ...

Now, to access the keys, use the following code. The example below is for the update routine:

VB
Protected Sub frvTesting_ItemUpdating(ByVal sender As Object, _
          ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) _
          Handles frvTesting.ItemUpdating
    Try

        Dim UserGroup_Key, UserSubGroup_Key As String
        ' Get Keys from datakey collection
        UserGroup_Key= frvTesting.DataKey("UserGroup")
        UserSubGroup_Key= frvTesting.DataKey("UserSubGroup")
        ...

Points of interest

Note that the way to retrieve the keys is different from retrieving the keys for a GridView control.

Conclusion

The above was a very simple example on how to retrieve multiple keys from a FormView control.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --