扩展Label控件(1) – 实现回发(Postback)功能 [源码下载]
\n
Label控件既强大又好用。为了让它更强大、更好用,我们来写一个继承自Label的控件。
介绍
扩展Label控件:
通过注册HiddenField控件,使Label控件支持回发(Postback)功能
使用方法(设置属性):
EnablePostback – 是否启用Label控件的回发(Postback)
HiddenFieldPostfix – 使Label支持回发(Postback)的隐藏控件的后缀名
\n
关键代码
ScriptLibrary.js
//—————————-
// http://webabcd.cnblogs.com/
//—————————-
\n
function yy_sl_copyTextToHiddenField(source, destination)
{
///
\n
document.getElementById(destination).value = document.getElementById(source).innerHTML;
}
SmartLabel.cs
using System;
using System.Collections.Generic;
using System.Text;
\n
using System.Web.UI.WebControls;
using System.Web.UI;
\n
[assembly: System.Web.UI.WebResource("YYControls.SmartLabel.Resources.ScriptLibrary.js", "text/javascript")]
\n
namespace YYControls
{
/**////
/// SmartLabel类,继承自DropDownList
///
[ToolboxData(@"<{0}:SmartLabel runat=\'server\'>")]
[System.Drawing.ToolboxBitmap(typeof(YYControls.Resources.Icon), "SmartLabel.bmp")]
public partial class SmartLabel : Label
{
/**////
/// 构造函数
///
public SmartLabel()
{
\n
}
\n
/**////
/// OnPreRender
///
/// e
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
\n
// 实现Label控件的回发(Postback)功能
ImplementPostback();
}
}
}
\n
Property.cs
using System;
using System.Collections.Generic;
using System.Text;
\n
using System.ComponentModel;
using System.Web.UI;
\n
namespace YYControls
{
/**////
/// SmartLabel类的属性部分
///
public partial class SmartLabel
{
/**////
/// 使Label支持回发(Postback)的隐藏控件的后缀名
///
[
Browsable(true),
Description("使Label支持回发(Postback)的隐藏控件的后缀名"),
Category("扩展"),
DefaultValue("EnablePostback")
]
public virtual string HiddenFieldPostfix
{
get
{
string s = (string)ViewState["HiddenFieldPostfix"];
\n
return (s == null) ? “EnablePostback” : s;
}
set
{
ViewState["HiddenFieldPostfix"] = value;
}
}
\n
/**////
/// 是否启用Label控件的回发(Postback)
///
[
Browsable(true),
Description("是否启用Label控件的回发(Postback)"),
Category("扩展"),
DefaultValue(false)
]
public virtual bool EnablePostback
{
get
{
bool? b = (bool?)ViewState["EnablePostback"];
\n
return (b == null) ? false : (bool)b;
}
\n
set
{
ViewState["EnablePostback"] = value;
}
}
}
}
\n
EnablePostback.cs
using System;
using System.Collections.Generic;
using System.Text;
\n
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
\n
namespace YYControls
{
/**////
/// SmartLabel类的属性部分
///
public partial class SmartLabel
{
/**////
/// 实现Label控件的回发(Postback)功能
///
private void ImplementPostback()
{
if (this.EnablePostback)
{
// 使Label支持回发(Postback)的隐藏控件的ID
string hiddenFieldId = string.Concat(this.ClientID, “_”, HiddenFieldPostfix);
\n
// 注册隐藏控件
Page.ClientScript.RegisterHiddenField(hiddenFieldId, “”);
\n
// 注册客户端脚本
this.Page.ClientScript.RegisterClientScriptResource(this.GetType(),
“YYControls.SmartLabel.Resources.ScriptLibrary.js”);
\n
// 表单提交前将Label控件的的值赋给隐藏控件
this.Page.ClientScript.RegisterOnSubmitStatement(this.GetType(),
“yy_sl_enablePostback”,
string.Format(“yy_sl_copyTextToHiddenField(‘{0}’, ‘{1}’)”,
this.ClientID,
hiddenFieldId));
}
}
\n
/**////
/// 获取或设置 YYControls.SmartLabel 控件的文本内容
///
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 来源:http://www.cnblogs.com/webabcd