当前位置:编程学习 > asp >>

asp.net之DataList的使用方法,及分页(存储过程创建),编辑,更新,删除

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;

using System.Data;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if(!IsPostBack)

            BindProduct("1");

    }

    private void BindProduct(string pageindex)

    {

        string str = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;

        using (SqlConnection sqlCnn = new SqlConnection(str))

        {

            SqlDataAdapter da = new SqlDataAdapter("sp_Student_Select_by_Page_rowNumber", sqlCnn);

            da.SelectCommand.Parameters.AddWithValue("@pageIndex", pageindex);

            da.SelectCommand.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;

            da.SelectCommand.Parameters.AddWithValue("@pageSize", 2);

            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet ds = new DataSet();

            da.Fill(ds);

            this.DataList1.DataSource = ds.Tables[0].DefaultView;

            this.DataList1.DataBind();

            this.HiddenField1.Value = pageindex;

            this.HiddenField2.Value = da.SelectCommand.Parameters["@pageCount"].Value.ToString();

        }

    }     //绑定数据 www.zzzyk.com

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)

    {

        if(e.CommandName == "buy")

            Response.Write(e.CommandArgument.ToString());

    }

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

    {

        this.DataList1.EditItemIndex = e.Item.ItemIndex;

        this.BindProduct(this.HiddenField1.Value);

    }         //编辑

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)

    {

        this.DataList1.EditItemIndex = -1;

        this.BindProduct(this.HiddenField1.Value);

    }           //取消

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)

    {

        string name = (e.Item.FindControl("TextBox1") as TextBox).Text;

        string 易做图 = (e.Item.FindControl("TextBox2") as TextBox).Text;

        string age = (e.Item.FindControl("TextBox3") as TextBox).Text;

        string str = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;

        using (SqlConnection sqlCnn = new SqlConnection(str))

        {

            SqlCommand sqlcmm = sqlCnn.CreateCommand();

            sqlcmm.CommandText = "update student set sname=@sname,易做图=@易做图,age=@age where sid=@sid";

            sqlcmm.Parameters.AddWithValue("@sname", name);

            sqlcmm.Parameters.AddWithValue("@易做图", 易做图);

            sqlcmm.Parameters.AddWithValue("@age", age);

            sqlcmm.Parameters.AddWithValue("@sid", e.CommandArgument);

            sqlCnn.Open();

            sqlcmm.ExecuteNonQuery();

        }

        this.DataList1.EditItemIndex = -1;

        this.BindProduct(this.HiddenField1.Value);

    } //更新

    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)

    {

        string str = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;

        using (SqlConnection sqlCnn = new SqlConnection(str))

        {

            SqlCommand sqlcmm = sqlCnn.CreateCommand();

            sqlcmm.CommandText = "delete from student where sid=@sid";

            sqlcmm.Parameters.AddWithValue("@sid", e.CommandArgument);

            sqlCnn.Open();

            sqlcmm.ExecuteNonQuery();

        }

        this.BindProduct(this.HiddenField1.Value);

    }    //删除

    protected void Button6_Click(object sender, EventArgs e)

    {

        this.BindProduct("1");

    }  //首页

    protected void Button9_Click(object sender, EventArgs e)

    {

        int count = Convert.ToInt32(this.HiddenField2.Value);

        this.BindProduct(count.ToString());

    }  //尾页

    protected void Button7_Click(object sender, E

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,