实现了IPostBackEventHandler接口的TextBox

    using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace PEONormalControl
{
   
/// <summary>
   
/// 回传事件参数类型
   
/// </summary>   

    public class EventTextBoxEventArgs:EventArgs
   
{
       
private string _ToAppend;
        EventTextBoxEventArgs()
       
{           
           
        }
       
       
public EventTextBoxEventArgs(String ArgText)
       
{
           
this._ToAppend=ArgText;           
        }

       
/// <summary>
       
/// 回传事件参数
       
/// </summary>

        public String ArgText
       
{
           
get{return _ToAppend;}
           
set{_ToAppend =value;}
        }

    }

   
/// <summary>
   
/// 从TextBox继承,实现了IPostBackEventHandler接口
   
/// </summary>

    [DefaultProperty("Text"),
    ToolboxData(
"<{0}:EventTextBox runat=server></{0}:EventTextBox>")]
   
public class EventTextBox : System.Web.UI.WebControls.TextBox,IPostBackEventHandler
   
{
       
//private string text;
        private static readonly object objEventChangedKey;
       
public delegate void EventChangedHandler(object sender,EventTextBoxEventArgs e);

        [Bindable(
false),
            Category(
"Appearance"),
            DefaultValue(
"")]
       
public string PrefixText
       
{
           
get
           
{    if    (ViewState["PrefixText"] == null)
                   
return "";
               
else
               
return ViewState["PrefixText"].ToString() ;
            }


           
set
           
{
                ViewState[
"PrefixText"] = value;
            }

        }
       
       
/// <summary>
       
/// 引起回传的事件名称
       
/// </summary>

        [Bindable(false),
            Category(
"Behavior"),
            DefaultValue(
""),Description("引起回传的事件名称")]           
       
public string EventName
       
{
           
get
           
{    if    (ViewState["EventName"] == null)
                   
return "";
               
else
               
return ViewState["EventName"].ToString() ;
            }


           
set
           
{
                ViewState[
"EventName"] = value;
            }

        }

       
/// <summary>
       
/// 回传事件参数
       
/// </summary>

        [Bindable(false),
            Category(
"Behavior"),
            DefaultValue(
""),Description("回传事件参数")]
       
public string EventArgText
       
{
           
get
           
{    if    (ViewState["EventArgText"] == null)
                   
return "";
               
else
               
return ViewState["EventArgText"].ToString() ;
            }


           
set
           
{
                ViewState[
"EventArgText"] = value;
            }

        }

       
/// <summary>
       
/// 用户自定义的回传函数
       
/// </summary>

        [Bindable(false),
            Category(
"Behavior"),
            DefaultValue(
""),Description("用户自定义的回传函数,为空则使用标准的回传函数")]
       
public String UserPostBackFunction
       
{
           
get
           
{    if    (ViewState["UserPostBackFunction"] == null)
                   
return "";
               
else
               
return ViewState["UserPostBackFunction"].ToString() ;
            }


           
set
           
{
                ViewState[
"UserPostBackFunction"] = value;
            }

        }

       
protected virtual void OnEventChanged(EventTextBoxEventArgs e)
       
{
            EventChangedHandler handler1
= (EventChangedHandler) base.Events[objEventChangedKey];
           
if (handler1 != null)
           
{
                handler1(
this, e);       
            }

        }

       
public void RemoveStateView(String strKey)
       
{
           
this.ViewState.Remove(strKey);
        }

        [Description(
"EventTextBox_OnEventChanged"), Category("Action")]
       
public event EventChangedHandler EventChanged
       
{
            add
           
{
               
base.Events.AddHandler(objEventChangedKey, value);
            }

            remove
           
{
               
base.Events.RemoveHandler(objEventChangedKey, value);
            }

        }
       
       
       
/// <summary>
       
/// 将此控件呈现给指定的输出参数。
       
/// </summary>
       
/// <param name="output"> 要写出到的 HTML 编写器 </param>

        protected override void Render(HtmlTextWriter output)
       
{
            EventChangedHandler handler1
= (EventChangedHandler) base.Events[objEventChangedKey];
           
if (handler1 != null && EventName != String.Empty)
           
{
               
base.Attributes[EventName]=UserPostBackFunction==""? Page.GetPostBackClientEvent(this,EventArgText):UserPostBackFunction;
               
if(PrefixText != "" && !base.Text.StartsWith(PrefixText)) base.Text = PrefixText + base.Text;
            }

           
base.Render(output);
           
        }

       
//增加自己想渲染的属性
        protected override void AddAttributesToRender(HtmlTextWriter writer)
       
{
           
//writer.AddAttribute(HtmlTextWriterAttribute.Title,"PEOControl");
           
           
base.AddAttributesToRender (writer);
        }


   
       
#region IPostBackEventHandler 成员

       
public void RaisePostBackEvent(string eventArgument)
       
{
           
// TODO:  添加RaisePostBackEvent 实现
            EventTextBoxEventArgs e = new EventTextBoxEventArgs(eventArgument);
            OnEventChanged(e);
        }


       
#endregion

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值