Asp.Net从零开始学-20
来源:互联网 发布:青岛市淘宝运营招聘 编辑:程序博客网 时间:2024/06/06 02:17
购物车
主页面main.aspx
通过在HTML中使用样式表统一页面字体<style type="text/css"> TABLE { FONT-SIZE: 12px } </style>
相关数据库信息
create database shoppingBus
go
use shoppingBus
go
create table petType
(
petTypeID varchar(10) primary key,
petTypeName varchar(50) not null unique
)
go
insert into petType values('pt1001','鸟类')
insert into petType values('pt1002','狗')
insert into petType values('pt1003','猫')
go
create table pet
(
petID varchar(20) primary key,
petName varchar(100) not null,
petTypeID varchar(10) foreign key references petType(petTypeID),
petPrice money,
petPhoto varchar(30),
perRemark varchar(1000)
)
go
insert into pet values('per1001','波斯猫','pt1003',100,'0001.gif','好猫啊,绝对杂种')
insert into pet values('per1002','波斯猫','pt1003',100,'0002.gif','好猫啊,绝对杂种')
insert into pet values('per1003','波斯猫','pt1003',100,'0003.gif','好猫啊,绝对杂种')
insert into pet values('per1004','波斯猫','pt1003',100,'0004.gif','好猫啊,绝对杂种')
go
select * from pet
go
select *from petType
//建立数据库连接类DB.cs
using System;
using System.Data.SqlClient;
namespace shoppingTest
{
public class DB
{
public static SqlConnection conCreate(){
return new SqlConnection("server=.;database=shoppingBus;uid=sa;pwd=12345678");
}
}
}
//页面绑定数据库
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack){
SqlConnection con=DB.conCreate();
con.Open();
SqlCommand cmd=new SqlCommand("select * from petType",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.DataGrid2.DataSource=sdr;
this.DataGrid2.DataBind();
}
}
创建一个超级连接列(用于传值)和模版列
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
#
</ItemTemplate>
</asp:TemplateColumn>
showPetBytypeID.aspx
private void Page_Load(object sender, System.EventArgs e)
{
// 数据绑定
if(!this.IsPostBack){
string PetTypeID=Request.QueryString["typeID"].ToString();
SqlConnection con=DB.conCreate();
con.Open();
SqlCommand cmd=new SqlCommand("select petTypeName from petType where petTypeID='"+PetTypeID+"'",con);
this.Label1.Text=Convert.ToString(cmd.ExecuteScalar());
cmd.CommandText="select * from pet where petTypeID='"+PetTypeID+"'";
SqlDataReader sdr=cmd.ExecuteReader();
this.DataGrid1.DataKeyField="petID";
this.DataGrid1.DataSource=sdr;
this.DataGrid1.DataBind();
}
}
添加绑定列
宠物照片 petPhoto <img src='photo/{0}' width=60 height=90></img>
宠物名称 petName
售价 petPrice {0:c}
描述 perRemark
添加按钮列
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{//判断是否购买,通过Session对象来传值
if(e.CommandName=="AddToBus")
{
string petID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
if(Session["bus"]==null)
{
System.Collections.Hashtable ht=new Hashtable();
ht.Add(petID,1);
Session["bus"]=ht;
//Session.Add("bus",ht);
}
else{
System.Collections.Hashtable ht=(Hashtable)Session["bus"];
if(ht[petID]==null)
{
ht[petID]=1;
}
else{
ht[petID]=(int)ht[petID]+1;
}
Session["bus"]=ht;
}
}
}
private void btnShowBus_Click(object sender, System.EventArgs e)
{
Response.Redirect("showBus.aspx");
}
showBus.aspx
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack){
this.DataList1.DataSource=(Hashtable)Session["bus"];
this.DataList1.DataBind();
}
}
<asp:DataList id="DataList1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 48px" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"key") %>
:
<asp:TextBox id=TextBox1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "value") %>'>
</asp:TextBox>
</ItemTemplate>
<SeparatorTemplate>
<HR width="100%" SIZE="1">
</SeparatorTemplate>
</asp:DataList></FONT>
TABLE { FONT-SIZE: 12px }
- Asp.Net从零开始学-20
- 从零开始学ASP.NET
- Asp.net从零开始学-1
- Asp.net从零开始学-2
- Asp.net从零开始学-3
- Asp.net从零开始学-4
- Asp.Net从零开始学-5
- Asp.Net从零开始学-6
- Asp.Net从零开始学-7
- Asp.Net从零开始学-8
- Asp.Net从零开始学-9
- Asp.Net从零开始学-10
- Asp.Net从零开始学-11
- Asp.Net从零开始学-12
- Asp.Net从零开始学-13
- Asp.Net从零开始学-14
- Asp.Net从零开始学-15
- Asp.Net从零开始学-16
- 有中国特色的测试论坛
- 我们需要数学的理由
- 不安装客户端连接sql server 2000
- popup组件问题分析
- CSS学习笔记五
- Asp.Net从零开始学-20
- 我会好好的
- IBM笔记本选购注意事项(笔记本是一个大物品,所以会三思而后行的)
- 使fread和fwrite的运用更加的强壮
- VS2005 常用快捷键
- camera calibration 摄像头标定 代码整理
- jsp实现的数据库模糊搜索(可以自己设定匹配字符个数)
- 1次插入多条记录的方法(SQL语句,在MS SQL Server 2000查询分析器中执行成功)
- JSP--读文本文件和中文字符乱码的测试