用DataList來做一個仿噹噹網上書店的小應用

c979170768發表於2011-11-25
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          Disp(1);
        }
   


    }

    private  void Disp(int index_num)
    {
        string strcon = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strcon))
        {
            using (SqlCommand cmd = new SqlCommand("sp_Student_Select_by_Page_rowNumber", con))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@pagesize",3);
                cmd.Parameters.Add("@pagecount",SqlDbType.Int).Direction=ParameterDirection.Output;
                cmd.Parameters.Add("@pageindex",index_num);

                DataSet ds = new DataSet();
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    sda.Fill(ds);
                }
                HiddenField1.Value = index_num.ToString();
                HiddenField2.Value = cmd.Parameters["@pagecount"].Value.ToString();
                Label4.Text = HiddenField1.Value.ToString() + "/" + HiddenField2.Value.ToString();
                DataList1.DataSource = ds.Tables[0];
                DataBind();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Disp(1);
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Disp(Convert.ToInt32(HiddenField2.Value));
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int page_num = Convert.ToInt32(HiddenField1.Value);
        if (page_num>1)
        {
            Disp(page_num-1);
           // page_num--;
        }
     //   Disp(page_num);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int page_num = Convert.ToInt32(HiddenField1.Value);
        if (page_num < Convert.ToInt32(HiddenField2.Value))
        {
            Disp(page_num + 1);
        }

    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        int num=Convert.ToInt32(TextBox1.Text);
        if (num>0&&num<=Convert.ToInt32(HiddenField2.Value))
        {
            Disp(num);
        }
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        Disp(Convert.ToInt32(HiddenField1.Value));
      
    }

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        Disp(Convert.ToInt32(HiddenField1.Value));
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;

        using (SqlConnection con = new SqlConnection(str))
        {
            using (SqlCommand cmd = new SqlCommand("delete from book where id=@id", con))
            {

                cmd.Parameters.Add("@id",e.CommandArgument.ToString());
                con.Open();
                cmd.ExecuteNonQuery();
                Disp(Convert.ToInt32(HiddenField1.Value));
               
            
            }
        }

 

    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
      //  Response.Write("asdfasdfasdf");
       //  Response.Write((e.Item.FindControl("HiddenField3") as HiddenField).Value);
        // Response.Write(e.CommandArgument.ToString());
         string title = (e.Item.FindControl("title") as TextBox).Text;
         string price = (e.Item.FindControl("price") as TextBox).Text;
         string content = (e.Item.FindControl("content") as TextBox).Text;
         string Date1 = (e.Item.FindControl("data") as TextBox).Text;
         string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
         using (SqlConnection con = new SqlConnection(str))
         {
             using (SqlCommand cmd = new SqlCommand("update book set title=@title,content=@content,datatime=@date,price=@price where id=@id", con))
             {
                 cmd.Parameters.Add("@title", title);
                 cmd.Parameters.Add("@content", content);
                 cmd.Parameters.Add("@date", Date1);
                 cmd.Parameters.Add("@price", price);
                 cmd.Parameters.Add("@id", e.CommandArgument.ToString());
                 con.Open();
                 cmd.ExecuteNonQuery();

                 DataList1.EditItemIndex = -1;
                 Disp(Convert.ToInt32(HiddenField1.Value));
             }
         }

 


    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName=="1")
        {
             Response.Write(e.CommandArgument.ToString()+"這本書已經加入購物車!");
        }
        else if(e.CommandName=="2")
            Response.Write(e.CommandArgument.ToString() + "收藏成功!");
    }
}


 

相關文章