首页 > 代码库 > DataList的使用 分页 用Button删除 编辑 datalist中的内容 数据源的绑定 及表格的使用

DataList的使用 分页 用Button删除 编辑 datalist中的内容 数据源的绑定 及表格的使用

以前没接触过datalist  这次做项目第一次使用它   遇到一些问题  所以跟大家分享一下  以后遇到问题可以节约点时间 

先来个效果图吧

 

1

先来说模版八   我用的是ItemTemplate模块  

1:先在页面中假如datalist控件  接下来我们编辑模块选择ItemTempalte选项 然后结束模版编辑 

2:数据源的绑定

 1

数据源根据你自己的情况来选择 我的数据在sql中  所以选的这个

 2

按上面表好的步骤操作    4是你自己的数据库名称   测试成功后一路默认选择下一步  最后来到

1

先测试一下有没有数据  是不是你要的数据   然后点击完成  

datalist就变成下面这个样子了

QQ截图20140804163107

接下来就是修改了 修改模块  

1

模版建好了  接下来我们来修改数据源    当我们点击到datalist控件的时候  都会又一个向右的箭头  我们点击它  把数据源该成使用自定义绑定

刚刚我们本来也可以这样绑定  但为了自己在写表达式的时候出问题 我们选择这会儿修改一下   给编辑按钮和删除按钮添加onclick事件(双击就可以了)  并给他们的commandName属性中添加一个名字  待会儿会用到 除了edit delete update外的名字  (我的名字为edit1 delete1)

然后结束模版编辑  并且把数据源选择为无 出现提示框时选否

添加分页按钮   自己想怎么弄怎么弄

linkbutton 按钮跟lable两个构成

1
 

始让我们的数据显示出来八  记得添加命名空间

   1: using System.Data;
   2: using System.Data.SqlClient;
   3: using System.Web.Configuration;
   1: protected void Page_Load(object sender, EventArgs e)
   2:         {
   3:             string sql = "select * from AdvertisingPicture order by PlaySort";
   4:  
   5:             if (!Page.IsPostBack)
   6:                 bind(sql);
   7:  
   8:         }
   9:  
  10:         public void bind(string sql)
  11:         {
  12:             int current_page = Convert.ToInt32(lblCurrent.Text);
  13:             string constr = WebConfigurationManager.ConnectionStrings["OrderSystem4ConnectionString"].ConnectionString;
  14:  
  15:             SqlConnection con = new SqlConnection(constr);
  16:             SqlDataAdapter da = new SqlDataAdapter(sql, con);
  17:             DataSet ds = new DataSet();
  18:             da.Fill(ds);
  19:             PagedDataSource ps = new PagedDataSource();
  20:             ps.DataSource = ds.Tables[0].DefaultView;
  21:             ps.AllowPaging = true;
  22:             ps.PageSize = 6;
  23:             lblTotal.Text = ps.PageCount.ToString();
  24:             lbSum.Text = ps.DataSourceCount.ToString();
  25:             ps.CurrentPageIndex = current_page - 1;
  26:             lbtnFirst.Enabled = true;
  27:             lbntUp.Enabled = true;
  28:             lbtnDown.Enabled = true;
  29:             lbtnLast.Enabled = true;
  30:             if (current_page == 1)
  31:             {
  32:                 lbtnFirst.Enabled = false;
  33:                 lbntUp.Enabled = false;
  34:             }
  35:             if (current_page == Convert.ToInt32(lblTotal.Text))
  36:             {
  37:                 lbtnLast.Enabled = false;
  38:                 lbtnDown.Enabled = false;
  39:             }
  40:             DataList1.DataSource = ps;
  41:             DataList1.DataBind();
  42:         }
  43: protected void lbtnFirst_Click(object sender, EventArgs e)
  44:        {
  45:            string sql = "select * from AdvertisingPicture order by PlaySort";
  46:            lblCurrent.Text = "1";
  47:            bind(sql);
  48:        }
  49:  
  50:        protected void lbntUp_Click(object sender, EventArgs e)
  51:        {
  52:            string sql = "select * from AdvertisingPicture order by PlaySort";
  53:            lblCurrent.Text = (Convert.ToInt32(lblCurrent.Text) - 1).ToString();
  54:            bind(sql);
  55:        }
  56:  
  57:        protected void lbtnDown_Click(object sender, EventArgs e)
  58:        {
  59:            string sql = "select * from AdvertisingPicture order by PlaySort";
  60:            lblCurrent.Text = (Convert.ToInt32(lblCurrent.Text) + 1).ToString();
  61:            bind(sql);
  62:        }
  63:  
  64:        protected void lbtnLast_Click(object sender, EventArgs e)
  65:        {
  66:            string sql = "select * from AdvertisingPicture order by PlaySort";
  67:            lblCurrent.Text = lblTotal.Text;
  68:            bind(sql);
  69:        }

插件问题    就将就看了八   已经实现了分页(当前页的text首先在属性里设置为1)  接下来就是删除 跟编辑按钮

