public partial class DataListDemo1 : System.Web.UI.Page
{
private SqlConnection conn;
private SqlDataAdapter dad;
private DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
this.conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["pubsConnectionString"].ToString());
this.dad = new SqlDataAdapter("select emp_id,fname,lname,hire_date from employee", conn);
this.ds = new DataSet();
this.dad.Fill(ds);
//this.DataList1.DataSource = ds;
this.DataList1.DataKeyField = "emp_id";//一定指定这个属性,否则后面通过索引号,来得到键的值就会发生数组越界的异常
PagedDataSource pagedate = new PagedDataSource();
pagedate.DataSource =this.ds.Tables[0].DefaultView;//设置表的自定义视图
pagedate.AllowPaging = true;//启动分页
pagedate.PageSize = 2;//设置要在单页面上的显示的记录条
//设置每次请求的页面
if (this.Request.QueryString["Page"] != null)
pagedate.CurrentPageIndex = Convert.ToInt32(this.Request.QueryString["Page"]);
else
pagedate.CurrentPageIndex = 0;
this.DataList1.DataSource = pagedate;
if (!this.IsPostBack) {
this.DataList1.DataBind();//将数据绑定到控件上
//设置第一页,用HyperLink1的NavigateUrl属性用Get传值的方式链接到当前请求的虚拟路径第一页
this.HyperLink1.NavigateUrl = this.Request.CurrentExecutionFilePath + "?Page=0";
//判断是否是第一页,防止如果到达第一页是向前翻会找不到路径
if (!pagedate.IsFirstPage) {
//设置上一页,用HyperLink1的NavigateUrl属性用Get传值的方式链接到当前请求的虚拟路径当前页数-1;
this.HyperLink2.NavigateUrl = this.Request.CurrentExecutionFilePath + "?Page=" + (pagedate.CurrentPageIndex - 1);
}
//判断是否是第一页,防止如果到达最后一页是向后翻会找不到路径
if (!pagedate.IsLastPage) {
//设置下一页,用HyperLink1的NavigateUrl属性用Get传值的方式链接到当前请求的虚拟路径当前页数+1;
this.HyperLink3.NavigateUrl = this.Request.CurrentExecutionFilePath + "?Page=" + (pagedate.CurrentPageIndex + 1);
}
//设置最后一页,用HyperLink1的NavigateUrl属性用Get传值的方式链接到当前请求的虚拟路径总页数-1;
this.HyperLink4.NavigateUrl = this.Request.CurrentExecutionFilePath + "?Page=" + (pagedate.PageCount - 1);
//用Label1的Text属性显示当前页+1和总页数
this.Label1.Text=(pagedate.CurrentPageIndex+1)+"/"+pagedate.PageCount;
}
}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.DataList1.EditItemIndex = -1;//将索引切换到编辑状态
this.DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;//获取每项的索引号并赋值给要编辑的索引
this.DataList1.DataBind();
// this.Response.Write(e.Item.ItemIndex.ToString());
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
//找到要修改的那行的全部记录,返回一个数据
DataRow[] drs = ds.Tables[0].Select("emp_id='" + this.DataList1.DataKeys[e.Item.ItemIndex] + "'");
//通过控件的id来找服务器控件来的所修改的值
drs[0]["emp_id"] = ((TextBox)e.Item.FindControl("TextBox1")).Text;
drs[0]["fname"] = ((TextBox)e.Item.FindControl("TextBox2")).Text;
drs[0]["lname"] = ((TextBox)e.Item.FindControl("TextBox3")).Text;
drs[0]["hire_date"] = ((TextBox)e.Item.FindControl("TextBox4")).Text;
//修改数据库的做法
SqlCommandBuilder cb = new SqlCommandBuilder(this.dad);
this.dad.Update(this.ds);
this.RegisterStartupScript("", "<script>alert('Update!')</script>");
this.DataList1.EditItemIndex = -1;//将索引切换到编辑状态
this.DataList1.DataBind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
//找到要修改的那行的全部记录,返回一个数据
DataRow[] drs = ds.Tables[0].Select("emp_id='" + this.DataList1.DataKeys[e.Item.ItemIndex] + "'");
//讲这行删除掉
drs[0].Delete();
//修改数据库的做法
SqlCommandBuilder cb = new SqlCommandBuilder(this.dad);
this.dad.Update(this.ds);
this.DataList1.SelectedIndex = -1;//将索引切换到编辑状态
this.DataList1.DataBind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;//将索引切换到编辑状态
this.DataList1.DataBind();
}
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
//if (e.Item.ItemType == ListItemType.SelectedItem)
//{
// ((LinkButton)e.Item.FindControl("LinkButton4")).Attributes.Add("on click", "return confirm('updage?')");
//}
//if (e.Item.ItemType == ListItemType.SelectedItem)
//{
// ((LinkButton)e.Item.FindControl("LinkButton3")).Attributes.Add("on click", "return confirm('delete?')");
//}
}
}