asp.net控件开发(二)--------简单属性
2008-05-15 09:30 Henry Cui 阅读(1868) 评论(13) 编辑 收藏 举报
我们开发出来的控件一般都需要跟使用控件的人编程人员交互,其中交互有两个途径:一个是属性还有就是事件。属性可能是简单的类型如:int、string等并不太复杂的类型,也有比较复杂的类型如:集合。
一、属性实现交互
我们还是沿用上面的例子来添加属性 PicShow示例:
public class PicShow : Control
{
private string _imgUrl;
//属性
public virtual string ImgUrl
{
get
{
return this._imgUrl;
}
set
{
this._imgUrl = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void Render(HtmlTextWriter writer)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100px");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
//Create Img Tag
writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ImgUrl);
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "80px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "80px");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
//End Of Div
writer.RenderEndTag();
}
}
上面的示例就是改变了上次的控件只能指定死了图片,这样图片的地址就可以由控件的使用人员自己去指定了。控件中定义了一个私有字段,通过属性ImgUrl来设置其值,因为我们允许重写其属性,所以定义成了virtual类型的。这里的属性定义跟一般类的定义是一样的,就不多做说明。
二、视图状态
底下我们在一个项目中应用此控件,但我们发现,这样的控件的属性的状态是没法维持的。也就是说,它永远只能维持我们初始给它的值。
举个例子:
我们通过按钮来改变其属性:
protected void btnChange_Click(object sender, EventArgs e)
{
if (this.psDemo.ImgUrl == "Images/1109508867.jpg")
this.psDemo.ImgUrl = "Images/bg.jpg";
else
this.psDemo.ImgUrl = "Images/1109508867.jpg";
}
运行后我们会发现,我们的图片只能改变一次。原因很简单,客户端发出http请求后,服务器端并没有帮我们去维持上一次的请求信息。
在有的时候我们必须要求其维持状态。状态分为两种:一个是视图状态,一个是控件状态。
视图状态在脚本中的表现为:保存到一个叫"_VIEWSTATE"的隐藏域中去,这样来实现保存上次的用户请求。视图状态是一个集合(ViewState)。
我们来继承一个PicShow
public class viewStatePic : PicShow
{
public override string ImgUrl
{
get
{
string strImgUrl = (string)ViewState["ImgUrl"];
return (strImgUrl == null) ? String.Empty : strImgUrl;
}
set
{
ViewState["ImgUrl"] = value;
}
}
}
这里我们只是把ImgUrl重写了,通过视图的状态来实现的,这下子我们在使用时就可以维持上次用户的请求。
三、控件状态
我们会发现如果用户禁止使用了视图状态的话(在<%Page%>中添加EnableViewState="false"),我们还是维持不了用户的上次请求,这时我们可以通过控件状态来永远地维持。
控件状态的维持一般分为三步:1.通过Page.RegisterRequiresControlState()方法来通知控件运行时所在的页面,把此控件维持成控件状态。
2.重写SaveControlState()方法来保存控件状态的值。
3.最后再将控件状态的值返回到属性中去。
控件状态示例:
public class ControlStatePic : PicShow
{
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override object SaveControlState()
{
return this.ImgUrl;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从保存的控件视图中取出来
protected override void LoadControlState(object savedState)
{
this.ImgUrl = savedState as string;
}
}
注意的一点,我们在重写OnInit方法时,我们只是需要要添加额外的功能,去注册控件状态,所以一定要调用base.OnInit(e)方法,来保存以前的初始化工作。
一、属性实现交互
我们还是沿用上面的例子来添加属性 PicShow示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
二、视图状态
底下我们在一个项目中应用此控件,但我们发现,这样的控件的属性的状态是没法维持的。也就是说,它永远只能维持我们初始给它的值。
举个例子:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ControlSatePic.aspx.cs" Inherits="ControlSatePic" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<%@ Register Assembly="HenllyeeConrol" Namespace="MyConrol1" TagPrefix="cc1" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:PicShow ID="" runat="server"
ImgUrl="Images/1109508867.jpg">
</cc1:PicShow>
<asp:Button ID="btnChange" Text="Change Picture" runat="server"
onclick="btnChange_Click" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们通过按钮来改变其属性:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
运行后我们会发现,我们的图片只能改变一次。原因很简单,客户端发出http请求后,服务器端并没有帮我们去维持上一次的请求信息。
在有的时候我们必须要求其维持状态。状态分为两种:一个是视图状态,一个是控件状态。
视图状态在脚本中的表现为:保存到一个叫"_VIEWSTATE"的隐藏域中去,这样来实现保存上次的用户请求。视图状态是一个集合(ViewState)。
我们来继承一个PicShow
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
三、控件状态
我们会发现如果用户禁止使用了视图状态的话(在<%Page%>中添加EnableViewState="false"),我们还是维持不了用户的上次请求,这时我们可以通过控件状态来永远地维持。
控件状态的维持一般分为三步:1.通过Page.RegisterRequiresControlState()方法来通知控件运行时所在的页面,把此控件维持成控件状态。
2.重写SaveControlState()方法来保存控件状态的值。
3.最后再将控件状态的值返回到属性中去。
控件状态示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)