代码人生的小狗窝

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_C#

小弟我的datalist 里的Updata事件,要点2次更新按钮才行

发布时间:2019-12-10浏览(1898)

    我的datalist 里的Updata事件,要点2次更新按钮才行
    C# code
    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 Default2 : System.Web.UI.Page
    {
       private string connStr = ConfigurationManager.ConnectionStrings["gbstr"].ToString();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
                int CurPage;
                if (Request.QueryString["Page"] != null)
                    CurPage = Convert.ToInt32(Request.QueryString["Page"]);
                else
                    CurPage = 1;
                //连接数据库及将数据封装到一个数据集中
                SqlConnection conn = new SqlConnection(connStr);
                SqlDataAdapter adapter = new SqlDataAdapter("select * from Post", conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Post");
    
                PagedDataSource ps = new PagedDataSource();
                ps.DataSource = ds.Tables["Post"].DefaultView;
                ps.AllowPaging = true;
                //每个页面显示的条数
                ps.PageSize = 5;
                onepage.Text = ps.PageSize.ToString();
                //求数据的总数
                allmsg.Text = ps.DataSourceCount.ToString();
                ps.CurrentPageIndex = CurPage - 1;
                //求总页
                allpage.Text = ps.PageCount.ToString();
                allpages.Text = ps.PageCount.ToString();
                nowpage.Text = CurPage.ToString();
                //将数据源与控件绑定
    
                this.DataList1.DataSource = ps;
                
                if (!IsPostBack)
                {    
                    this.DataList1.DataBind();
                }
    
                //上一页
                if (!ps.IsFirstPage)
                {
                    firstpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
                    prepage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
                }
                //下一页
                if (!ps.IsLastPage)
                {
                    nextpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                    endpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(ps.PageCount);
                }
    
          }
              
        protected void BtnJump_Click(object sender, EventArgs e)
        {
            if (this.TxtPageNum.Text != null)
            {
                Response.Redirect(Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(this.TxtPageNum.Text));
               
            }
        }
        protected void BtnPost_Click(object sender, EventArgs e)
        {
            string _UserName = this.TxtName.Text;
            string _UserPwd = this.TxtPwd.Text;
            string _PostContent = this.TxtContent.Text;
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["gbstr"].ToString();
            SqlConnection myStr = new SqlConnection(connStr);
            myStr.Open();
            SqlCommand Insert = new SqlCommand("insert into Post(UserName,UserPwd,PostContent) values('" + _UserName + "','" + _UserPwd + "','" + _PostContent + "')", myStr);
            Insert.ExecuteNonQuery();
            Response.Redirect(Request.CurrentExecutionFilePath); 
    
        }
    
        protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.DataList1.EditItemIndex = e.Item.ItemIndex;//编辑
            this.DataList1.DataBind();
            
        
    
        }
      
    
    
        protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
        {
    
    
            string id = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
            string postcontent = ((TextBox)e.Item.FindControl("TxtEdit")).Text;
            //((TextBox)e.Item.FindControl("TxtEdit")).Attributes.Add("onchange()", "TextChange()");
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand Update = new SqlCommand("update Post set PostContent='" + postcontent + "'where ID='" + id + "'", conn);
            Update.ExecuteNonQuery();
            this.DataList1.EditItemIndex = -1;
            this.DataList1.DataBind();
            Response.Redirect(Request.CurrentExecutionFilePath); 
    
            
        }
    
        protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            
            string id = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand delcmd = new SqlCommand("delete from Post where ID='" + id + "'", conn);
            //((TextBox)e.Item.FindControl("LbtnDelete")).Attributes.Add("onClick", "return confirm('" + "确认删除?" + "')");
            delcmd.ExecuteNonQuery();
            this.DataList1.DataBind();
            Response.Redirect(Request.CurrentExecutionFilePath); 
        }
    }