ajaxLoginCtrl4StudyezCom
来源:互联网 发布:怎么网络监听 编辑:程序博客网 时间:2024/06/05 00:41
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;namespace AjaxControl{ /// <summary> ///AjaxLogin 的摘要说明 /// </summary> public class AjaxLogin:CompositeControl { private AjaxLoginContainer _templateContainer; private ITemplate _loginTemplate; [PersistenceMode(PersistenceMode.InnerProperty ),TemplateContainer(typeof(AjaxLogin ))] public ITemplate LayoutTemplate { get { return this._loginTemplate; } set { this._loginTemplate =value; base.ChildControlsCreated = false; } } public AjaxLogin() { // //TODO: 在此处添加构造函数逻辑 // } protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "js4ajaxLoginControl")) { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "js4ajaxLoginControl", "<script language=/"javascript/" type=/"text/javascript/" src=/"ajaxLogin.js/" ></script>", false); } } protected override void CreateChildControls() { this.Controls.Clear(); this._templateContainer = new AjaxLoginContainer(this); ITemplate layoutTemplate = this.LayoutTemplate; if (layoutTemplate == null) { this._templateContainer.EnableViewState = false; this._templateContainer.EnableTheming = false; layoutTemplate = new AjaxLoginTemplate(this); } layoutTemplate.InstantiateIn(this._templateContainer); this.Controls.Add(this._templateContainer); } protected override void Render(HtmlTextWriter writer) { base.Render(writer); if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), this.UniqueID)) { Control uLv = this.FindControl("LoginView"); Control uLs = this.FindControl("LoginStatus"); Control uName = this.FindControl("UserName"); Control uPwd = this.FindControl("Password"); Control uLi = this.FindControl("LoginIn"); Control uLo = this.FindControl("LoginOut"); Control uCs = this.FindControl("CurrentUser"); Control uRm = this.FindControl("RememberMe"); string uRmId = ""; if (uRm == null) { uRmId = ""; } else { uRmId = uRm.ClientID; } AjaxLoginContainer ajc = this._templateContainer; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.AppendLine("<script language=/"javascript/" type=/"text/javascript/">"); sb.AppendFormat("var jx_{0}=new ajaxLogin(/"{0}/",/"{1}/",/"{2}/",/"{3}/",/"{4}/",/"{5}/",/"{6}/",/"{7}/",/"{8}/");", this.ClientID, uLv.ClientID, uName.ClientID, uPwd.ClientID, uLi.ClientID, uLs.ClientID, uCs.ClientID, uLo.ClientID,uRmId); sb.AppendFormat("jx_{0}.isOnline(jx_{0}.onCheckUserCompleted,jx_{0}.onHttpStatusError);", this.ClientID); sb.AppendFormat("</script>"); Page.ClientScript.RegisterStartupScript(this.GetType(), this.UniqueID, sb.ToString(), false); } } private sealed class AjaxLoginTemplate : ITemplate { private AjaxLogin _owner; public AjaxLoginTemplate(AjaxLogin owner) { this._owner = owner; } #region ITemplate 成员 public void InstantiateIn(Control container) { } #endregion private void CreateControls(AjaxLoginContainer loginContainer) { TextBox tb = new TextBox(); tb.ID = "UserName"; tb.TextMode = TextBoxMode.SingleLine; loginContainer.UserName = tb; TextBox pwd = new TextBox(); pwd.ID = "Password"; pwd.TextMode = TextBoxMode.Password; loginContainer.Password = pwd; Button lgIn = new Button(); lgIn.ID = "LoginIn"; lgIn.Text = "登录"; lgIn.UseSubmitBehavior = false; loginContainer.LoginInButton = lgIn; CheckBox cb = new CheckBox(); cb.ID = "RememberMe"; cb.Text = "RememberMe"; loginContainer.RememberMe = cb; Label lgN = new Label(); lgN.ID = "CurrentUser"; lgN.Text = ""; loginContainer.CurrentUser = lgN; Button lgOut = new Button(); lgOut.ID = "LoginOut"; lgOut.Text = "注销"; lgOut.UseSubmitBehavior = false; loginContainer.LoginOutButton = lgOut; Panel lgView = new Panel(); lgView.ID = "LoginView"; loginContainer.LoginView = lgView; Panel lgStatus = new Panel(); lgStatus.ID = "LoginStatus"; loginContainer.LoginStatus = lgStatus; } } internal sealed class AjaxLoginContainer : WebControl { public TextBox UserName; public TextBox Password; public CheckBox RememberMe; public Button LoginInButton; public Label CurrentUser; public Button LoginOutButton; public Panel LoginView; public Panel LoginStatus; private AjaxLogin _owner; public AjaxLoginContainer(AjaxLogin owner) { _owner = owner; } public override void RenderBeginTag(HtmlTextWriter writer) { } public override void RenderEndTag(HtmlTextWriter writer) { } } } }-----------------------Usage-------------------- <jx:AjaxLogin ID="lgView" runat="server"> <LayoutTemplate> <asp:Panel ID="LoginView" runat="server"> 用户名:<asp:TextBox ID="UserName" runat="server"></asp:TextBox>密码:<asp:TextBox ID="Password" TextMode="Password" runat="server" ></asp:TextBox> <asp:Button ID="LoginIn" Text="LoginIn" runat="server"/> </asp:Panel> <asp:Panel ID="LoginStatus" runat="server"> <asp:Label ID="CurrentUser" runat="server" ></asp:Label><asp:Button ID="LoginOut" Text="LoginOut" runat="server" /> </asp:Panel> </LayoutTemplate> </jx:AjaxLogin>