实验2 -DataList 增删改查等操作
2009-03-25 20:06
323 查看
具体效果如下所示:
使用Datalist显示数据,
新增数据,
修改
删除数据等操作
主要设置下面两个属性修改显示的外观。
6:在编辑模板列中,寻找事件选项,生成相关事件。并注意拖入的按钮的CommandName要改为delete,update,cancel等。
全例
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 xin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ti
if (!Page.IsPostBack)
{
Bind();
}
}
/// <summary>
/// 数据绑定
/// </summary>
public void Bind()
{
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
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.Label5.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();
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
Bind();
}
/// <summary>
/// 取消
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
}
/// <summary>
/// 更新
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string strName = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox2")).Text.ToString();
string strSq = "update stuinfo set name ='"+strName+"' where id= "+this.DataList1.DataKeys[e.Item.ItemIndex].ToString()+"";
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq,con);
com.ExecuteNonQuery();
this.DataList1.EditItemIndex = -1;
con.Close();
Bind();
}
/// <summary>
/// 删除后绑定
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strSq = "delete stuinfo where id= " + this.DataList1.DataKeys[e.Item.ItemIndex].ToString() + "";
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
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 Button1_Click(object sender, EventArgs e)
{
string str = "1/" + this.FileUpload1.FileName;
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.TextBox3.Text+"','"+this.TextBox4.Text+"','"+str+"') ";
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
con.Close();
Bind();
}
}
使用Datalist显示数据,
新增数据,
修改
删除数据等操作
主要设置下面两个属性修改显示的外观。
6:在编辑模板列中,寻找事件选项,生成相关事件。并注意拖入的按钮的CommandName要改为delete,update,cancel等。
全例
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 xin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ti
if (!Page.IsPostBack)
{
Bind();
}
}
/// <summary>
/// 数据绑定
/// </summary>
public void Bind()
{
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
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.Label5.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();
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
Bind();
}
/// <summary>
/// 取消
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
}
/// <summary>
/// 更新
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string strName = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox2")).Text.ToString();
string strSq = "update stuinfo set name ='"+strName+"' where id= "+this.DataList1.DataKeys[e.Item.ItemIndex].ToString()+"";
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq,con);
com.ExecuteNonQuery();
this.DataList1.EditItemIndex = -1;
con.Close();
Bind();
}
/// <summary>
/// 删除后绑定
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strSq = "delete stuinfo where id= " + this.DataList1.DataKeys[e.Item.ItemIndex].ToString() + "";
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
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 Button1_Click(object sender, EventArgs e)
{
string str = "1/" + this.FileUpload1.FileName;
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.TextBox3.Text+"','"+this.TextBox4.Text+"','"+str+"') ";
string strCon = "server=.;database=studentTable;pwd=123;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand(strSq, con);
com.ExecuteNonQuery();
con.Close();
Bind();
}
}
相关文章推荐
- IOS9中联系人对象的使用及增删改查操作的函数封装
- Django-Model操作数据库(增删改查、连表结构)
- 十、SQLite数据库增删改查操作
- nodejs-第四天-使用MongoDB、shell增删改查操作、聚集集合操作
- SQLite常见操作(增删改查)
- ios Sqlite数据库增删改查基本操作
- 操作XML,对XML的增删改查操作。asp.net C#
- 在ASP.NET 2.0中操作数据之三十:格式化DataList和Repeater的数据
- gridview数据增删改查(当前行操作)
- python list 操作 增删查
- php中操作memcached缓存进行增删改查数据的实现代码
- Elasticsearch 单模式下API的增删改查操作
- 实验二(3)用循环双链表实现对数据的基本操作
- 在ASP.NET 2.0中操作数据之三十五:使用Repeater和DataList单页面实现主/从报表
- 【ssi】增删改查六操作小框架(六)
- nodejs自学之旅(完结篇)—— 简单数据库操作增删改查、表关联and分页排序
- Linux 基本操作实验(2)
- java连接mysql数据库增删改查操作记录
- php基础06_文件增删、目录读取及其他操作
- 栈实验之栈的基本操作