ASP.NET Tutorial/ASP.net Controls/HiddenField

Материал из .Net Framework эксперт
Версия от 15:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Hidden fields and ViewState (C#)

   <source lang="csharp">

<%@ 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">
       <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
       <asp:Button ID="Button1" Runat="server" Text="Button"  />
       <asp:HiddenField ID="HiddenField1" Runat="server" />
   </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;
       }
   }

}</source>


Hidden fields and ViewState (VB)

   <source lang="csharp">

<%@ 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">
       <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
       <asp:Button ID="Button1" Runat="server" Text="Button"  />
       <asp:HiddenField ID="HiddenField1" Runat="server" />
   </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</source>


Use hidden field to pass value

   <source lang="csharp">

<%@ 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">

HiddenField Test

     Click multiple times on these two images
           <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" />
     <asp:HiddenField ID="hfImage1" runat="server" />
     <asp:HiddenField ID="hfImage2" runat="server" />      
  </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();
  }

}</source>


Working with the HiddenField server control (C#)

   <source lang="csharp">

<%@ 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></source>


Working with the HiddenField server control (VB)

   <source lang="csharp">

<%@ 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></source>