学习ASP.NET2.0笔记记录。三年多没有写代码啦,呵呵,认认真真学习一下,记录
代码
//Label控件的AssociatedControlID 属性将帮助辅助设备正确的吧标签和表单字段关联起来
//同时另一个好处时,单击标签时,自动把表单的焦点设置为关联的表单输入框。
//为Label设置了AssociatedControlID 属性后,Label控件显示的是HTML<label>标签,而不是HTML 的 SPAN 标签
this.Label1.AssociatedControlID = "TextBox1";
//同时另一个好处时,单击标签时,自动把表单的焦点设置为关联的表单输入框。
//为Label设置了AssociatedControlID 属性后,Label控件显示的是HTML<label>标签,而不是HTML 的 SPAN 标签
this.Label1.AssociatedControlID = "TextBox1";
代码
// Literal控件用于在游览器中显示文本或HTML内容,但是Literal控件并不把它的内容呈现在一个span标签中
// 所以Literal控件不支持任何由span标签支持的格式化属性,比如Literal不支持cssclass属性,也不支持backcolorshux
// Literal控件只支持一个Label不支持的属性,Mode属性
// PassThrough :显示控件的内容而不进行任何编码 示例中显示为 <b>黑色的字</b>
// Encode : 在编码HTML内容后再显示控件的内容
// Transform :在清除请求设备不支持的标记后再显示控件的内容
this.Literal1.Mode = LiteralMode.Encode;
this.Literal2.Mode = LiteralMode.PassThrough;
this.Literal1.Text="<b>黑色的字</b>"; //显示为 <b>黑色的字</b>
this.Literal2.Text = "<b>黑色的字</b>"; //黑色的字
// 所以Literal控件不支持任何由span标签支持的格式化属性,比如Literal不支持cssclass属性,也不支持backcolorshux
// Literal控件只支持一个Label不支持的属性,Mode属性
// PassThrough :显示控件的内容而不进行任何编码 示例中显示为 <b>黑色的字</b>
// Encode : 在编码HTML内容后再显示控件的内容
// Transform :在清除请求设备不支持的标记后再显示控件的内容
this.Literal1.Mode = LiteralMode.Encode;
this.Literal2.Mode = LiteralMode.PassThrough;
this.Literal1.Text="<b>黑色的字</b>"; //显示为 <b>黑色的字</b>
this.Literal2.Text = "<b>黑色的字</b>"; //黑色的字
代码
/*
PreviousPage页面的示例
*
* <%@ PreviousPageType VirtualPath="~/Default.aspx" %>
需要设置页面的<@PreviousPageType%>指令 如果没有这个指令,PreviousPage属性会把前面页面作为通用的Page类的实例返回
* 类似的对与以后的页面传值可以通过这个传递,不知道是否能传递空间的 datasource 这样的属性。
*
* 跨页面提交的时候可以使用以下两种方法一种是弱类型 untyped , 没有指定详细的类型,PreviousPage属性返回的是Page类,通过 FindControl来查找。
* 第二种是强类型 指定<%@ PreviousPageType VirtualPath="~/Default.aspx" %> ,因为PreviousPage 返回的是具体的页面类型,所以可以直接引用公开的方法等
*/
弱类型使用
if (PreviousPage != null)
{
Label lbl;
lbl = (Label)PreviousPage.FindControl("Label1");
}
强类型使用
本页添加
PreviousPage页面的示例
*
* <%@ PreviousPageType VirtualPath="~/Default.aspx" %>
需要设置页面的<@PreviousPageType%>指令 如果没有这个指令,PreviousPage属性会把前面页面作为通用的Page类的实例返回
* 类似的对与以后的页面传值可以通过这个传递,不知道是否能传递空间的 datasource 这样的属性。
*
* 跨页面提交的时候可以使用以下两种方法一种是弱类型 untyped , 没有指定详细的类型,PreviousPage属性返回的是Page类,通过 FindControl来查找。
* 第二种是强类型 指定<%@ PreviousPageType VirtualPath="~/Default.aspx" %> ,因为PreviousPage 返回的是具体的页面类型,所以可以直接引用公开的方法等
*/
弱类型使用
if (PreviousPage != null)
{
Label lbl;
lbl = (Label)PreviousPage.FindControl("Label1");
}
强类型使用
本页添加
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
//default.aspx代码
fstrSearch = "Default.aspx页面的strSearch";
public string strSearch
{
get
{
return fstrSearch;
}
}
本页中直接调用 PreviousPage.strSearch
//default.aspx代码
fstrSearch = "Default.aspx页面的strSearch";
public string strSearch
{
get
{
return fstrSearch;
}
}
本页中直接调用 PreviousPage.strSearch
XmlDataSource示例
代码
XmlDataSource xmlDS = new XmlDataSource();
xmlDS.DataFile = "~/xmlfile1.xml";
xmlDS.XPath = "/movies/*";
this.Menu1.DataSource = xmlDS;
this.TreeView1.DataSource = xmlDS;
this.Menu1.DataBind();
this.TreeView1.DataBind();
xmlDS.DataFile = "~/xmlfile1.xml";
xmlDS.XPath = "/movies/*";
this.Menu1.DataSource = xmlDS;
this.TreeView1.DataSource = xmlDS;
this.Menu1.DataBind();
this.TreeView1.DataBind();
BulletedList示例
代码
/*
Circle 项目符号样式为空心圆。
CustomImage 项目符号样式为自定义图像。
Disc 项目符号样式为实心圆。
LowerAlpha 项目符号样式为小写字母(a、b、c...)。
LowerRoman 项目符号样式为小写罗马数字(i、ii、iii...)。
NotSet 不设置项目符号样式。呈现 BulletedList 控件的浏览器将决定要显示的项目符号样式。
Numbered 项目符号样式为数字(1、2、3...)。
Square 项目符号样式为实心四方形。
UpperAlpha 项目符号样式为大写字母(A、B、C...)。
UpperRoman 项目符号样式为大写罗马数字(I、II、III...)。
this.BulletedList1.BulletStyle = BulletStyle.Numbered;
*/
//显示为自定义图形
this.BulletedList1.BulletStyle = BulletStyle.CustomImage;
this.BulletedList1.BulletImageUrl = "~/closebox.gif";
this.BulletedList1.DisplayMode = BulletedListDisplayMode.HyperLink;
this.BulletedList1.Target = "_blank";
this.BulletedList1.DataTextField = "dmsm";
this.BulletedList1.DataSource = ds;
this.BulletedList1.DataBind();
Circle 项目符号样式为空心圆。
CustomImage 项目符号样式为自定义图像。
Disc 项目符号样式为实心圆。
LowerAlpha 项目符号样式为小写字母(a、b、c...)。
LowerRoman 项目符号样式为小写罗马数字(i、ii、iii...)。
NotSet 不设置项目符号样式。呈现 BulletedList 控件的浏览器将决定要显示的项目符号样式。
Numbered 项目符号样式为数字(1、2、3...)。
Square 项目符号样式为实心四方形。
UpperAlpha 项目符号样式为大写字母(A、B、C...)。
UpperRoman 项目符号样式为大写罗马数字(I、II、III...)。
this.BulletedList1.BulletStyle = BulletStyle.Numbered;
*/
//显示为自定义图形
this.BulletedList1.BulletStyle = BulletStyle.CustomImage;
this.BulletedList1.BulletImageUrl = "~/closebox.gif";
this.BulletedList1.DisplayMode = BulletedListDisplayMode.HyperLink;
this.BulletedList1.Target = "_blank";
this.BulletedList1.DataTextField = "dmsm";
this.BulletedList1.DataSource = ds;
this.BulletedList1.DataBind();
对于后台手动设置BulletedList控件的DataSourceID,SqlDataSource
得加上this.Controls.Add(dSource); 否则页面会提示没有找到什么DataSourceId 为"dSource"的控件
//手动绑定BulletedList中的超链接的值
protected void BulletedList1_DataBound(object sender, EventArgs e)
{
for (int i=0;i< BulletedList1.Items.Count ;i++)
{
BulletedList1.Items[i].Value =string.Format("http://localhost/id=%7B0%7D%22,BulletedList1.Items[i].Value);
}
}
protected void BulletedList1_DataBound(object sender, EventArgs e)
{
for (int i=0;i< BulletedList1.Items.Count ;i++)
{
BulletedList1.Items[i].Value =string.Format("http://localhost/id=%7B0%7D%22,BulletedList1.Items[i].Value);
}
}
DropDownList
代码
/*
* 加密web.config
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -pef connectionStrings D:\Project\WebSites\ASPNET2005Demo
正在加密配置节...
成功!
*/
using Oracle.DataAccess.Client;
OracleConnection oraConn = new OracleConnection();
ConnectionStringSettings cs ;
cs = ConfigurationManager.ConnectionStrings["OracleConn"];
oraConn.ConnectionString = cs.ConnectionString;
//DropDownList 控件
//AppendDataBoundItems = true | false 获取或设置一个值,该值指示是否在绑定数据之前清除列表项。
//AppendDataBoundItems 属性使您可以在执行数据绑定之前将项添加到 ListControl 对象中。
//执行数据绑定之后,项集合中包含数据源中的项以及以前添加的项。此属性的值存储在视图状态中。
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "dmsm";
this.DropDownList1.DataValueField = "dmlb";
this.DropDownList1.AppendDataBoundItems = true;
this.DropDownList1.Items.Add(new ListItem("请选择号牌", String.Empty));
this.DropDownList1.DataBind();
aspx文件中的数据绑定
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<!--
1.* 中,数据绑定必须使用DataBinder.Eval方法,2.0中可以使用简化的Eval方法
pub 为页面中一个public property
-->
<%#Eval("dmsm") %> GUID:<%# Pub %> <%#DataBinder.Eval(Container.DataItem,"sysdate","{0:D}") %> <br />
<a href ="<%#DataBinder.Eval(Container.DataItem,"dmsm")%>"><%#Eval("dmsm") %></a>
</ItemTemplate>
</asp:Repeater>
public string Pub
{
get
{
return System.Guid.NewGuid().ToString();
}
}
//DropDownList控件呈现一个HTML的<select>标签,这个标签产生一个问题,他的z-index是无穷大的,换句话说 在网页上,不能再将其他的对象(例如绝对定位标签 div ) 放在DropDownList控件的上面
* 加密web.config
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -pef connectionStrings D:\Project\WebSites\ASPNET2005Demo
正在加密配置节...
成功!
*/
using Oracle.DataAccess.Client;
OracleConnection oraConn = new OracleConnection();
ConnectionStringSettings cs ;
cs = ConfigurationManager.ConnectionStrings["OracleConn"];
oraConn.ConnectionString = cs.ConnectionString;
//DropDownList 控件
//AppendDataBoundItems = true | false 获取或设置一个值,该值指示是否在绑定数据之前清除列表项。
//AppendDataBoundItems 属性使您可以在执行数据绑定之前将项添加到 ListControl 对象中。
//执行数据绑定之后,项集合中包含数据源中的项以及以前添加的项。此属性的值存储在视图状态中。
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "dmsm";
this.DropDownList1.DataValueField = "dmlb";
this.DropDownList1.AppendDataBoundItems = true;
this.DropDownList1.Items.Add(new ListItem("请选择号牌", String.Empty));
this.DropDownList1.DataBind();
aspx文件中的数据绑定
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<!--
1.* 中,数据绑定必须使用DataBinder.Eval方法,2.0中可以使用简化的Eval方法
pub 为页面中一个public property
-->
<%#Eval("dmsm") %> GUID:<%# Pub %> <%#DataBinder.Eval(Container.DataItem,"sysdate","{0:D}") %> <br />
<a href ="<%#DataBinder.Eval(Container.DataItem,"dmsm")%>"><%#Eval("dmsm") %></a>
</ItemTemplate>
</asp:Repeater>
public string Pub
{
get
{
return System.Guid.NewGuid().ToString();
}
}
//DropDownList控件呈现一个HTML的<select>标签,这个标签产生一个问题,他的z-index是无穷大的,换句话说 在网页上,不能再将其他的对象(例如绝对定位标签 div ) 放在DropDownList控件的上面

浙公网安备 33010602011771号