datalist实现增删改 分页
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;
using System.IO;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind();
}
}
public void Bind()
{
string strCon = "Data Source=355146557A47426\\SQLEXPRESS;Initial Catalog=stuinfo;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strSql = "select * from stuinfo";
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
da.Fill(ds);
PagedDataSource ps = new PagedDataSource();
ps.AllowPaging = true;
ps.DataSource = ds.Tables[0].DefaultView;
int curpage;
if (Request.QueryString["page"] != null)
{
curpage = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
curpage = 1;
}
ps.CurrentPageIndex = curpage - 1;
ps.PageSize = 2;
int count = ds.Tables[0].DefaultView.Count / ps.PageSize;
if (ds.Tables[0].DefaultView.Count % ps.PageSize != 0)
{
count++;
}
this.Label1.Text = "当前页:" + curpage.ToString() + " 共" + count.ToString() + "页";
this.HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath;
if (!ps.IsFirstPage)
{
this.HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (curpage - 1);
}
if (!ps.IsLastPage)
{
this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (curpage + 1);
}
this.HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + count;
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
con.Close();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
Bind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string sql = (this.DataList1.DataKeys[e.Item.ItemIndex]).ToString().Trim();
string strSq = "delete stuinfo where id= " + sql + "";
string strCon = "Data Source=355146557A47426\\SQLEXPRESS;Initial Catalog=stuinfo;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
con.Close();
Bind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string strName = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox1")).Text.ToString();
string strSq = "update stuinfo set name ='" + strName + "' where id= " + this.DataList1.DataKeys[e.Item.ItemIndex].ToString() + "";
string strCon = "Data Source=355146557A47426\\SQLEXPRESS;Initial Catalog=stuinfo;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
this.DataList1.EditItemIndex = -1;
con.Close();
Bind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "c=this.stle.backgroundColor,this.style.backgroundColor='#223344'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
((Button)e.Item.FindControl("Button4")).Attributes.Add("onclick", "return confirm('是否删除')");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string str = "image/"+this.FileUpload1.FileName;
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
string strPath = Server.MapPath(str);
FileUpload1.PostedFile.SaveAs(strPath);
string strSq = "insert into stuinfo(name,sex,age,pwd,image)";
strSq += "values('" + this.TextBox1.Text + "'," + this.RadioButtonList1.SelectedIndex + ",'" + this.TextBox2.Text + "','" + this.TextBox3.Text + "','" + str + "') ";
string strCon = "Data Source=355146557A47426\\SQLEXPRESS;Initial Catalog=stuinfo;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
con.Close();
Bind();
}
}
上面是完整代码 调试点编辑提示
string strName = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox1")).Text.ToString();
未将对象引用设置到对象的实例
点删除提示
string sql = (this.DataList1.DataKeys[e.Item.ItemIndex]).ToString().Trim();
索引超出范围。必须为非负值并小于集合大小。
寻求解决方法 如有更合适的写法可再加分