2012.4.20
新建带母版页 CookieAndSession.aspx
添加控件:
<asp:Literal ID="ltlCookie" runat="server"/>
<asp:Button runat="server" ID="btnCookie"/>
<br />
<asp:Literal ID="ltlSession" runat="server"/>
<asp:Button runat="server" ID="Session"/>
--修改
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Literal ID="ltlCookie" runat="server" Text=""/>
<br />
<asp:Button runat="server" ID="btnCookie" Text="添加Cookie"/>
<br />
<asp:Literal ID="ltlSession" runat="server" Text=""/>
<br />
<asp:Button runat="server" ID="btnSession" Text="添加Session"/>
</asp:Content>
--
双击添加button 事件 添加以下代码
protected void Page_Load(object sender, EventArgs e)
{
var cookie = Request.Cookies["name"];
if (cookie != null && cookie.Value != null)
{
ltlCookie.Text = cookie.Value;
}
else
{
ltlCookie.Text = "Cookies[\"name\"] 不存在";
}
var age = Session["age"];
if(age != null)
{
ltlSession.Text = age.ToString();
}
else
{
ltlSession.Text = "Session[\"age\"]不存在";
}
}
protected void btnCookie_Click(object sender, EventArgs e)
{
//删除重名 Cookie
Request.Cookies.Remove("name");
//添加 Cookie
Response.AppendCookie(new HttpCookie("name","sa"));
//重定向页面,刷新
Response.Redirect("CookieAndSession.aspx");
}
--添加 Session 的代码
protected void Session_Click(object sender, EventArgs e)
{
//添加 Session
Session["age"] = 30;
Session.Timeout = 1000;
//重定向页面,刷新
Response.Redirect("CookieAndSession.aspx");
}
-------------------------------------------------------------
添加 objectDataSouceDemo.aspx
--
objectDataSouceDemo.aspx.cs
//新建类
public class Fruit
{
public string Name { get; set; }
public double Price{ get; set; }
public Fruit(string name, double price)
{ //
Name = name;
Price = price;
}
}
//数据库 访问层
// BLL 业务逻辑层
public class FruitBLL
{
public List<Fruit> GetFruits()
{
var fruits = new List<Fruit>();
fruits.Add(new Fruit("苹果", 400));
fruits.Add(new Fruit("香蕉", 500));
fruits.Add(new Fruit("葡萄", 450));
fruits.Add(new Fruit("橙子", 300));
fruits.Add(new Fruit("火龙果", 900));
fruits.Add(new Fruit("菠萝", 100));
return fruits;
}
}
//表现层 Web 页面层
---------------------------------------------
业务逻辑层
数据库
界面(表现层 Web 页面层)
--配置 object
<asp:ObjectDataSource runat="server" ID="odsFruits" ></asp:ObjectDataSource>
配置 选择业务对象 NewWebApp.objectDataSourceDemo+FriotBLL
SELECT - GetFruits(),返回 List<Fruit> 完成
代码自动添加:
<asp:ObjectDataSource runat="server" ID="odsFruits" SelectMethod="GetFruits"
TypeName="NewWebApp.objectDataSouceDemo+FruitBLL" ></asp:ObjectDataSource>
--
添加
<asp:GridView runat="server" ID="gvwFruit" DataSourceID="odsFruits" />
自动套用格式
--
在项目中 添加类 EmployDataReader.cs
//using System.Data.SqlClient;
public SqlDataReader GetEmploy()
{
//using System.Configuration;
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["courseConnectionString"].ConnectionString);
var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT 员工号码,姓名,性别,地址,部门 From 章立民研究室";
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
--
GridView 选择数据源 DataSourceID="odsFruits"
ObjectDataSource 配置数据源 选择业务对象 NewWebApp.objectDataSouceDemo+FruitBLL SELECT
GridView 选择数据源 DataSourceID="odsEmploy"
ObjectDataSource 配置数据源 选择业务对象 TypeName="NewWebApp.EmployDataReader" SELECT
--
源代码中
<asp:GridView runat="server" ID="gvwFruit" DataSourceID="odsFruits"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource runat="server" ID="odsFruits" SelectMethod="GetFruits"
TypeName="NewWebApp.objectDataSouceDemo+FruitBLL" ></asp:ObjectDataSource>
<asp:Panel runat ="server" ID="pnlDataReader" GroupingText ="数据库访问">
<asp:GridView runat="server" ID="gvwEmploy" DataSourceID="odsEmploy">
</asp:GridView>
<asp:ObjectDataSource runat="server" ID="odsEmploy" SelectMethod="GetEmploy"
TypeName="NewWebApp.EmployDataReader" ></asp:ObjectDataSource>
</asp:Panel>
---------------------------------------------------------
定义类:
public DataSet GetEmployDataSet()
{
var dstEmploy = new DataSet();
var reader = GetEmploy();
//读取 覆盖 访问
dstEmploy.Load(reader,LoadOption.OverwriteChanges,"Employ");
return dstEmploy;
}
---
<asp:Panel ID ="pnlDataSet" runat="server" GroupingText="DataSet数据库访问">
<asp:GridView ID="gvwDataSet" runat="server" AllowPaging="True" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="odsDataSet" >
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<asp:ObjectDataSource runat="server" ID="odsDataSet" TypeName="NewWebApp.EmployDataReader" SelectMethod="GetEmployDataSet">
</asp:ObjectDataSource>
</asp:Panel>
---
添加一个 Web Service ServerSpace.asmx
[WebMethod] //一个特性
public long GetServerSpace()
{
//using System.IO;
DriveInfo cdrive = new DriveInfo("D:\\");
return cdrive.TotalSize;
}
---
http://localhost:2035/ServerSpace.asmx
添加服务引用 localhost 发现 命名空间 -LocalWebService ok
--
Web.config 中可查看
--
ObjectDataSource.aspx
添加:
<asp:Panel runat="server" ID="pnlWebServiceObjectDataSource" GroupingText=" WebService ObjectDataSource">
<asp:FormView runat="server" ID="fvwServerSpace" DataSourceID="odsWebService">
<ItemTemplate>服务器计算机 C 盘总空间:<%#Container.DataItem %>GB</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="odsWebService" runat="server"
SelectMethod="GetServerSpace"
TypeName="NewWebApp.LocalWebService.ServerSpaceSoapClient">
</asp:ObjectDataSource>
</asp:Panel>
--
修改public long GetServerSpace()
{
//using System.IO;
DriveInfo cdrive = new DriveInfo("C:\\");
return cdrive.TotalSize/(1024*1024*1024);
}
--可看见计算机 C 盘总空间

浙公网安备 33010602011771号