Link to home
Start Free TrialLog in
Avatar of navinbabu
navinbabuFlag for India

asked on

2 Hidden Fields in DataGrid

Hello guys ,

I have a datagrid and I want 2 hidden fields and I have a row command I want to retrieve the hidden field in the server side up on the row command .

I tried setting the bound field attribute to false and I'm not to retrieve the same at server side . Please suggest a alternative .

I'm looking for VB.NET code if possible

Avatar of krunal_shah

access the value of hiddeen fied in OnItemDataBound="Item_Bound" event like,

void Item_Bound(Object sender, DataGridItemEventArgs e) 
HtmlInputHidden hdnID = dgItem.Cells[0].FindControl("hdnID") as HtmlInputHidden;
if (hdnID!=null)
 int intID = Convert.ToInt32(hdnID.Value); //This way if the control is not found for any reason the application will not blow up exposing your logic

Open in new window

try this in code
Private Sub Item_Bound(ByVal sender As [Object], ByVal e As DataGridItemEventArgs)
    Dim hdnID As HtmlInputHidden = TryCast(dgItem.Cells(0).FindControl("hdnID"), HtmlInputHidden)
    If hdnID IsNot Nothing Then
            'This way if the control is not found for any reason the application will not blow up exposing your logic
        Dim intID As Integer = Convert.ToInt32(hdnID.Value)
    End If
End Sub

Open in new window

Avatar of navinbabu


Can i access the DatakeyName field value in server side ?
DataGrid.DataKeys(index)(number of the datakeyname).ToString
number in the sense ? I mean can we assign a field name to the datakeyname field and call it back in the server side so that we can avoid the use of the hidden field ?
Avatar of rajeeshmca
Hi navinbabu,

You can assign only the primary key field as a datakey field....

With the DataGrid ItemCommand Event u can Find the Value of the Hidden Field as like the Given example

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
            <asp:DataGrid ID="Dg1" runat="server">
                    <asp:TemplateColumn HeaderText="Test">
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'
                            <asp:HiddenField ID="Hidden1" runat="server" Value='<%# Eval("Id") %>' />
                    <asp:TemplateColumn >
                    <asp:Button ID="ClickButton" runat="server" Text="Click" CommandName="Click" />
vb page
Protected Sub Dg1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Dg1.ItemCommand
        If e.CommandName = "click" Then
            Dim objGridItem As DataGridItem
            objGridItem = DirectCast(DirectCast(source, Button).Parent.Parent, DataGridItem)
            Dim Id As String
            Id = DirectCast(Dg1.Items(objGridItem.ItemIndex).FindControl("Hidden1"), HiddenField).Value.ToString();
        End If
    End Sub

I have not bind ... Just an example...

Avatar of rajeeshmca
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there,
You can assign as many fields as you like to the datakeynames.
This article describes how a developer can use the features of the gridview and detailsview controls to update underlying database records.

In a 3-tier application, data is retrieved from the storage layer, usually with a stored procedure, and stored into objects in the central business layer, such as a  dataset or a list of class instances located in the App_data folder.

To be displayed, the object holding the data is bound to the gridview or details view, which are part of the presentation layer sent to the client. From the control, the user can edit the information and update it back into the database.

Often, we do not wish to display all the record information to the user on the screen. For example, we do not want to display the primary key and foriegn keys. We may not want to display fields that we do not want the user to update such as date_of_birth, or some other data. In the control, if we make these invisible, or delete the column from the grid, (or the field in the details view), we run into a problem when it comes to saving the changes. The primary key data is not available so the record cannot be updated.  
The answer to this problem lies in the datakeynames property of the control. Here, the server automatically stores the datavalues we specify for each record into an array. When the time comes for the record to be updated, the key data (and any other fields specified) are retrieved from the array and used to complete the update.  

The datakeynames can be set when the control is designed. After configuring the control's data source to the object, delete the columns you do not wish to display, then click on datakeynames, and the wizard will present a list of datafields, from which you pick these same names. This ensures that the values are saved for the update.

If you are binding to the object with code (as opposed to binding at design time), then the datakeynames property should be set before binding the object to the control. For example:
                    'the data is in a dataset called dsAddressNotes...
                    Dim mykeycodes(1) As String            'array
                    mykeycodes(0) = "notes_ID"              'first element in the array set to the primary key
                    mykeycodes(1) = "note_type_ID"       'second element in the array set to a foriegn key
                    gdvAddressNotes.DataKeyNames = mykeycodes 'set datakeynames to the array
                    gdvAddressNotes.DataSource = dsAddressNotes 'define the gidview's datasource
                    gdvAddressNotes.DataBind()   'bind the dataset to the gridview

I hope yhis will help you