我的代码:public partial class admin_index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "数据管理 -用户管理" + mssql.web_name.ToString();
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "select * from [User]";
AspNetPager1.PageSize = 5;
AspNetPager1.RecordCount = (int)com.ExecuteScalar();
int shujin = (int)com.ExecuteScalar();
con.Close();
DataListDataBind();
}
}
private void DataListDataBind()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
SqlDataAdapter dr = new SqlDataAdapter("select * from [User] ", con);
DataSet ds = new DataSet();
dr.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "User ");
DataList1.DataSource = ds.Tables["User "];
DataList1.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
DataListDataBind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
SqlCommand cmd = new SqlCommand( "delete from [User] where UserId = '"+id+"'",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind(); }
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = this.DataList1.DataKeyField[e.Item.ItemIndex].ToString();
string Username = ((TextBox)e.Item.FindControl("")).Text.Trim();
string Pass = ((TextBox)e.Item.FindControl(" Password")).Text.Trim();
string sql = "update User set UserId =" + id+ ",UserName='" +Username + "',Password='" + Pass + "' where UserId=" + id+ "";
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataList1.DataBind();
mssql.DoSql(sql);
Response.Write("<script>alert('编辑成功');window.location.href='admin_index.aspx';</script>"); }
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
DataList1.DataBind(); }
}
编辑和取消不能完成!!求教高手还有查询的也不会写数据表UseId int(主键)
UseName varchar(50)
Password varchar(50)
{
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "数据管理 -用户管理" + mssql.web_name.ToString();
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "select * from [User]";
AspNetPager1.PageSize = 5;
AspNetPager1.RecordCount = (int)com.ExecuteScalar();
int shujin = (int)com.ExecuteScalar();
con.Close();
DataListDataBind();
}
}
private void DataListDataBind()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
SqlDataAdapter dr = new SqlDataAdapter("select * from [User] ", con);
DataSet ds = new DataSet();
dr.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "User ");
DataList1.DataSource = ds.Tables["User "];
DataList1.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
DataListDataBind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
SqlCommand cmd = new SqlCommand( "delete from [User] where UserId = '"+id+"'",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind(); }
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = this.DataList1.DataKeyField[e.Item.ItemIndex].ToString();
string Username = ((TextBox)e.Item.FindControl("")).Text.Trim();
string Pass = ((TextBox)e.Item.FindControl(" Password")).Text.Trim();
string sql = "update User set UserId =" + id+ ",UserName='" +Username + "',Password='" + Pass + "' where UserId=" + id+ "";
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataList1.DataBind();
mssql.DoSql(sql);
Response.Write("<script>alert('编辑成功');window.location.href='admin_index.aspx';</script>"); }
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
DataList1.DataBind(); }
}
编辑和取消不能完成!!求教高手还有查询的也不会写数据表UseId int(主键)
UseName varchar(50)
Password varchar(50)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind(); }
改为
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
DataListDataBind(); }
同样的
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
DataList1.DataBind(); }
改为 protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
DataListDataBind(); }
查询是哪个查询?
{
int ID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
TextBox txt= e.Item.FindControl("txtName") as TextBox;
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) {
DataList1.EditItemIndex = e.Item.ItemIndex;
}
分页使用aspnetpager分页控件