ASP.NET Tutorial/ASP.net Controls/HiddenField

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Hidden fields and ViewState (C#)

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Hidden Fields and ViewState</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
        <asp:Button ID="Button1" Runat="server" Text="Button"  />
        <asp:HiddenField ID="HiddenField1" Runat="server" />
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
[Serializable]
public class Person
{
    public string firstName;
    public string lastName;
}
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HiddenField1.Value = "foo";
            ViewState["AnotherHiddenValue"] = "bar";
            Person p = new Person();
            p.firstName = "Scott";
            p.lastName = "Hanselman";
            ViewState["HiddenPerson"] = p;
        }
    }
}


Hidden fields and ViewState (VB)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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>Hidden Fields and ViewState</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
        <asp:Button ID="Button1" Runat="server" Text="Button"  />
        <asp:HiddenField ID="HiddenField1" Runat="server" />
    </div>
    </form>
</body>
</html>
File: Default.aspx.vb
<Serializable()> _
Public Class Person
    Public firstName As String
    Public lastName As String
End Class
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles Me.Load
        If Not Page.IsPostBack Then
            HiddenField1.Value = "foo"
            ViewState("AnotherHiddenValue") = "bar"
            Dim p As New Person
            p.firstName = "Scott"
            p.lastName = "Hanselman"
            ViewState("HiddenPerson") = p
        End If
    End Sub
End Class


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.nfex.ru/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.nfex.ru/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();
   }
}


Working with the HiddenField server control (C#)

<%@ Page Language="C#"%>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField1.Value = System.Guid.NewGuid().ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HiddenField Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:HiddenField ID="HiddenField1" Runat="Server" />
    </form>
</body>
</html>


Working with the HiddenField server control (VB)

<%@ Page Language="VB" %>
<script runat="server" language="vb">    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       HiddenField1.Value = System.Guid.NewGuid().ToString()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HiddenField Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:HiddenField ID="HiddenField1" Runat="Server" />
    </form>
</body>
</html>