一个自定义登入的控件

// LoginUI.cs
// Developing Microsoft ASP.NET Server Controls and Components
// Copyright ?2002, Nikhil Kothari and Vandana Datye
//

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

namespace MSPress.ServerControls {
   
    // The LoginUI control renders a user interface that consists of
    // HTML elements (2 textboxes and a button) for entering
    // login information. LoginUI renders these elements
    // within an HTML table.
    // LoginUI uses the methods of the HtmlTextWriter object
    // to write the HTML elements and does not write out "raw" HTML.
    // LoginUI uses the HtmlTextWriterTag,
    // HtmlTextWriterAttribute, and HtmlTextWriterStyle 
    // enumerations for generating the names of HTML
    // tags, attributes, and styles.
    public class LoginUI : Control {
       
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(typeof(Color), ""),
        Description("The background color"),
        // The WebColorConverter is a type converter
        // that allows a string specifying an HTML color
        // to be converted to a System.Drawing.Color type.
        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 {
                if (value.Value < 0)
                    throw new ArgumentOutOfRangeException("value");
                ViewState["BorderWidth"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The text to display on the button")
        ]
        public string ButtonText {
            get {
                string s = (string)ViewState["ButtonText"];
                return (s == null) ? String.Empty : s;
            }
            set {
                ViewState["ButtonText"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The font")
        ]
        public string FontFamily {
            get {
                string s = (string)ViewState["FontFamily"];
                return (s == null) ? String.Empty : s;
            }
            set {
                ViewState["FontFamily"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(typeof(FontUnit), ""),
        Description("The size of the font")
        ]
        public FontUnit FontSize {
            get {
                object o = ViewState["FontSize"];
                return (o == null) ? FontUnit.Empty : (FontUnit)o;
            }
            set {
                ViewState["FontSize"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(false),
        Description("The weight of the font")
        ]
        public bool FontBold {
            get {
                object o = ViewState["FontBold"];
                return (o == null) ? false : (bool)o;
            }
            set {
                ViewState["FontBold"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The label for the Name textbox")
        ]
        public string NameLabel {
            get {
                string s = (string)ViewState["NameLabel"];
                return (s == null) ? String.Empty : s;
            }
            set {
                ViewState["NameLabel"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The label for the Password textbox")
        ]
        public string PasswordLabel {
            get {
                string s = (string)ViewState["PasswordLabel"];
                return (s == null) ? String.Empty : s;
            }
            set {
                ViewState["PasswordLabel"] = value;
            }
        }    
       
        // The id attribute to render on the HTML button tag.
        // The id attribute enables client-side script to access
        // an HTML element and must be unique for every
        // HTML element on the page.
        // We use the server control's ClientID property
        // as the HTML "id" because it is guaranteed to
        // be unique and does not have any invalid characters.
        // The UniqueID uses the invalid ":" character
        // as a separator, as we'll explain in Chapter 12.
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string ButtonID {
            get {
                return this.ClientID + "_Button";
            }
        }
       
        // The name attribute to render on the HTML button tag.
        // The name attribute is required on every HTML form
        // element and is used by the client to post name/value
        // pairs to the server when the user submits the form.
        // On the server, the name is used by the
        // page framework to process postback data and must be
        // unique for each control on the page.
        // To enable the page framework to handle postback events,
        // the name attribute of an HTML element that causes postback
        // (such as an HTML button) must equal the UniqueID of the control.
        //
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string ButtonName {
            get {
                return this.UniqueID + ":Button";  //UniqueID 处理页请求时自动生成此标识符
            }
        }

        // The id attribute to render on the HTML TextBox
        // for the pasword. We'll base this on the
        // ClientID to guarantee a unique id.
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string PasswordInputID {
            get {
                return this.ClientID + "_Password";   //获取由 ASP.NET 生成的服务器控件标识符 ClientID
            }
        }

        // The name attribute to render on the HTML TextBox
        // for the password. We'll base this on the
        // UniqueID to guarantee a unique name.
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string PasswordInputName {
            get {
                return this.UniqueID + ":Password";
            }
        }

        // The id attribute to render on the HTML TextBox
        // for the user name. We'll base this off of the
        // ClientID to guarantee a unique id.
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string UserNameInputID {
            get {
                return this.ClientID + "_UserName";
            }
        }

        // The name attribute to render on the HTML TextBox
        // for the user name. We'll base this off of the
        // UniqueID to guarantee a unique name.
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string UserNameInputName {
            get {
                return this.UniqueID + ":UserName";
            }
        }
             
        protected override void Render(HtmlTextWriter writer) {          
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "1");

            // You must pass in each style attribute by making a
            // separate call to AddStyleAttribute.
            // The HtmlTextWriter combines all the style
            // attributes into a single semicolon-delimited list,
            // which it assigns to the style HTML attribute
            // on the HTML tag.
            if (!BackColor.IsEmpty) {
                // The ColorTranformer.ToHtml method converts a System.Drawing.Color object
                // to a string that represents the color in HTML.
                writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(BackColor));  //将指定的 Color 结构翻译成 HTML 字符串颜色表示形式
            }
            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));
            }
            if (FontFamily != String.Empty) {
                writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, FontFamily);
            }
            if (!FontSize.IsEmpty) {
                writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, FontSize.ToString(CultureInfo.InvariantCulture));
            }
            if (FontBold == true) {
                writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "Bold");
            }
            // Render an id on the outer tag.
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            if (NameLabel != String.Empty){
                writer.Write(NameLabel);                 
            }
            writer.RenderEndTag();  // Td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
           
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "Text");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, UserNameInputName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, UserNameInputID);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, String.Empty);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);  
            ///  input 上面的是input的属性

            // Each call to RenderBeginTag must have a matching
            // call to RenderEndTag.
            writer.RenderEndTag();  // Input
            writer.RenderEndTag();  // Td
            writer.RenderEndTag();  // Tr
            /
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            if (PasswordLabel != String.Empty){
                writer.Write(PasswordLabel);
            }
            writer.RenderEndTag();  // Td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "Password");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, PasswordInputName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, PasswordInputID);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, String.Empty);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);   
            writer.RenderEndTag();  // Input
            writer.RenderEndTag();  // Td
            writer.RenderEndTag();  // Tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, ButtonName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ButtonID);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, ButtonText);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);   
            writer.RenderEndTag();  // Input
            writer.RenderEndTag();  // Td
            writer.RenderEndTag();  // Tr
           
            writer.RenderEndTag();  // Table
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值