擴展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;
                                }
                        }
                }
        }
}
 
相關文章
相關標籤/搜索