冷风.NET

    ---默默無聞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

服务器控件之用户登录

Posted on 2004-10-19 14:30  冷风.net  阅读(1178)  评论(2编辑  收藏  举报

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;

namespace WebFrame
{
 
 public class UserLogin : Control, IPostBackDataHandler, IPostBackEventHandler
 {

  #region 定義類變量

  private static readonly object InClick = new object();
  private static readonly object OutClick = new object();
  private string _userName;
  private string _passWord;
  private int iButton = 0;

  #endregion

  #region 獲得控件的方法集

  public event EventHandler LoginIn
  {
   add
   {
    Events.AddHandler(InClick,value);
   }
   remove
   {
    Events.RemoveHandler(InClick,value);
   }
  }

  public event EventHandler LoginOut
  {
   add
   {
    Events.AddHandler(OutClick,value);
   }
   remove
   {
    Events.RemoveHandler(OutClick,value);
   }
  }

  #endregion

  #region 定義IPostBackDataHandler與IPostBackEventHandler的方法

  bool IPostBackDataHandler.LoadPostData(string postDataKey,NameValueCollection values)
  {
   _userName = values[this.UserInputName];
   _passWord = values[this.PassWordInputName];
   string buttonIn = values[this.InButtonName];
   bool buttonInClick = (buttonIn!=null)&&(buttonIn.Length!=0);
   if(buttonInClick)
   {
    iButton = 1;
    Page.RegisterRequiresRaiseEvent(this);
   }
   string buttonOut = values[this.OutButtonName];
   bool buttonOutClick = (buttonOut!=null)&&(buttonOut.Length!=0);
   if(buttonOutClick)
   {
    iButton = 2;
    Page.RegisterRequiresRaiseEvent(this);
   }
   return false;
  }

  void IPostBackDataHandler.RaisePostDataChangedEvent()
  {

  }

  void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  {
   OnLogin(EventArgs.Empty);
  }

  protected virtual void OnLogin(EventArgs e)
  {
   EventHandler clickButton = null;
   if(iButton==2)
    clickButton = (EventHandler)Events[OutClick];
   else if(iButton==1)
    clickButton = (EventHandler)Events[InClick];
   if(clickButton!=null)
    clickButton(this,e);
  }

  #endregion

  #region 將控件加入頁面時發生

  protected override void OnPreRender(EventArgs e)
  {
   base.OnPreRender(e);
   //將控件在頁面注冊為回傳時需要處理 
   Page.RegisterRequiresPostBack(this);
  }

  #endregion
  
  #region 定義控件屬性

  public virtual string UserLabelText
  {
   get
   {
    string s = (string)ViewState["UserLabelText"];
    return ((s==null)?"UserName":s);
   }
   set
   {
    ViewState["UserLabelText"] = value;
   }
  }

  public virtual string PassWordLabelText
  {
   get
   {
    string s = (string)ViewState["PassWordLabelText"];
    return ((s==null)?"PassWord":s);
   }
   set
   {
    ViewState["PassWordLabelText"] = value;
   }
  }

  public string UserInputId
  {
   get
   {
    return (this.UniqueID + "_UserName");
   }
  }

  public string UserInputName
  {
   get
   {
    return (this.UniqueID + ":UserName");
   }
  }

  public string UserName
  {
   get
   {
    return ((_userName==null)?String.Empty:_userName);
   }
  }

  public string PassWordInputId
  {
   get
   {
    return (this.UniqueID + "_PassWord");
   }
  }

  public string PassWordInputName
  {
   get
   {
    return (this.UniqueID + ":PassWord");
   }
  }

  public string PassWord
  {
   get
   {
    return ((_passWord==null)?String.Empty:_passWord);
   }
  }

  public string InButtonId
  {
   get
   {
    return (this.UniqueID + "_InButton");
   }
  }

  public string InButtonName
  {
   get
   {
    return (this.UniqueID + ":InButton");
   }
  }

  public virtual string InButtonText
  {
   get
   {
    string s = (string)ViewState["InButtonText"];
    return ((s==null)?"Login In":s);
   }
   set
   {
    ViewState["InButtonText"] = value;
   }
  }

  public string OutButtonId
  {
   get
   {
    return (this.UniqueID + "_OutButton");
   }
  }

  public string OutButtonName
  {
   get
   {
    return (this.UniqueID + ":OutButton");
   }
  }

  public virtual string OutButtonText
  {
   get
   {
    string s = (string)ViewState["OutButtonText"];
    return ((s==null)?"Login Out":s);
   }
   set
   {
    ViewState["OutButtonText"] = value;
   }
  }

  [
  Bindable(true),
  Category("Appearance"),
  DefaultValue(typeof(Color),""),
  Description("the background color"),
  TypeConverter(typeof(WebColorConverter))
  ]
  public Color BackColor
  {
   get
   {
    object o = ViewState["BackColor"];
    return (o==null)?Color.Empty:(Color)o;
   }
   set
   {
    ViewState["BackColor"] = value;
   }
  }

  [
  Bindable(true),
  Category("Appearance"),
  DefaultValue(typeof(Color),""),
  Description("the border color"),
  TypeConverter(typeof(WebColorConverter))
  ]
  public Color BorderColor
  {
   get
   {
    object o = ViewState["BorderColor"];
    return (o==null)?Color.Empty:(Color)o;
   }
   set
   {
    ViewState["BorderColor"] = value;
   }
  }

  [
  Bindable(true),
  Category("Appearance"),
  DefaultValue(BorderStyle.NotSet),
  Description("the border style"),
  ]
  public BorderStyle BorderStyle
  {
   get
   {
    object o = ViewState["BorderStyle"];
    return (o==null)?BorderStyle.None:(BorderStyle)o;
   }
   set
   {
    ViewState["BorderStyle"] = value;
   }
  }

  [
  Bindable(true),
  Category("Appearance"),
  DefaultValue(typeof(Unit),""),
  Description("the border width"),
  ]
  public Unit BorderWidth
  {
   get
   {
    object o = ViewState["BorderWidth"];
    return (o==null)?Unit.Empty:(Unit)o;
   }
   set
   {
    ViewState["BorderWidth"] = value;
   }
  }


  #endregion

  #region 控件輸出

  protected override void Render(HtmlTextWriter writer)
  {
   if(Page!=null)
    Page.VerifyRenderingInServerForm(this);
   writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"1");
   if(!BackColor.IsEmpty)
   {
    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor,ColorTranslator.ToHtml(BackColor));
   }
   if(!BorderColor.IsEmpty)
   {
    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor,ColorTranslator.ToHtml(BorderColor));
   }
   if((BorderStyle != BorderStyle.None) && (BorderStyle != BorderStyle.NotSet))
   {
    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle,BorderStyle.ToString());
   }
   if(!BorderWidth.IsEmpty)
   {
    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth,BorderWidth.ToString(CultureInfo.InvariantCulture));
   }
   writer.AddAttribute(HtmlTextWriterAttribute.Border,"1");
   writer.RenderBeginTag(HtmlTextWriterTag.Table);
   writer.RenderBeginTag(HtmlTextWriterTag.Tr);
   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.Write(this.UserLabelText);
   writer.RenderEndTag();
   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.AddAttribute(HtmlTextWriterAttribute.Type,"Text");
   writer.AddAttribute(HtmlTextWriterAttribute.Id,this.UserInputId);
   writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UserInputName);
   writer.AddAttribute(HtmlTextWriterAttribute.Value,String.Empty);
   writer.RenderBeginTag(HtmlTextWriterTag.Input);
   writer.RenderEndTag();
   writer.RenderEndTag();
   writer.RenderEndTag();
   writer.RenderBeginTag(HtmlTextWriterTag.Tr);
   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.Write(this.PassWordLabelText);
   writer.RenderEndTag();
   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.AddAttribute(HtmlTextWriterAttribute.Type,"PassWord");
   writer.AddAttribute(HtmlTextWriterAttribute.Id,this.PassWordInputId);
   writer.AddAttribute(HtmlTextWriterAttribute.Name,this.PassWordInputName);
   writer.AddAttribute(HtmlTextWriterAttribute.Value,String.Empty);
   writer.RenderBeginTag(HtmlTextWriterTag.Input);
   writer.RenderEndTag();
   writer.RenderEndTag();
   writer.RenderEndTag();
   writer.RenderBeginTag(HtmlTextWriterTag.Tr);
   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.AddAttribute(HtmlTextWriterAttribute.Type,"Submit");
   writer.AddAttribute(HtmlTextWriterAttribute.Id,this.InButtonId);
   writer.AddAttribute(HtmlTextWriterAttribute.Name,this.InButtonName);
   writer.AddAttribute(HtmlTextWriterAttribute.Value,this.InButtonText);
   writer.RenderBeginTag(HtmlTextWriterTag.Input);
   writer.RenderEndTag();
   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.AddAttribute(HtmlTextWriterAttribute.Type,"Submit");
   writer.AddAttribute(HtmlTextWriterAttribute.Id,this.OutButtonId);
   writer.AddAttribute(HtmlTextWriterAttribute.Name,this.OutButtonName);
   writer.AddAttribute(HtmlTextWriterAttribute.Value,this.OutButtonText);
   writer.RenderBeginTag(HtmlTextWriterTag.Input);
   writer.RenderEndTag();
   writer.RenderEndTag();
   writer.RenderEndTag();
  }

  #endregion
 }
}