前言:之前幾篇文章寫了有關視圖的知識,也講述了如何實現IStateManager接口。本篇將講解如何實 現自定義轉換器。當然了,有大量的代碼演示!本篇算是有關視圖機制的終結篇,還會講解有關 ControlStatel控件狀態的實現。
有個小插曲,其實之前我已經把文章寫了一大半,不知道怎麼回事,IE出了問題,自動關閉了,真是 心痛阿,現在得重寫一遍。我想每個寫博文的朋友都有這樣的經歷吧,寫一篇文章真的很不容易!
在上篇中,我們講解了如何在一個自定義控件中保存自定義類的屬性,我現在再次把代碼貼出來,免 得大家去翻頁。
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 CustomComponents
{
/**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//// <summary>
///MyLogin 的摘要說明
/// </summary>
public class MyLogin:WebControl
{
屬性屬性屬性屬性#region 屬性
public string UserName
{
get
{
return ViewState["UserName"] != null ? (string)ViewState["UserName"] : "UserName";
}
set
{
ViewState["UserName"] = value;
}
}
public string UserPassword
{
get
{
return ViewState ["UserPassword"] != null ? (string)ViewState["UserPassword"] : "UserPassword";
}
set
{
ViewState["UserPassword"] = value;
}
}
#endregion
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Table;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
//顯示用戶名
writer.RenderBeginTag (HtmlTextWriterTag.Tr);
writer.RenderBeginTag (HtmlTextWriterTag.Td);
writer.Write(UserName);
writer.RenderEndTag();
writer.RenderBeginTag (HtmlTextWriterTag.Td);
writer.AddAttribute (HtmlTextWriterAttribute.Id, "txtUserName");
writer.AddAttribute (HtmlTextWriterAttribute.Type, "text");
writer.RenderBeginTag (HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
//顯示用戶密碼
writer.RenderBeginTag (HtmlTextWriterTag.Tr);
writer.RenderBeginTag (HtmlTextWriterTag.Td);
writer.Write(UserPassword);
writer.RenderEndTag();
writer.RenderBeginTag (HtmlTextWriterTag.Td);
writer.AddAttribute (HtmlTextWriterAttribute.Id, "txtPassword");
writer.AddAttribute (HtmlTextWriterAttribute.Type, "text");
writer.RenderBeginTag (HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
//顯示登錄按鈕
writer.RenderBeginTag (HtmlTextWriterTag.Tr);
writer.AddAttribute (HtmlTextWriterAttribute.Colspan, "2");
writer.AddAttribute (HtmlTextWriterAttribute.Align, "center");
writer.RenderBeginTag (HtmlTextWriterTag.Td);
writer.AddAttribute (HtmlTextWriterAttribute.Id, "btnSubmit");
writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
writer.AddAttribute (HtmlTextWriterAttribute.Value, "Login");
writer.RenderBeginTag (HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
}