bugsbuddy的专栏

“托管”的肉体,“抽象”的灵魂

实现了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

    }

}

阅读更多
上一篇一个关于复合型枚举标志位的功能类
下一篇继承System.Web.UI.Page的页面基类
想对作者说点什么? 我来说一句

.net中textbox下划线

2010年05月14日 50KB 下载

没有更多推荐了,返回首页

关闭
关闭