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());
}
}
}
DataList控件
最新推荐文章于 2024-09-21 10:25:52 发布