给Datalist添加ItemCommand事件

   1: protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
   2:         {
   3:             if (e.CommandName == "edit1")
   4:             {
   5:                  
   6:                 Label lb = (Label) e.Item.FindControl("PictureNoLabel");
   7:                 string num = lb.Text.ToString();
   8:                 Response.Redirect("adEdit.aspx?num=" + num);
   9:             }
  10:             if (e.CommandName == "delete1")
  11:             {
  12:                 Label lb =(Label) e.Item.FindControl("PictureNoLabel");
  13:                 string  no = lb.Text.ToString();
  14:                 string sql = "delete from AdvertisingPicture where PictureNo=‘" + no + "‘";
  15:                 string constr = WebConfigurationManager.ConnectionStrings["OrderSystem4ConnectionString"].ConnectionString;
  16:  
  17:                 SqlConnection con = new SqlConnection(constr);
  18:                 try
  19:                 {
  20:                     SqlCommand cmd = new SqlCommand(sql, con);
  21:                     con.Open();
  22:  
  23:                     cmd.ExecuteNonQuery();
  24:  
  25:                     Response.Write("<script>alert(‘删除成功!‘)</script>");
  26:                 }
  27:                 catch (SqlException ex)
  28:                 {
  29:                     throw ex;
  30:                 }
  31:                 finally
  32:                 {
  33:                     con.Close();
  34:                 }
  35:                 sql = "select * from AdvertisingPicture order by PlaySort";
  36:                 bind(sql);
  37:             
  38:             }
  39:         }

我的代码就演示完了 对于编辑 我是直接跳转到另一个页面的 给你看看另一个页面代码 以及效果图

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.UI;
   6: using System.Web.UI.WebControls;
   7: using System.Data.SqlClient;
   8: using System.Data;
   9: using System.Web.Configuration;
  10:  
  11: namespace test1
  12: {
  13:     public partial class adEdit : System.Web.UI.Page
  14:     {
  15:         protected void Page_Load(object sender, EventArgs e)
  16:         {
  17:             if (Request.QueryString["num"] != null&&!IsPostBack)
  18:             {
  19:                 string num = Request.QueryString["num"].ToString();
  20:                // Session["num"] = Request.QueryString["num"].ToString();
  21:                 string sql = "select * from AdvertisingPicture where PictureNo=‘" + num + "‘";
  22:                 string constr = WebConfigurationManager.ConnectionStrings["OrderSystem4ConnectionString"].ConnectionString;
  23:                 SqlConnection con = new SqlConnection(constr);
  24:                 SqlDataAdapter da = new SqlDataAdapter(sql, con);
  25:                 DataSet ds = new DataSet();
  26:                 da.Fill(ds);
  27:  
  28:                 Label1.Text = ds.Tables[0].Rows[0]["PictureNo"].ToString();
  29:                 Image1.ImageUrl = ds.Tables[0].Rows[0]["PictureUrl"].ToString();
  30:                 Session["pathname"] = ds.Tables[0].Rows[0]["PictureUrl"].ToString();
  31:                 TextBox1.Text = ds.Tables[0].Rows[0]["Name"].ToString();
  32:                 TextBox2.Text = ds.Tables[0].Rows[0]["PlaySort"].ToString();
  33:             }
  34:  
  35:  
  36:         }
  37:  
  38:  
  39:         protected void Button1_Click(object sender, EventArgs e)
  40:         {
  41:             if (FileUpload1.HasFile)
  42:             {
  43:  
  44:                 try {
  45:                     if (FileUpload1.PostedFile.ContentType != "image/gif" && FileUpload1.PostedFile.ContentType != "image/jpg"
  46:                            && FileUpload1.PostedFile.ContentType != "image/pjpeg" && FileUpload1.PostedFile.ContentType != "image/jpeg")
  47:                     {
  48:  
  49:                         //只能是图片格式   
  50:                         Response.Write("<script>alert(‘您要上传的照片格式只能是JPG或GIF格式.‘);</script>");
  51:                     }
  52:                     else
  53:                     {
  54:                         string path = Request.MapPath("~/image/");
  55:                         FileUpload1.SaveAs(path + FileUpload1.FileName);
  56:                         string pathname = "~/image/" + FileUpload1.FileName;
  57:                         Session["pathname"] = pathname;
  58:                         Image1.ImageUrl = Session["pathname"].ToString();
  59:                     }
  60:                 }
  61:                 catch(SqlException ex)
  62:                 {
  63:                     throw ex;
  64:                 }
  65:             }
  66:         }
  67:  
  68:         protected void Button2_Click(object sender, EventArgs e)
  69:         {
  70:             string PictureUrl = Session["pathname"].ToString();
  71:             string Name = string.IsNullOrEmpty(TextBox1.Text.ToString().Trim()) ? "空" : TextBox1.Text.ToString();
  72:            // int m = Convert.ToInt32(TextBox2.Text.ToString());
  73:             int PlaySort = 0;
  74:                 try{
  75:  
  76:               PlaySort =Convert.ToInt32( string.IsNullOrEmpty(TextBox2.Text.ToString().Trim())?0:Convert.ToInt32( TextBox2.Text.ToString()));
  77:                 }
  78:             catch
  79:                 {
  80:                     Response.Write("<script>alert(‘请输入正确的播放顺序!‘)</script>");
  81:                     return;
  82:             }
  83:             string num = Request.QueryString["num"].ToString();
  84:             string sql = "update AdvertisingPicture set  Name = ‘"+Name+"‘,PictureUrl=‘"+PictureUrl+"‘ ,PlaySort=‘"+PlaySort+"‘ where PictureNo = ‘"+num+"‘";
  85:             string constr = WebConfigurationManager.ConnectionStrings["OrderSystem4ConnectionString"].ConnectionString;
  86:             SqlConnection con = new SqlConnection(constr);
  87:             try
  88:             {
  89:                 con.Open();
  90:                 SqlCommand cmd = new SqlCommand(sql, con);
  91:                 cmd.ExecuteNonQuery();
  92:             }
  93:             catch (SqlException ex)
  94:             {
  95:                 throw ex;
  96:             }
  97:             finally
  98:             {
  99:                 con.Close();
 100:                 Response.Redirect("test.aspx");
 101:             }
 102:         }
 103:  
 104:         protected void Button3_Click(object sender, EventArgs e)
 105:         {
 106:             Response.Redirect("test.aspx");
 107:         }
 108:     }
 109: }
1