实验2 -DataList 增删改查等操作

 

具体效果如下所示:

  1. 使用Datalist显示数据,
  2. 新增数据,
  3. 修改
  4. 删除数据等操作
  5. 主要设置下面两个属性修改显示的外观。

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();

    }

}

posted @ 2009-03-25 20:06  花****半  阅读(449)  评论(0编辑  收藏  举报