ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ASPxHiddenField

ASPxHiddenField

原创 Linux操作系统 作者:fangqm 时间:2010-07-22 15:07:40 0 删除 编辑

1.1    说明

类似于HTML的Hidden标签,在页面上不可见,主要是用来传递和同步客户端与服务器端的数据,但HiddenField功能更强大,同时改进了Asp.net下的Hidden从客户端赋值        而服务器端取不到值的问题。三它继承了

         IDictionary,

         违规广告ection>,

IEnumerable>一系列的接口,除了可以用键/值对的方式存取数据外,还提供了一系列的操作方法,如:Add、Set、Get、Clear、Remove等方法进行操作。

1.2    代码及用法(Js方法原型同样适用)

public void Add(string propertyName, object propertyValue);

        public void Set(string propertyName, object propertyValue);

        public void Get(string propertyName);

        public void Clear();

public bool Remove(string propertyName);

 

propertyName:key值。

propertyValue:Object.

1.3    代码与演示说明

HTML:在页在中放置一个ASPxHiddenField,并添加四个按钮进行读写操作控制。

  1. <dxhf:ASPxHiddenField ID="hdCollection" runat="server" ClientInstanceName="hdCollection">  
  2.   dxhf:ASPxHiddenField>  
  3.   <br />  
  4.   Key:    <input id="txtKey" type="text" /><br />  
  5.   Value:<input id="txtValue" type="text" />  
  6.   <br />  
  7.   <input id="Button1" type="button" value="Add/Set" onclick="Add()" />  
  8.   <input id="Button2" type="button" value="Get" onclick="Get()"/>  
  9.   <input id="Button3" type="button" value="Clear"  onclick="Clear()" />  
  10.   <input id="Button4" type="button" value="Remove" onclick="Remove()" />  


Key:  
Value:

JS代码,进行读写操作。

Add方法:用来向ASPxHiddenField写入值。

Remove方法:移除指定Key的值。

Get方法:获取指定Key的值

Clear方法:清除所有的值。

  1. "text/javascript"><!--   
  2.     function GetControl(id) {   
  3.         return document.getElementById(id);   
  4.     }   
  5.     function Add() {   
  6.         var key = GetControl("txtKey").value;   
  7.         var value = GetControl("txtValue").value;   
  8.         if (hdCollection.Get(key) == null) {   
  9.             hdCollection.Add(key, value);   
  10.         }   
  11.         else {   
  12.             hdCollection.Set(key, value);   
  13.         }   
  14.                
  15.     }   
  16.     function Remove() {   
  17.         var key = GetControl("txtKey").value;   
  18.         if (hdCollection.Get(key) != null) {   
  19.             hdCollection.Remove(key);   
  20.         }   
  21.         else {   
  22.             alert(key + " 值不存在!");   
  23.         }   
  24.     }   
  25.     function Get() {   
  26.         var key = GetControl("txtKey").value;   
  27.         if (hdCollection.Get(key) != null) {   
  28.             alert(hdCollection.Get(key));   
  29.         }   
  30.         else {   
  31.             alert(key + " 值不存在!");   
  32.         }   
  33.     }   
  34.     function Clear() {   
  35.         hdCollection.Clear();   
  36.     }   
  37. // -->  

<!-- function GetControl(id) { return document.getElementById(id); } function Add() { var key = GetControl("txtKey").value; var value = GetControl("txtValue").value; if (hdCollection.Get(key) == null) { hdCollection.Add(key, value); } else { hdCollection.Set(key, value); } } function Remove() { var key = GetControl("txtKey").value; if (hdCollection.Get(key) != null) { hdCollection.Remove(key); } else { alert(key + " 值不存在!"); } } function Get() { var key = GetControl("txtKey").value; if (hdCollection.Get(key) != null) { alert(hdCollection.Get(key)); } else { alert(key + " 值不存在!"); } } function Clear() { hdCollection.Clear(); } // -->

服务器代码,在加载页面时添加一个测试值。

  1. if (!IsPostBack)   
  2.             hdCollection.Add("DEMOKey", "Hello,World!");  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-668758/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
创业项目 https://u.rscode.cn

注册时间:2010-03-24

  • 博文量
    297
  • 访问量
    784625