Mega Code Archive

 
Categories / ASP.Net Tutorial / ASP Net Controls
 

Use hidden field to pass value

<%@ Page Language="C#" AutoEventWireup="true"    CodeFile="Default.aspx.cs" Inherits="HiddenFieldTest" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server">     <title>HiddenField Test</title>     <style type="text/css">       #left { float: left; width: 200px; }       #right { float: right; width: 200px; }     </style> </head> <body>    <form id="form1" runat="server">    <div id="container">       <h1>HiddenField Test</h1>       Click multiple times on these two images       <div id="left">           <div class="box">                                <asp:ImageButton ID="ibtnImage1"                               runat="server"                               ImageUrl="http://www.rntsoft.com/style/logo.png"                               AlternateText="Click on me"                               OnClick="ibtnImage1_Click" />             <asp:Label ID="labMessage1" runat="server" />          </div>       </div>       <div id="right">          <div class="box">             <asp:ImageButton ID="ibtnImage2"                               runat="server"                               ImageUrl="http://www.rntsoft.com/style/logo.png"                               AlternateText="Click on me"                               OnClick="ibtnImage2_Click" />             <asp:Label ID="labMessage2" runat="server" />          </div>                      </div>       <asp:HiddenField ID="hfImage1" runat="server" />       <asp:HiddenField ID="hfImage2" runat="server" />          </div>    </form> </body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class HiddenFieldTest : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {       if (!IsPostBack)       {          hfImage1.Value = "0";          hfImage2.Value = "0";       }    }    protected void ibtnImage1_Click(object sender, EventArgs e)    {       IncrementCount(hfImage1);       labMessage1.Text = "# Clicks: " + hfImage1.Value;    }    protected void ibtnImage2_Click(object sender, EventArgs e)    {       IncrementCount(hfImage2);       labMessage2.Text = "# Clicks: " + hfImage2.Value;    }    private void IncrementCount(HiddenField hf)    {       int count = Convert.ToInt32(hf.Value);       count++;       hf.Value = count.ToString();    } }