控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

浅谈ASP.NET的内部机制(七)

作者:小洋 出处:博客园 2010年01月27日 阅读:

前言:本篇讲解与视图ViewState相关的知识,包括IStateManager,自定义转换器TypeConvert,以及和视图功能相同的控件状态。可以说本篇是对保存状态有关知识的总结,代码也详细的给出。。。。。。。。

     自从上次写了有关视图的文章后,收到了很多朋友的邮件,很感谢大家的支持:)很多朋友都说要求说说实践性更强的东西,所以本篇就进一步的来谈谈视图(不仅仅只是视图,而且在ASP.NET中的状态保存的话题)。首先希望大家对自定义控件有一定的了解。大家可以去参看我的控件开发系列。

     首先我看从一个简单的控件开发来谈起,我们在ASP.NET有Login的登录控件,我们现在就来自己来实现一个类似的控件,因为本篇主要讲述与视图有关的话题,所以关于事件冒泡等我们不提及,主要是为了使得代码简洁,易懂,集中讲述一个问题。

     实现自定义Login控件有很多的方式,我这里会带着大家一步步的做,首先我们继承WebControl来实现一个控件,然后我们再改进例子,我们来看看从WebControl继承的Login控件:

    

Code
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();

 

                }

            

        }
}
 

     了解自定义控件开发的朋友应该对上面的代码不陌生。控件最后呈现的效果基本和ASP.NET中的标准的控件外观差不多的。上面的控件缺少事件等。但是我们这里不关注这些。我们关注视图。

     大家可以看过我们实际上是再用ViewState来把保存控件的UserName,Password的信息,就是说如果我们仅仅只是用下面的代码来保存,如下:

    

Code
                private string userName;
                public string UserName
                {
                        get { return userName; }
                        set{userName =value ;}
                }
 

     我们控件的状态就会在回传的过程中丢失。

     我们现在就来具体的讲述视图在上面的控件是如何起作用的。

     假设我们开发的控件用在了一个已经部署好了的IIS中的网站的页面上了。当我们第一次请求这个页面,如:http://localhost/Test/Default.aspx。页面就开始被ASP.NET运行时开始解析编译,最后把结果发送给我们。

     起始ASP.NET运行时在解析页面的时候,就把Login控件的一些状态,如UserName属性等其他的设置信息序列化为一个字符串保存名为_VIEWSTATE的隐藏控件中,,然后把这些个序列化是通过一个转换器来进行的,如UserName属性的值类型是String,所以就用StringConvert来转换。其实在ASP.NET中有很多的内置的转换器,派生自TypeConvert,,如StringConvert,BoolenConvert。也就是说,如果我们在上面的控件中有sring,int,bool等的属性(如UserName就是string),ASP.NET自动将他们用对应的转换器转换;所以如果我们开发了自定义的一个类,如Person类,而且这个类还是上面Login控件的一个属性,  那么我们就必须开发自定的转换器来转换Person,我们自定义的转化器一定要继承自TypeConvert。
 
     然后就把转换后的结果发送客户端的浏览器中,当我们在浏览器中填写了用户名等信息后,我们点击按钮,整个页面就回传给了服务器,然后服务器就解析数据,同时也解析之前保存在_VIEWSTATE中的信息,而且用相应的转换器,来还原之前的属性,然后把这些属性赋值给在服务器端新生成的类的实例。
    
     用转换器是一种方法,但是还有另外的一种方法就是使得我们自定义的类,如之前提及的person类实现IStateManager接口,接口定义了一个属性,三个方法,其实实现起来也很简单,比实现自定义的转换器更加简单。而且实现起来格式也很固定的。我们就开改进之前的例子:
    
Code
  public class Person:IStateManager
        {

                public string AddressInfo
                {
                        get
                        {
                                return ViewState["AddressInfo"] != null ? (string)ViewState["AddressInfo"] : "AddressInfo";

                        }
                        set
                        {
                                ViewState["AddressInfo"] = value;
                        }

                }
            
                IStateManager 成员#region IStateManager 成员

                private bool _isTrackViewState;
                private StateBag _viewState;
             
                public StateBag ViewState
                {
                    get
                    {
                        if (_viewState == null)
                        {
                            _viewState = new StateBag(false);
                            if (_isTrackViewState)
                            {
                                ((IStateManager)_viewState).TrackViewState();
                            }
                        }
                        return _viewState;
                    }
                }

              
                public bool IsTrackingViewState
                {
                    get
                    {
                        return _isTrackViewState;
                    }
                }

                public void LoadViewState(object state)
                {
                    if (state != null)
                    {
                        ((IStateManager)ViewState).LoadViewState(state);
                    }
                }

                public object SaveViewState()
                {
                    if (this._viewState != null)
                    {
                        return ((IStateManager)_viewState).SaveViewState();
                    }
                    return null;
                }

                public void TrackViewState()
                {
                    this._isTrackViewState = true;
                    if (_viewState != null)
                    {
                        ((IStateManager)_viewState).TrackViewState();
                    }
                }

                #endregion

             

        }
 
     然后,我们来看看之前的Login控件,下面是完整的代码,不同的地方我用注释格开了:大家先扫过,代码我们下面会讲解的。
    
Code
 public class MyLogin : WebControl
        {
                //---------------------------------------------------
                public Person personInfo;
                public Person PersonInfo
                {
                        get
                        {
                                if (personInfo == null)
                                        personInfo = new Person();
                                ((IStateManager)personInfo).TrackViewState();
                        }
                        set
                        {
                                personInfo = value;
                        }
                }
                //--------------------------------------------
            
                protected override object SaveViewState()
                {
                        object[] states = new object[2];
                        states[0] = base.SaveViewState();
                        states[1] = ((IStateManager)PersonInfo).SaveViewState();
                } 

                protected override void LoadViewState(object savedState)
                {
                        object[] states = (object[])savedState;
                        base.LoadViewState(states[0]);
                        ((IStateManager)PersonInfo).SaveViewState(states[1]);

                }

                protected override void TrackViewState()
                {
                        base.TrackViewState();
                        ((IStateManager)PersonInfo).TrackViewState();
                }
             
                和之前的相同的部分#region 和之前的相同的部分

                属性#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();

 

                }
                #endregion

 

        }
    
     上面的代码首先就是多了一个Person类型的属性PersonInfo,然后就是重写了三个方法。其实重写的这个三个方法是很好理解的,而且代码也不是很难,首先我们看看
    
 

Code
     protected override object SaveViewState()
                {
                        object[] states = new object[2];
                        states[0] = base.SaveViewState();
                        states[1] = ((IStateManager)PersonInfo).SaveViewState();
                } 
         

     上面的代码就是保存视图状态,首先base.SaveViewState();返回的就是控件中其他的一些状态,如UserName等的状态,我们这里实际上就是把我们的属性PersonInfo的状态和控件的其他状态值加载在一起。如果还有其他的类型,如Employee类型的属性,我们就要在这个方法中写上:   

 

Code
 protected override object SaveViewState()
                {
                        object[] states = new object[3];
                        states[0] = base.SaveViewState();
                        states[1] = ((IStateManager)PersonInfo).SaveViewState();
                        states[2] = ((IStateManager)Employee).SaveViewState();
                } 

 

     其他的两个方法大家已经就可以看懂了。

     今天我们就说到这里,下篇我们谈谈转换器的问题。(本文由控件中国网转载)

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统