What is HiddenField in ASP.NET?

HiddenField in ASP.NET

HiddenField, as the name implies, is hidden. This is non-visual control in ASP.NET where you can save the value. This is one of the types of client-side state management tools. It stores the value between the roundtrip. Anyone can see HiddenField details by simply viewing the source of the document.


HiddenFields are not encrypted or protected and can be changed by anyone. However, from a security point of view, this is not suggested. ASP.NET uses HiddenField control for managing the ViewState. So, don’t store any important or confidential data like password and credit card details with this control.

Use of HiddenField:
I show you simple example where hiddenfield is mostly used.

Create a new page to understand the concept of the hiddenfield.

Design page:


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Use_of_hiddenfield.aspx.cs" Inherits="Use_of_hiddenfield" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>            
            <table>
                <tr>
                    <td>
                        <asp:Label ID="lbl" runat="server" Text="Gender"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtgender" runat="server"></asp:TextBox>
                    </td>
                     <td>
                        <asp:Button ID="btnsubmit" runat="server" Text="Update" OnClick="btnsubmit_Click" />
                    </td>
                </tr>              
                <tr>
                    <td>
                        <br />
                    </td>                    
                </tr>
                <tr>                    
                    <td>                       
                        <asp:GridView ID="grid" runat="server" DataKeyNames="genderID" AutoGenerateColumns="false">
                            <Columns>
                                <asp:TemplateField HeaderText="S/N">
                                    <ItemTemplate>
                                        <asp:Label ID="lblsno" runat="server" Text="<%# Container.DataItemIndex+1 %>"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="Gname" HeaderText="Gender" />
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkedit" runat="server" Text="Edit" OnClick="lnkedit_Click"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>                       
                    </td>
                </tr>
                <%-- Hiddenfield --%>
                <asp:HiddenField ID="hdnid" runat="server" />
            </table>
        </div>
    </form>
</body>
</html>

Code file:


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Use_of_hiddenfield : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillgrid();
        }
    }
    public void fillgrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["DaljitSingh"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("select * from Gender", con);
        con.Open();
        grid.DataSource = cmd.ExecuteReader();
        grid.DataBind();
        con.Close();
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["DaljitSingh"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("update Gender set Gname='" + txtgender.Text + "' where genderID=" + hdnid.Value + "", con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            fillgrid();
            txtgender.Text = "";
            hdnid.Value = "";
        }
        con.Close();
    }
    protected void lnkedit_Click(object sender, EventArgs e)
    {
        LinkButton lnk = sender as LinkButton;
        GridViewRow gvrow = lnk.NamingContainer as GridViewRow;
        hdnid.Value = grid.DataKeys[gvrow.RowIndex].Value.ToString();
        string constr = ConfigurationManager.ConnectionStrings["DaljitSingh"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("select * from Gender where genderID=" + hdnid.Value + "", con);
        con.Open();       
        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                txtgender.Text = rdr["Gname"].ToString();
            }
        }
        con.Close();
    }
}

Explanation:

                   This code basically show that usage of hiddenfield in ASP.NET. This code shows how to edit any data that is stored on table using hiddenfield.

To use this code first create a table in your database(I create Gender Table).
To Create:
                 Expand your Database.
                 Right click on Tables folder.
                 Choose New and Click on Table.
                 
Hiddenfield
Add Column names
                Add Column names in a table as shown above.
                Press Ctrl+S to save and then enter the name of the table.

Insert entries into table.
as:-
       insert into gender values(1,'Male')
       insert into gender values(2,'Female')
       insert into gender values(3,'Transgender')


Design Page:


                       On Design page We use
                       -> textbox control
                       -> button for update
                       -> GridView     
                       -> Hiddenfield

Code Page:

                     Use three functions that are:

1. public void fillgrid() : Used to fill data in gridview. 

2. protected void btnsubmit_Click(object sender, EventArgs e):  Used to update data        using hiddenfield.
                                                           
3. protected void lnkedit_Click(object sender, EventArgs e): Fetch a key from datakey        of gridview and get data from server with that id and show data on the textbox.

 Output Screenshots:
                                 
                                    
Use of Hiddenfield
fig 1.
fig 1. shows the gridview and textbox. Here in gridview we have one edit linkbutton when we click on that link we store the unique id of the row in hiddenfield. And then fill that text on textbox control.



use of hiddenfield
fig 2.
fig 2. shows that textbox control filled with text when we click on edit link button.

Use of hiddenfield
fig 3.
 fig 3. shows that when we click on the update button that row gender name is changed as shown in fig 3. This is done using hiddenfield. We assign value to hiddenfield on linkbutton click function and used this hiddenfield in update button function.












Post a Comment

0 Comments