Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gridview edit index being set to 0 after postback problem

Status
Not open for further replies.

Crowley16

Technical User
Jan 21, 2004
6,931
GB
Hi Everyone.

I'm using asp.net 2.0, .net framework 3.5...

I've got a gridview control with normal command buttons and I've used GetPostBackEventReference to add an event against an entire row, where when enter is pressed against the row during edit, it automatically saves.

However, the problem I'm getting is that, when pressing enter, the record saves, but the edit index on the gridview is set to 0, i.e. the edit mode jumps to the first row, instead of no rows being edited.

If I use the standard command buttons, then this doesn't happen. If I'm already editing the first row, then it doesnt' happen.
Also, this doesn't happen in firefox.

Anyone got any ideas of why this is happening and how to stop it?

Thanks

relevant code snippet
Code:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
...
	string update = ClientScript.GetPostBackEventReference(btnSave, "") + "; ";
	e.Row.Attributes.Add("onkeypress", "if(event.keyCode == 13) { " + update + "}");
...
}

--------------------
Procrastinate Now!
 
there's a fair bit of code in the save event...

Code:
    protected void EmployeesGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
		try
		{
			//warning message for modifying employee codes
			if 
			(
				e.OldValues["OperatorCode"] != null && e.NewValues["OperatorCode"] != null
				&& e.OldValues["OperatorCode"].ToString() != "*New*"
				&& (e.NewValues["OperatorCode"].ToString() != e.OldValues["OperatorCode"].ToString())
			)
				lblMessage.Text = "Operator Code already exists, new operator created.";
			else
				lblMessage.Text = "";

			//check operator code has been filled in
			if 
			(
				e.NewValues["OperatorCode"].ToString() == "" 
				|| e.NewValues["OperatorCode"].ToString() == null 
				|| e.NewValues["OperatorCode"].ToString() == "*New*"
			)
			{
				throw new Exception("Please input a valid Operator Code");
			}

			//input checking
			try
			{
				System.Convert.ToInt32(e.NewValues["StoreID"]);
				System.Convert.ToDecimal(e.NewValues["ssBalanceDiscount"]);
				System.Convert.ToDecimal(e.NewValues["ssBalancePercent"]);
				System.Convert.ToInt32(e.NewValues["ssBalancePair"]);
				System.Convert.ToDecimal(e.NewValues["ssPairAllowance"]);
			}
			catch (Exception CustEx)
			{
				throw new Exception("Invalid input for numeric fields.", CustEx);
			} 

			//handling the drop down list for grades
			DropDownList ddl = (DropDownList) EmployeesGrid.Rows[e.RowIndex].FindControl("Grades");
			
			//initiate a connection to database and stored procedure
			SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["MainDB"].ConnectionString);
			cnn.Open();
			SqlCommand cmd = new SqlCommand("usp_InsStaff", cnn);
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add("@OperatorCode", SqlDbType.VarChar);
			cmd.Parameters.Add("@OperatorName", SqlDbType.VarChar);
			cmd.Parameters.Add("@Grade", SqlDbType.Int);
			cmd.Parameters.Add("@PayrollNr", SqlDbType.VarChar);
			cmd.Parameters.Add("@StoreID", SqlDbType.Int);
			cmd.Parameters.Add("@ssBalanceDiscount", SqlDbType.Money);
			cmd.Parameters.Add("@ssBalancePair", SqlDbType.Int);
			cmd.Parameters.Add("@IsUpdatePending", SqlDbType.Bit);

			cmd.Parameters["@OperatorCode"].Value = e.NewValues["OperatorCode"].ToString();
			cmd.Parameters["@OperatorName"].Value = e.NewValues["OperatorName"].ToString();
			cmd.Parameters["@Grade"].Value = ddl.SelectedValue;
			cmd.Parameters["@PayrollNr"].Value = e.NewValues["PayrollNr"].ToString();
			cmd.Parameters["@StoreID"].Value = e.NewValues["StoreID"].ToString();

			//handle nullable parameters
			if (e.NewValues["ssBalanceDiscount"] == null || e.NewValues["ssBalanceDiscount"] == "")
				cmd.Parameters["@ssBalanceDiscount"].Value = null;
			else
				cmd.Parameters["@ssBalanceDiscount"].Value = e.NewValues["ssBalanceDiscount"].ToString();

			if (e.NewValues["ssBalancePair"] == null || e.NewValues["ssBalancePair"] == "")
				cmd.Parameters["@ssBalancePair"].Value = null;
			else
				cmd.Parameters["@ssBalancePair"].Value = e.NewValues["ssBalancePair"].ToString();

			cmd.Parameters["@IsUpdatePending"].Value = e.NewValues["IsUpdatePending"].ToString();

			//run update sproc
			cmd.ExecuteNonQuery();
		}
		catch (Exception ex)
		{
			lblMessage.Text = ex.Message.ToString();
		}
		finally
		{
			//this is an important step. After the update, if a user manually refreshes, the page retains it's updating status
			//because the update method is post. A popup box appears prompting for retry/cancel, if retry is hit,
			//form will re-do the entire update procedure. 
			//Having this code here will make sure that any post method calls are cleared up after editing.
			Response.Redirect("~/Secure/Employees.aspx");
		}
    }

--------------------
Procrastinate Now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top