Avatar of navinbabu
navinbabu
Flag 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

Thanks
.NET ProgrammingASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
chrisgreaves

8/22/2022 - Mon
krunal_shah

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

krunal_shah

try this in vb.net 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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
navinbabu

ASKER
Can i access the DatakeyName field value in server side ?
krunal_shah

DataGrid.DataKeys(index)(number of the datakeyname).ToString
navinbabu

ASKER
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 ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
krunal_shah

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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DataGrid ID="Dg1" runat="server">
                <Columns>
                    <asp:TemplateColumn HeaderText="Test">
                        <ItemTemplate>
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'
                            <asp:HiddenField ID="Hidden1" runat="server" Value='<%# Eval("Id") %>' />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn >
                    <ItemTemplate>
                    <asp:Button ID="ClickButton" runat="server" Text="Click" CommandName="Click" />
                    </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </div>
    </form>
</body>
</html>
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...

ASKER CERTIFIED SOLUTION
rajeeshmca

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
chrisgreaves

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
Chris
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck