http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=8ADE535F-AD40-4DE3-A962-A64B4FAF12C4

解决方案 »

  1.   

    你的page_load里怎么写的?
    也可以这样:
    DataGrid1.DataBind();
    Response.Redirect("当前页面的.aspx")
      

  2.   

    少了一句:DataGrid1.EditItemIndex = -1
    另外,你的DataGrid1 的EnableStateView 要为 True
      

  3.   

    用 net_lover(孟子E章) 解决了,十分感谢啊,但我还是没搞懂问题出在哪里,我是用的数据库组件,是不是这个原因导致有的dataset没有即时更新,哪位高人讲解一下啊。谢谢了。
      

  4.   

    我的page_load里是
    sqlDA.Fill(dsFactory);
    DataGrid1.DataBind();
    另外,我全部是用的控件,sqldataadpter,sqlconnection,sqlcommand,是不是这个原因啊。
      

  5.   

    我的page_load里是
    sqlDA.Fill(dsFactory);
    DataGrid1.DataBind();
    另外,我全部是用的控件,sqldataadpter,sqlconnection,sqlcommand,是不是这个原因啊。
    /////////////不是,你必须重新对当前页请求一次才能看见最新的结果
      

  6.   

    为什么又要有一次请求呢,我点击删除后不是已经发了请求了吗,而且我用输入框输入数据点“添加”按钮后datagrid就会立即刷新(当然此时页面也提交、刷新了),但删除和提交有什么不同吗?为什么要再请求?
      

  7.   

    代码如下,各位大哥帮我看看吧,谢了。using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace localhost
    {
    /// <summary>
    /// manage_factory 的摘要说明。
    /// </summary>
    public class admin_factory : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox txt_factory_name;
    protected System.Web.UI.WebControls.TextBox txt_factory_description;
    protected System.Web.UI.WebControls.DropDownList DropDownList_parent;
    protected System.Web.UI.WebControls.RadioButton radio1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
    protected System.Web.UI.WebControls.RadioButton radio2;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Data.SqlClient.SqlDataAdapter sqlDA;
    protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
    protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
    protected System.Data.SqlClient.SqlConnection sqlConn;
    protected System.Data.SqlClient.SqlCommand sqlCmd;
    protected localhost.dsFactory dsFactory;
    protected System.Web.UI.WebControls.Button bt_confirm; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    /* string sql = "SELECT FactoryID, FactoryCName, FactoryEName, FactoryCountory, FactoryDes, FactoryURL FROM tb_factory";
    SqlConnection Conn = new SqlConnection("workstation id=ZORRO2003;packet size=4096;user id=sa;data source=127.0.0.1;persist security info=True;initial catalog=ecarzone;password=ease2pass");
    SqlDataAdapter sqlADP = new SqlDataAdapter();
    sqlADP.SelectCommand = new SqlCommand(sql,Conn);
    SQLDataSetCommand cmd = new SQLDataSetCommand(sql,Conn); DataSet aa = new DataSet();
    int iRowCount = cmd.FillDataSet(aa);
    */ sqlDA.Fill(dsFactory);
    DataGrid1.DataBind();
    //Response.Write("<SCRIPT LANGUAGE='javascript'>alert('刷新了!');</SCRIPT>"); } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlDA = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlConn = new System.Data.SqlClient.SqlConnection();
    this.sqlCmd = new System.Data.SqlClient.SqlCommand();
    this.dsFactory = new localhost.dsFactory();
    ((System.ComponentModel.ISupportInitialize)(this.dsFactory)).BeginInit();
    this.bt_confirm.Click += new System.EventHandler(this.bt_confirm_Click);
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
    // 
    // sqlDA
    // 
    this.sqlDA.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDA.InsertCommand = this.sqlInsertCommand1;
    this.sqlDA.SelectCommand = this.sqlSelectCommand1;
    this.sqlDA.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
    new System.Data.Common.DataTableMapping("Table", "tb_factory", new System.Data.Common.DataColumnMapping[] {
      new System.Data.Common.DataColumnMapping("FactoryID", "FactoryID"),
      new System.Data.Common.DataColumnMapping("FactoryCName", "FactoryCName"),
      new System.Data.Common.DataColumnMapping("FactoryEName", "FactoryEName"),
      

  8.   

    new System.Data.Common.DataColumnMapping("FactoryCountory", "FactoryCountory"),new System.Data.Common.DataColumnMapping("FactoryDes", "FactoryDes"), new System.Data.Common.DataColumnMapping("FactoryURL", "FactoryURL")})});
    this.sqlDA.UpdateCommand = this.sqlUpdateCommand1;
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT FactoryID, FactoryCName, FactoryEName, FactoryCountory, FactoryDes, Factor" +
    "yURL FROM tb_factory";
    this.sqlSelectCommand1.Connection = this.sqlConn;
    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = @"INSERT INTO tb_factory(FactoryCName, FactoryEName, FactoryCountory, FactoryDes, FactoryURL) VALUES (@FactoryCName, @FactoryEName, @FactoryCountory, @FactoryDes, @FactoryURL); SELECT FactoryID, FactoryCName, FactoryEName, FactoryCountory, FactoryDes, FactoryURL FROM tb_factory WHERE (FactoryID = @@IDENTITY)";
    this.sqlInsertCommand1.Connection = this.sqlConn;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryCName", System.Data.SqlDbType.VarChar, 50, "FactoryCName"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryEName", System.Data.SqlDbType.VarChar, 50, "FactoryEName"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryCountory", System.Data.SqlDbType.VarChar, 50, "FactoryCountory"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryDes", System.Data.SqlDbType.VarChar, 2147483647, "FactoryDes"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryURL", System.Data.SqlDbType.VarChar, 200, "FactoryURL"));
    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = @"UPDATE tb_factory SET FactoryCName = @FactoryCName, FactoryEName = @FactoryEName, FactoryCountory = @FactoryCountory, FactoryDes = @FactoryDes, FactoryURL = @FactoryURL WHERE (FactoryID = @Original_FactoryID) AND (FactoryCName = @Original_FactoryCName OR @Original_FactoryCName IS NULL AND FactoryCName IS NULL) AND (FactoryCountory = @Original_FactoryCountory OR @Original_FactoryCountory IS NULL AND FactoryCountory IS NULL) AND (FactoryEName = @Original_FactoryEName OR @Original_FactoryEName IS NULL AND FactoryEName IS NULL) AND (FactoryURL = @Original_FactoryURL OR @Original_FactoryURL IS NULL AND FactoryURL IS NULL); SELECT FactoryID, FactoryCName, FactoryEName, FactoryCountory, FactoryDes, FactoryURL FROM tb_factory WHERE (FactoryID = @FactoryID)";
    this.sqlUpdateCommand1.Connection = this.sqlConn;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryCName", System.Data.SqlDbType.VarChar, 50, "FactoryCName"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryEName", System.Data.SqlDbType.VarChar, 50, "FactoryEName"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryCountory", System.Data.SqlDbType.VarChar, 50, "FactoryCountory"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryDes", System.Data.SqlDbType.VarChar, 2147483647, "FactoryDes"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryURL", System.Data.SqlDbType.VarChar, 200, "FactoryURL"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryID", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryCName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryCName", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryCountory", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryCountory", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryEName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryEName", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryURL", System.Data.SqlDbType.VarChar, 200, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryURL", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FactoryID", System.Data.SqlDbType.Int, 4, "FactoryID"));
    // 
    // sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = @"DELETE FROM tb_factory WHERE (FactoryID = @Original_FactoryID) AND (FactoryCName = @Original_FactoryCName OR @Original_FactoryCName IS NULL AND FactoryCName IS NULL) AND (FactoryCountory = @Original_FactoryCountory OR @Original_FactoryCountory IS NULL AND FactoryCountory IS NULL) AND (FactoryEName = @Original_FactoryEName OR @Original_FactoryEName IS NULL AND FactoryEName IS NULL) AND (FactoryURL = @Original_FactoryURL OR @Original_FactoryURL IS NULL AND FactoryURL IS NULL)";
    this.sqlDeleteCommand1.Connection = this.sqlConn;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryID", System.Data.DataRowVersion.Original, null));
      

  9.   

    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryCName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryCName", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryCountory", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryCountory", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryEName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryEName", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FactoryURL", System.Data.SqlDbType.VarChar, 200, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FactoryURL", System.Data.DataRowVersion.Original, null));
    // 
    // sqlConn
    // 
    this.sqlConn.ConnectionString = "workstation id=ZORRO2003;packet size=4096;user id=sa;data source=\"127.0.0.1\";pers" +
    "ist security info=True;initial catalog=ecarzone;password=ease2pass";
    // 
    // sqlCmd
    // 
    this.sqlCmd.Connection = this.sqlConn;
    // 
    // dsFactory
    // 
    this.dsFactory.DataSetName = "dsFactory";
    this.dsFactory.Locale = new System.Globalization.CultureInfo("zh-CN");
    this.Load += new System.EventHandler(this.Page_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dsFactory)).EndInit(); }
    #endregion private void bt_confirm_Click(object sender, System.EventArgs e)
    {
    if(Page.IsValid == true)
    {
    if(sqlConn.State!=ConnectionState.Open)
    {
    string factory_country;
    if(radio1.Checked == true)
    {
    factory_country = "国内";
    }
    else
    {
    factory_country = "国外";
    };
    sqlCmd.CommandText = "INSERT INTO tb_factory (FactoryCName, FactoryDes, FactoryCountory) VALUES ('" + txt_factory_name.Text + "','" + txt_factory_description.Text + "','"+ factory_country +"')";
    sqlConn.Open();
    }
    sqlCmd.ExecuteNonQuery();
    sqlDA.Fill(dsFactory);
    DataGrid1.DataBind();
    txt_factory_name.Text = "";
    txt_factory_description.Text = "";
    }
    }
      

  10.   

    将你的数据绑定放在Page_PreRender中就可以了。
    不然你就要进行两次绑定才能解决问题。