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 Datalist_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FullDL("1");
        }
    }
	
    private void FullDL(string index)
    {
        string sql = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(sql))
        {
            //SqlDataAdapter da = new SqlDataAdapter("select * from student", conn);
            SqlDataAdapter da = new SqlDataAdapter("sp_Student_Select_by_Page_rowNumber", conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("pageIndex",index);
            da.SelectCommand.Parameters.AddWithValue("pageSize",4);
            da.SelectCommand.Parameters.Add("pageCount",SqlDbType.Int).Direction=ParameterDirection.Output;
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.DataList1.DataSource = ds.Tables[0].DefaultView;
            this.DataList1.DataBind();
            this.hidIndex.Value = index;
            this.hidMax.Value = da.SelectCommand.Parameters["pageCount"].Value.ToString();
            this.Label3.Text = index + "/" + this.hidMax.Value;
        }
}

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
        {
            Image img = e.Item.FindControl("Image1") as Image;
            if (img.ImageUrl=="")
            {
                img.ImageUrl = "images/noimage.gif";
            }
            Label labnm = e.Item.FindControl("Label2") as Label;
            Button btndel = e.Item.FindControl("Button3") as Button;
            btndel.OnClientClick = "return confirm('确定要删除- "+labnm.Text+" - 吗')"; 
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName=="buy")
        {
            Response.Write(e.CommandArgument);
        }
}
//修改选中项
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        this.FullDL(this.hidMax.Value);
}
//取消要修改的选中项
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = -1;
        this.FullDL(this.hidMax.Value);
}
//更新选中项
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        string sql = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(sql))
        {
            using (SqlCommand comm=conn.CreateCommand ())
            {
                comm.CommandText = "update student set sname=@name,sex=@sex,age=@age,photo=@photo where sid="+e.CommandArgument;
                comm.Parameters.AddWithValue("@name", (e.Item.FindControl("TextBox1") as TextBox).Text);
                comm.Parameters.AddWithValue("@sex", (e.Item.FindControl("TextBox2") as TextBox).Text);
                comm.Parameters.AddWithValue("@age", (e.Item.FindControl("TextBox3") as TextBox).Text);
                comm.Parameters.AddWithValue("@photo", (e.Item.FindControl("TextBox4") as TextBox).Text);
                conn.Open();
                comm.ExecuteNonQuery();
            }
        }
        this.DataList1.EditItemIndex = -1;
        FullDL(this.hidMax.Value);
}
//删除选中项
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        string sql = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(sql))
        {
            using (SqlCommand comm = conn.CreateCommand())
            {
                comm.CommandText = "delete student where sid=" + e.CommandArgument;
                conn.Open();
                comm.ExecuteNonQuery();
            }
        }
        this.DataList1.EditItemIndex = -1;
        FullDL(this.hidMax.Value);
    }

    //第一页
    protected void Button6_Click(object sender, EventArgs e)
    {
        FullDL("1");
    }
    //最后一页
    protected void Button9_Click(object sender, EventArgs e)
    {
        string zh = this.hidMax.Value;
        FullDL(zh);
    }
    //上一页
    protected void Button7_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.hidIndex.Value);
        if (index>1)
        {
            FullDL((index - 1).ToString());
        }

    }
    //下一页
    protected void Button8_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.hidIndex.Value);
        int count = Convert.ToInt32(this.hidMax.Value);
        if (index<count)
        {
            index++;
            FullDL(index.ToString());
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值