65.9K
CodeProject is changing. Read more.
Home

How to retrieve multiple datakeys from a FormView control

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.29/5 (6 votes)

Jul 22, 2008

CPOL
viewsIcon

28071

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

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.