Q:

GridView RowCommand update send variable to javascript function

I have a Gridview that has an update link on it. Once "Edit" is selected from one of the rows the values in that row in the grid change into text boxes and can be edited. The "edit" button disappears and changes into an "update" link. Then the user edits the contents of one (or more than one) of the text boxes and "update" is selected. A JavaScript confirmation box appears asking if the user wants to update the values(s) and the changes are saved or discarded depending upon the choice selected. This is performed by "OnClientClick"
However I want to validate the change server side and pass back a defined error message to the user if the validation has failed. Example; if registered company name in the wrong format has been entered into one of the text boxes in the row selected.
The grid row will then remain in an editable state until the user corrects the error. The operation can be discarded by selecting "Cancel" from the grid row (I already have this functionality).
The C# rowcommand function is:
  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            // get the primary key id of the clicked row
            int id = Convert.ToInt32(e.CommandArgument);
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["moduleConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE suppliers SET Registered_company_name=@registered_company_name WHERE Supplier_code=@supplier_code;";

   System.Web.UI.WebControls.TextBox myTextBox_registered_company_name = GridView1.Rows[id].FindControl("Registered_company_name") as System.Web.UI.WebControls.TextBox;
            cmd.Parameters.Add("@registered_company_name", SqlDbType.VarChar).Value = myTextBox_registered_company_name.Text;

            System.Web.UI.WebControls.Label myLabel = GridView1.Rows[id].FindControl("suppliercode") as System.Web.UI.WebControls.Label;
            cmd.Parameters.Add("@supplier_code", SqlDbType.VarChar).Value = Convert.ToInt32(myLabel.Text);

    cmd.Connection = con;
            con.Open();

            cmd.ExecuteNonQuery();
            con.Close();

            GridView1.EditIndex = -1;
            BindData();

        }
  } 

The ASPX is
   <asp:LinkButton ID="lnkUpdate" runat="server" CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return ConfirmOnUpdate();" CommandName="Update"  > Update</span></asp:LinkButton>

The javascript is 
  function ConfirmOnUpdate()
      {
          if (confirm("Are you sure you want to update this record?"))
              return true;
          else
              return false;
      }

Thank you for any replies

A:

Use this code for handling server side validation.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            // get the primary key id of the clicked row
            int id = Convert.ToInt32(e.CommandArgument);

            System.Web.UI.WebControls.TextBox myTextBox_registered_company_name = GridView1.Rows[id].FindControl("Registered_company_name") as System.Web.UI.WebControls.TextBox;

            if (myTextBox_registered_company_name.Text != "") // add your validation in this if block, now it checks textbox is not empty;
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["moduleConnectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "UPDATE suppliers SET Registered_company_name=@registered_company_name WHERE Supplier_code=@supplier_code;";

                System.Web.UI.WebControls.TextBox myTextBox_registered_company_name = GridView1.Rows[id].FindControl("Registered_company_name") as System.Web.UI.WebControls.TextBox;
                cmd.Parameters.Add("@registered_company_name", SqlDbType.VarChar).Value = myTextBox_registered_company_name.Text;

                System.Web.UI.WebControls.Label myLabel = GridView1.Rows[id].FindControl("suppliercode") as System.Web.UI.WebControls.Label;
                cmd.Parameters.Add("@supplier_code", SqlDbType.VarChar).Value = Convert.ToInt32(myLabel.Text);

                cmd.Connection = con;
                con.Open();

                cmd.ExecuteNonQuery();
                con.Close();

                GridView1.EditIndex = -1;
                BindData();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this,this.GetType(),"Error Message", "alert('TextBox is empty!')",true);
            }
        }

Hope this can help you!

