ASP.NET基础
1.如何理解viewstate?
参考What is viewstate in asp.net - Part 3
Web应用是基于HTTP,HTTP协议是无状态协议,它并不保存请求的状态,所以可以使用viewstate
来保存不同请求间的状态。
ClicksCount = (int)ViewState["Clicks"] + 1;
ViewState
数据序列化为base64编码的字符串,保存在隐藏域__ViewState
中
ASP.NET
的服务器控件会保存状态,而HTML控件则不会。HTML控件可以通过添加runat="server"
变为ASP.NET
服务器控件
2.web应用生命周期中的事件
参考Events in the life cycle of a web application - Part 4
如果想要在多个Web表单中提供数据,在ASP.NET中有如下的方式:
1. Query Strings
2. Cookies
3. Session State
4. Application State
注意Session State 与Application State、ViewState的区别,可参考Difference between ViewState, Session State and Application State in asp.net - Part 5
页面的生命周期ASP.NET Page Life Cycle Events - Part 6
当一个webform的请求到来时,事件执行的顺序如下:
1. Web Application creates an instance of the requested webform.
2. Processes the events of the webform.
3. Generates the HTML, and sends the HTML back to the requested client.
4. The webform gets destroyed and removed from the memory.
3.服务器控件的事件
参考ASP.NET Server control events - Part 7
控件的事件类型可分为3类:
1.Postback事件-提交webpage,直接到服务器处理。如button的click事件就是Postback事件
2.Cached事件-这些事件保存在page的viewstate中待处理,直到一个postback事件发生。例如,TextBox
的TextChanged
事件,DropDownList
的SelectedIndexChanged
事件。Cached事件可被转化为Postback事件,通过设置AutoPostBack
为true
3.Validation
事件-在page被post back到server之前,在客户端发生
4.对IsPostBack
的理解
参考IsPostBack in asp.net - Part 8
IsPostBack
is a Page level property, that can be used to determine whether the page is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.
5.Page的生命周期
参考ASP.NET Page Life Cycle Events - Part 6
Page_PreInit
在页面初始化事件之前发生。在此阶段,设置IsPostBack
、IsCallback
、IsCrossPagePostBack
属性。这个事件中可以动态的设置master page和web应用的主题。PreInit
在使用动态控件时被广泛使用。Page_Init
使用此事件来读取或初始化控件属性。Page_InitComplete
Page_PreLoad
Page_Load
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Page_Unload
ASP.NET控件
1.TextBox控件
参考ASP.NET TextBox Control - Part 10
属性:
- AutoPostBack- By default, the
TextChanged
event of aTextBox
control is cached in theviewstate
, and is executed when the webform is submitted thru apostback
by clicking the button control. If you want to change this behaviour, and post the webformimmediately
when the Text is changed, setAutoPostBack
totrue
. Setting this property to true, will convert thecached
event into apostback
event.
方法
- Focus()
事件:
- TextChanged
2.RadioButton控件
属性
- Checked表示是否选中
- GroupName默认的,每个Radio不是互斥的。如果想要互斥,对