日期:2014-05-18  浏览次数:21097 次

我的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.It