可以只要一点修改删除什么的 ~分页就会消失……
如何解决 ~
还有 求一删除的 代码
VS2005 C# SQL2000
谢谢 ~~
[此贴子已经被作者于2006-11-16 23:23:24编辑过]
这个App_Code里面的cs文件~自定义类库
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class db
{
protected string connstring ;
private int pagesize;
public db()
{
connstring = ConfigurationManager.ConnectionStrings["Shoping"].ConnectionString;
pagesize = 4;
}
public DataTable dt(string query)
{
SqlConnection con = new SqlConnection(connstring);
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
return ds.Tables["name"];
}
public PagedDataSource pds()
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dt("select * from Customer").DefaultView;
pds.AllowPaging = true;
pds.PageSize = pagesize;
pds.CurrentPageIndex = pds.CurrentPageIndex;
return pds;
}
public PagedDataSource pds(int pg)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dt("select * from Customer").DefaultView;
pds.AllowPaging = true;//允许分页
pds.PageSize = pagesize;//显示多少项
pds.CurrentPageIndex = pg;//当前页的索引
return pds;
}
public int sql(string query)
{
SqlConnection con = new SqlConnection(connstring);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
return cmd.ExecuteNonQuery();
}
}
这个是分页
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class shiyan_shiyan2 : System.Web.UI.Page
{
private static int p;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int n;
if (Request.QueryString["page"] != null)
{
n = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
n = 0;
}
p = n;
bind(n);
}
}
private void bind(int n)
{
db sdb = new db();
DataList1.DataSource = sdb.pds(n);
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//PlaceHolder ph = new PlaceHolder();
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
db sdb = new db();
for (int i = 0; i < sdb.pds().PageCount; i++)
{
HyperLink hl = new HyperLink();
Literal nsb = new Literal();
int n = i + 1;
hl.Text = n.ToString();
hl.ID = n.ToString();
hl.NavigateUrl = "?page=" + i.ToString();
nsb.Text = " ";
ph.Controls.Add(hl);
ph.Controls.Add(nsb);
}
}
}
}
}
这个是编辑,删除的~~
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class shiyan_shiyan1 : System.Web.UI.Page
{
//public static string con;
//con=ConfigurationManager.ConnectionStrings["Shoping"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
DataList3.DataSource = dt("select top 10 * from Customer");
DataList3.DataKeyField="C_Name";
DataList3.DataBind();
}
public DataTable dt(string qq)
{
string con = ConfigurationManager.ConnectionStrings["Shoping"].ConnectionString;
SqlConnection sc = new SqlConnection(con);
sc.Open();
SqlDataAdapter sda = new SqlDataAdapter(qq, sc);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
return ds.Tables["name"];
}
public int eut(string qq)
{
string con = ConfigurationManager.ConnectionStrings["Shoping"].ConnectionString;
SqlConnection sc = new SqlConnection(con);
sc.Open();
SqlCommand cmd = new SqlCommand(qq,sc);
return cmd.ExecuteNonQuery();
}
protected void DataList3_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList3.EditItemIndex = -1;
bind();
}
protected void DataList3_EditCommand(object source, DataListCommandEventArgs e)
{
DataList3.EditItemIndex = e.Item.ItemIndex;
bind();
}
protected void DataList3_DeleteCommand(object source, DataListCommandEventArgs e)
{
//string id = DataList3.DataKeyField[e.Item.ItemIndex].ToString();
string id = ((Label)e.Item.FindControl("Label4")).Text.Trim();
string qq = "delete from Customer where C_Name='" + id + "'";
if (eut(qq) > 0)
{
DataList3.EditItemIndex = -1;
bind();
}
}
protected void DataList3_UpdateCommand(object source, DataListCommandEventArgs e)
{
//string id = DataList3.DataKeyField[e.Item.ItemIndex].ToString();
string id = ((Label)e.Item.FindControl("Label4")).Text.Trim();
string datetime = ((TextBox)e.Item.FindControl("TextBox1")).Text.Trim();
string emaile = ((TextBox)e.Item.FindControl("TextBox2")).Text.Trim();
string qq = "update Customer set C_DateTime='" + datetime + "',C_Emaile='" + emaile + "' where C_Name='" + id +"'";
if(eut(qq) > 0)
{
DataList3.EditItemIndex = -1;
bind();
}
}
/*
protected void DataList3_ItemCommand(object source, DataListCommandEventArgs e)
{
string id = ((Label)e.Item.FindControl("Label4")).Text.Trim();
string qq = "delete from Customer where C_Name='" + id + "'";
if (eut(qq) > 0)
{
DataList3.EditItemIndex = -1;
bind();
}
}
*/
}
如何把2,3结合在一起~~
当编辑的时候~分页继续出现~
protected void DataList3_DeleteCommand(object source, DataListCommandEventArgs e)
{
//string id = DataList3.DataKeyField[e.Item.ItemIndex].ToString();
string id = ((Label)e.Item.FindControl("Label4")).Text.Trim();
string qq = "delete from Customer where C_Name='" + id + "'";
if (eut(qq) > 0)
{
DataList3.EditItemIndex = -1;
bind();
}
}
这个删除好像删除失败……帮忙改一下~~
[此贴子已经被作者于2006-11-16 23:14:32编辑过]
没有错~~