扩展Label控件(1) - 实现回发(Postback)功能

简介:
Label控件 既强大又好用。为了让它更强大、更好用,我们来写一个继承自Label的控件。
[源码下载]


扩展Label控件(1) - 实现回发(Postback)功能


作者: webabcd


介绍
扩展Label控件:
通过注册HiddenField控件,使Label控件支持回发(Postback)功能

使用方法(设置属性):
EnablePostback - 是否启用Label控件的回发(Postback)
HiddenFieldPostfix - 使Label支持回发(Postback)的隐藏控件的后缀名


关键代码
ScriptLibrary.js
//---------------------------- 
// http://webabcd.cnblogs.com/ 
//---------------------------- 

function yy_sl_copyTextToHiddenField(source, destination) 

/// <summary>将Label控件的的值赋给隐藏控件</summary> 

        document.getElementById(destination).value = document.getElementById(source).innerHTML; 
}
 
SmartLabel.cs
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.Web.UI.WebControls; 
using System.Web.UI; 
 
[assembly: System.Web.UI.WebResource( "YYControls.SmartLabel.Resources.ScriptLibrary.js""text/javascript")] 
 
namespace YYControls 

         /// <summary> 
         /// SmartLabel类,继承自DropDownList 
         /// </summary> 
        [ToolboxData( @"<{0}:SmartLabel runat='server'></{0}:SmartLabel>")] 
        [System.Drawing.ToolboxBitmap( typeof(YYControls.Resources.Icon),  "SmartLabel.bmp")] 
         public partial  class SmartLabel : Label 
        { 
                 /// <summary> 
                 /// 构造函数 
                 /// </summary> 
                 public SmartLabel() 
                { 
 
                } 
 
                 /// <summary> 
                 /// OnPreRender 
                 /// </summary> 
                 /// <param name="e">e</param> 
                 protected  override  void OnPreRender(EventArgs e) 
                { 
                         base.OnPreRender(e); 
 
                         // 实现Label控件的回发(Postback)功能 
                        ImplementPostback(); 
                } 
        } 
}
 
Property.cs
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.ComponentModel; 
using System.Web.UI; 
 
