扩展Label控件(1) - 实现回发(Postback)功能
2008-01-22 15:38
316 查看
来源:http://webabcd.cnblogs.com
介绍
扩展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(),
"yy_sl_enablePostback",
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;
}
}
}
}
}
OK
[源码下载]
介绍
扩展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(),
"yy_sl_enablePostback",
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;
}
}
}
}
}
OK
[源码下载]
相关文章推荐
- 扩展Label控件(1) - 实现回发(Postback)功能
- 扩展Label控件(1) - 实现回发(Postback)功能
- 扩展Label控件(1) - 实现回发(Postback)功能
- 扩展Label控件(1) - 实现回发(Postback)功能
- 扩展Label控件(1) - 实现回发(Postback)功能
- 扩展Label控件(1) - 实现回发(Postback)功能
- MFC使用CEF并实现js与C++交互功能,解决Render进程中OnContextCreated绑定与OnWebKitInitialized的js扩展无法回调问题
- jQuery基于扩展简单实现倒计时功能的方法
- PHP扩展开发之实现 require PHP文件功能
- 地磅称量系统之(53)在封装对象的类库中实现包括IDataErrorInfo接口提供的所有方法和并且扩展对异常具有添加和删除功能的基本业务对象基类
- 【IOS】扩展UIImageViewEx实现:手势移动,旋转,缩放(附带一个收缩的文字标签功能)
- 实现PHP5.5版本的array_column,并扩展取多列的功能
- 扩展CSplitterWnd——实现隐藏分割条的功能
- Yii安装EClientScript插件扩展实现css,js文件代码压缩合并加载功能
- scala使用implicit实现扩展已有功能
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- Android ListView功能扩展,实现高性能的瀑布流布局
- 使用JQuery库提供的扩展功能实现自定义方法
- jQuery插件FullCalendar日程表实现可扩展Google日历功能
- Linux里的防火墙(下):iptables的扩展模块——l7-filter的安装与功能实现