在gridview asp.net上更新取消

时间:2011-06-15 18:25:28

标签: asp.net gridview edit

我正在尝试使用存储过程更新asp.net上的gridview,但它总是重置为原始值。我做错了什么?

编辑:现在添加所有页面代码

  protected void page_PreInit(object sender, EventArgs e)
    {
        MembershipUser UserName;
        try
        {
            if (User.Identity.IsAuthenticated)
            {
                // Set theme in preInit event
                UserName = Membership.GetUser(User.Identity.Name);
                Session["UserName"] = UserName;
            }

        }
        catch (Exception ex)
        {
            string msg = ex.Message;


        }


    }
    protected void Page_Load(object sender, EventArgs e)
    {
        userLabel.Text = Session["UserName"].ToString();

        SqlDataReader myDataReader = default(SqlDataReader);

        SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString);

        SqlCommand command = new SqlCommand("sp_EditRescueDetails", MyConnection);

        if (!User.Identity.IsAuthenticated)
        {
        }
        else
        {
            command.Parameters.AddWithValue("@UserName", userLabel.Text.Trim());

        }

        try
        {
            command.CommandType = CommandType.StoredProcedure;
            MyConnection.Open();


            myDataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
            // myDataReader.Read();


            GridViewED.DataSource = myDataReader;
            GridViewED.DataBind();

            if (GridViewED.Rows.Count >= 1)
            {

                GridViewED.Visible = true;
                lblMsg.Visible = false;

            }
            else if (GridViewED.Rows.Count < 1)
            {
                GridViewED.Visible = false;

                lblMsg.Text = "Your search criteria returned no results.";
                lblMsg.Visible = true;
            }





            MyConnection.Close();
        }
        catch (SqlException SQLexc)
        {
            Response.Write("Read Failed : " + SQLexc.ToString());
        }
    }
    //to edit grid view
    protected void GridViewED_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewED.EditIndex = e.NewEditIndex;

    }


 protected void GridViewED_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString);
        SqlCommand command = new SqlCommand("sp_UpdateRescueDetails", MyConnection);

        if (!User.Identity.IsAuthenticated)
        {
        }
        else
        {
            command.Parameters.AddWithValue("@UserName", userLabel.Text.Trim());

            command.Parameters.Add("@PostalAddress", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[0].Controls[0]).Text;

            command.Parameters.Add("@TelephoneNo", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

            command.Parameters.Add("@Website", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

            command.Parameters.Add("@Email", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

        }


        command.CommandType = CommandType.StoredProcedure;
        MyConnection.Open();

        command.ExecuteNonQuery();


        MyConnection.Close();


        GridViewED.EditIndex = -1;



    }

2 个答案:

答案 0 :(得分:1)

我怀疑在回发时会调用加载网格的代码,这会导致数据在您不希望的情况下从数据库中提取。

是的 - 我认为您希望代码仅在不是回发时加载。您可以使用Page.IsPostBack属性。

这样的事情:

protected void Page_Load(object sender, EventArgs e)
{
    userLabel.Text = Session["UserName"].ToString();

    if (!IsPostBack) {

    SqlDataReader myDataReader = default(SqlDataReader);

    SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString);

    SqlCommand command = new SqlCommand("sp_EditRescueDetails", MyConnection);

    if (!User.Identity.IsAuthenticated)
    {
    }
    else
    {
        command.Parameters.AddWithValue("@UserName", userLabel.Text.Trim());

    }

    try
    {
        command.CommandType = CommandType.StoredProcedure;
        MyConnection.Open();


        myDataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
        // myDataReader.Read();


        GridViewED.DataSource = myDataReader;
        GridViewED.DataBind();

        if (GridViewED.Rows.Count >= 1)
        {

            GridViewED.Visible = true;
            lblMsg.Visible = false;

        }
        else if (GridViewED.Rows.Count < 1)
        {
            GridViewED.Visible = false;

            lblMsg.Text = "Your search criteria returned no results.";
            lblMsg.Visible = true;
        }





        MyConnection.Close();
    }
    catch (SqlException SQLexc)
    {
        Response.Write("Read Failed : " + SQLexc.ToString());
    }
    }
}

答案 1 :(得分:0)

不要在Page_Load事件中绑定您的数据。创建一个单独的方法来执行它,然后在Page Load中,if!IsPostback,调用它。

执行数据绑定的原因是它自己的方法是因为如果在执行任务后正在遍历数据集,删除,更新等,则需要调用它。对方法的调用优于相同的,重复的数据绑定代码的许多实例。