namespace YYControls 

         /// <summary> 
         /// SmartLabel类的属性部分 
         /// </summary> 
         public partial  class SmartLabel 
        { 
                 /// <summary> 
                 /// 使Label支持回发(Postback)的隐藏控件的后缀名 
                 /// </summary> 
                [ 
                Browsable( true), 
                Description( "使Label支持回发(Postback)的隐藏控件的后缀名"), 
                Category( "扩展"), 
                DefaultValue( "EnablePostback"
                ] 
                 public  virtual  string HiddenFieldPostfix 
                { 
                        get 
                        { 
                                 string s = ( string)ViewState[ "HiddenFieldPostfix"]; 
 
                                 return (s ==  null) ?  "EnablePostback" : s; 
                        } 
                        set 
                        { 
                                ViewState[ "HiddenFieldPostfix"] = value; 
                        } 
                } 
 
                 /// <summary> 
                 /// 是否启用Label控件的回发(Postback) 
                 /// </summary> 
                [ 
                Browsable( true), 
                Description( "是否启用Label控件的回发(Postback)"), 
                Category( "扩展"), 
                DefaultValue( false
                ] 
                 public  virtual  bool EnablePostback 
                { 
                        get 
                        { 
                                 bool? b = ( bool?)ViewState[ "EnablePostback"]; 
 
                                 return (b ==  null) ?  false : ( bool)b; 
                        } 
 
                        set 
                        { 
                                ViewState[ "EnablePostback"] = value; 
                        } 
                } 
        } 
}
 
EnablePostback.cs
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.Data; 
using System.Web.UI.WebControls; 
using System.Web.UI; 
using System.Web; 
 
namespace YYControls 

         /// <summary> 
         /// SmartLabel类的属性部分 
         /// </summary> 
         public partial  class SmartLabel 
        { 
                 /// <summary> 
                 /// 实现Label控件的回发(Postback)功能 
                 /// </summary> 
                 private  void ImplementPostback() 
                { 
                         if ( this.EnablePostback) 
                        { 
                                 // 使Label支持回发(Postback)的隐藏控件的ID 
                                 string hiddenFieldId =  string.Concat( this.ClientID,  "_", HiddenFieldPostfix); 
 
                                 // 注册隐藏控件 
                                Page.ClientScript.RegisterHiddenField(hiddenFieldId, ""); 
 
                                 // 注册客户端脚本 
                                 this.Page.ClientScript.RegisterClientScriptResource( this.GetType(), 
                                         "YYControls.SmartLabel.Resources.ScriptLibrary.js"); 
 
                                 // 表单提交前将Label控件的的值赋给隐藏控件 
                                 this.Page.ClientScript.RegisterOnSubmitStatement( this.GetType(), 
                                         string.Format( "yy_sl_enablePostback_{0}"
                                                 this.ClientID), 
                                         string.Format( "yy_sl_copyTextToHiddenField('{0}', '{1}')"
                                                 this.ClientID, 
                                                hiddenFieldId)); 
                        } 
                } 
 
                 /// <summary> 
                 /// 获取或设置 YYControls.SmartLabel 控件的文本内容 
                 /// </summary> 
                 public  override  string Text 
                { 
                        get 
                        { 
                                 try 
                                { 
                                         if ( this.EnablePostback && ! string.IsNullOrEmpty(HttpContext.Current.Request[ string.Concat( this.ClientID,  "_", HiddenFieldPostfix)])) 
                                        { 
                                                 // 隐藏控件的值 
                                                 return HttpContext.Current.Request[ string.Concat( this.ClientID,  "_", HiddenFieldPostfix)]; 
                                        } 
                                         else 
                                        { 
                                                 return  base.Text; 
                                        } 
                                } 
                                 catch 
                                { 
                                         return  base.Text; 
                                } 
                        } 
                        set 
                        { 
                                 try 
                                { 
                                         if ( this.EnablePostback && ! string.IsNullOrEmpty(HttpContext.Current.Request[ string.Concat( this.ClientID,  "_", HiddenFieldPostfix)])) 
                                        { 
                                                 // 隐藏控件的值 
                                                 base.Text = HttpContext.Current.Request[ string.Concat( this.ClientID,  "_", HiddenFieldPostfix)]; 
                                        } 
                                         else 
                                        { 
                                                 base.Text = value; 
                                        } 
                                } 
                                 catch 
                                { 
                                         base.Text = value; 
                                } 
                        } 
                } 
        } 
}
 



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345424 ,如需转载请自行联系原作者

相关文章
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp微信小程序的亿家旺生鲜云订单零售系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的亿家旺生鲜云订单零售系统的详细设计和实现
155 8
|
定位技术
Servlet 教程 之 Servlet 国际化 3
Servlet教程展示了如何实现国际化(i18n)和本地化(l10n)。通过request对象的getLocale()获取用户区域设置,利用DateFormat进行特定区域的日期格式化。示例代码显示了根据用户浏览器设置显示相应语言和文化的日期。
89 2
|
JSON 架构师 Java
SpringBoot:如何优雅地进行数据响应
SpringBoot:如何优雅地进行数据响应
1082 0
|
开发工具 git
CSS-多列布局3-瀑布流
1、实现效果 瀑布流 2、实现思路 (1) 使用多列布局进行布局。 (2) 使用column-break-inside 防止元素内部断开。 (3)使用img 宽度设置100%,可以等比例缩放到图片,显示不定宽度的图片。
1040 0
|
负载均衡 网络协议 Linux
Centos LVS DR模式详细搭建过程 推荐
目录 前言... 1 1、LVS环境组网... 2 2、ipvsadm安装前准备... 2 3、httpd与ipvsadm下载... 3 4、LVS负载均衡配置... 4 5、真实WEB服务器配置及arp抑制.
1029 0
|
14天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
6天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI