博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
扩展Label控件(1) - 实现回发(Postback)功能
阅读量:6507 次
发布时间:2019-06-24

本文共 4085 字,大约阅读时间需要 13 分钟。

Label控件
既强大又好用。为了让它更强大、更好用,我们来写一个继承自Label的控件。




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


作者:



介绍

扩展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
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI; 

InBlock.gif 

InBlock.gif[assembly: System.Web.UI.WebResource(
"YYControls.SmartLabel.Resources.ScriptLibrary.js"
"text/javascript")] 

InBlock.gif 

InBlock.gif
namespace YYControls 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SmartLabel类,继承自DropDownList 

InBlock.gif        
/// </summary> 

InBlock.gif        [ToolboxData(
@"<{0}:SmartLabel runat='server'></{0}:SmartLabel>")] 

InBlock.gif        [System.Drawing.ToolboxBitmap(
typeof(YYControls.Resources.Icon), 
"SmartLabel.bmp")] 

InBlock.gif        
public partial 
class SmartLabel : Label 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
public SmartLabel() 

InBlock.gif                { 

InBlock.gif 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// OnPreRender 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="e">e</param> 

InBlock.gif                
protected 
override 
void OnPreRender(EventArgs e) 

InBlock.gif                { 

InBlock.gif                        
base.OnPreRender(e); 

InBlock.gif 

InBlock.gif                        
// 实现Label控件的回发(Postback)功能 

InBlock.gif                        ImplementPostback(); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
Property.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ComponentModel; 

InBlock.gif
using System.Web.UI; 

InBlock.gif 

InBlock.gif
namespace YYControls 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SmartLabel类的属性部分 

InBlock.gif        
/// </summary> 

InBlock.gif        
public partial 
class SmartLabel 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 使Label支持回发(Postback)的隐藏控件的后缀名 

InBlock.gif                
/// </summary> 

InBlock.gif                [ 

InBlock.gif                Browsable(
true), 

InBlock.gif                Description(
"使Label支持回发(Postback)的隐藏控件的后缀名"), 

InBlock.gif                Category(
"扩展"), 

InBlock.gif                DefaultValue(
"EnablePostback"

InBlock.gif                ] 

InBlock.gif                
public 
virtual 
string HiddenFieldPostfix 

InBlock.gif                { 

InBlock.gif                        get 

InBlock.gif                        { 

InBlock.gif                                
string s = (
string)ViewState[
"HiddenFieldPostfix"]; 

InBlock.gif 

InBlock.gif                                
return (s == 
null) ? 
"EnablePostback" : s; 

InBlock.gif                        } 

InBlock.gif                        set 

InBlock.gif                        { 

InBlock.gif                                ViewState[
"HiddenFieldPostfix"] = value; 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 是否启用Label控件的回发(Postback) 

InBlock.gif                
/// </summary> 

InBlock.gif                [ 

InBlock.gif                Browsable(
true), 

InBlock.gif                Description(
"是否启用Label控件的回发(Postback)"), 

InBlock.gif                Category(
"扩展"), 

InBlock.gif                DefaultValue(
false

InBlock.gif                ] 

InBlock.gif                
public 
virtual 
bool EnablePostback 

InBlock.gif                { 

InBlock.gif                        get 

InBlock.gif                        { 

InBlock.gif                                
bool? b = (
bool?)ViewState[
"EnablePostback"]; 

InBlock.gif 

InBlock.gif                                
return (b == 
null) ? 
false : (
bool)b; 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        set 

InBlock.gif                        { 

InBlock.gif                                ViewState[
"EnablePostback"] = value; 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
EnablePostback.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.Data; 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI; 

InBlock.gif
using System.Web; 

InBlock.gif 

InBlock.gif
namespace YYControls 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SmartLabel类的属性部分 

InBlock.gif        
/// </summary> 

InBlock.gif        
public partial 
class SmartLabel 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 实现Label控件的回发(Postback)功能 

InBlock.gif                
/// </summary> 

InBlock.gif                
private 
void ImplementPostback() 

InBlock.gif                { 

InBlock.gif                        
if (
this.EnablePostback) 

InBlock.gif                        { 

InBlock.gif                                
// 使Label支持回发(Postback)的隐藏控件的ID 

InBlock.gif                                
string hiddenFieldId = 
string.Concat(
this.ClientID, 
"_", HiddenFieldPostfix); 

InBlock.gif 

InBlock.gif                                
// 注册隐藏控件 

InBlock.gif                                Page.ClientScript.RegisterHiddenField(hiddenFieldId, ""); 

InBlock.gif 

InBlock.gif                                
// 注册客户端脚本 

InBlock.gif                                
this.Page.ClientScript.RegisterClientScriptResource(
this.GetType(), 

InBlock.gif                                        
"YYControls.SmartLabel.Resources.ScriptLibrary.js"); 

InBlock.gif 

InBlock.gif                                
// 表单提交前将Label控件的的值赋给隐藏控件 

InBlock.gif                                
this.Page.ClientScript.RegisterOnSubmitStatement(
this.GetType(), 

InBlock.gif                                        
string.Format(
"yy_sl_enablePostback_{0}"

InBlock.gif                                                
this.ClientID), 

InBlock.gif                                        
string.Format(
"yy_sl_copyTextToHiddenField('{0}', '{1}')"

InBlock.gif                                                
this.ClientID, 

InBlock.gif                                                hiddenFieldId)); 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取或设置 YYControls.SmartLabel 控件的文本内容 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
override 
string Text 

InBlock.gif                { 

InBlock.gif                        get 

InBlock.gif                        { 

InBlock.gif                                
try 

InBlock.gif                                { 

InBlock.gif                                        
if (
this.EnablePostback && !
string.IsNullOrEmpty(HttpContext.Current.Request[
string.Concat(
this.ClientID, 
"_", HiddenFieldPostfix)])) 

InBlock.gif                                        { 

InBlock.gif                                                
// 隐藏控件的值 

InBlock.gif                                                
return HttpContext.Current.Request[
string.Concat(
this.ClientID, 
"_", HiddenFieldPostfix)]; 

InBlock.gif                                        } 

InBlock.gif                                        
else 

InBlock.gif                                        { 

InBlock.gif                                                
return 
base.Text; 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                                
catch 

InBlock.gif                                { 

InBlock.gif                                        
return 
base.Text; 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif                        set 

InBlock.gif                        { 

InBlock.gif                                
try 

InBlock.gif                                { 

InBlock.gif                                        
if (
this.EnablePostback && !
string.IsNullOrEmpty(HttpContext.Current.Request[
string.Concat(
this.ClientID, 
"_", HiddenFieldPostfix)])) 

InBlock.gif                                        { 

InBlock.gif                                                
// 隐藏控件的值 

InBlock.gif                                                
base.Text = HttpContext.Current.Request[
string.Concat(
this.ClientID, 
"_", HiddenFieldPostfix)]; 

InBlock.gif                                        } 

InBlock.gif                                        
else 

InBlock.gif                                        { 

InBlock.gif                                                
base.Text = value; 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                                
catch 

InBlock.gif                                { 

InBlock.gif                                        
base.Text = value; 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 

OK
     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345424
,如需转载请自行联系原作者
你可能感兴趣的文章
使用Spring Cloud和Docker构建微服务
查看>>
常用链接
查看>>
NB-IoT的成功商用不是一蹴而就
查看>>
九州云实战人员为您揭秘成功部署OpenStack几大要点
查看>>
1.电子商务支付方式有哪些 2.比较不同支付方式的优势劣势
查看>>
医疗卫生系统被爆漏洞,7亿公民信息泄露……
查看>>
神秘函件引发的4G+与全网通的较量
查看>>
CloudCC:智能CRM究竟能否成为下一个行业风口?
查看>>
高德开放平台推出LBS游戏行业解决方案提供专业地图平台能力支持
查看>>
追求绿色数据中心
查看>>
Web开发初学指南
查看>>
OpenStack Days China:华云数据CTO郑军分享OpenStack创新实践
查看>>
探寻光存储没落的真正原因
查看>>
高通64位ARMv8系列服务器芯片商标命名:Centriq
查看>>
中国人工智能学会通讯——融合经济学原理的个性化推荐 1.1 互联网经济系统的基本问题...
查看>>
盘点大数据商业智能的十大戒律
查看>>
戴尔为保护数据安全 推出新款服务器PowerEdge T30
查看>>
今年以来硅晶圆涨幅约达40%
查看>>
构建智能的新一代网络——专访Mellanox市场部副总裁 Gilad Shainer
查看>>
《数字视频和高清:算法和接口》一导读
查看>>