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();
   
   }
  }

创建一个超级连接列(用于传值)和模版列
<asp:TemplateColumn>
     <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 }

原创粉丝点击