【问题标题】:GridView RowCommand update send variable to javascript functionGridView RowCommand 更新将变量发送到 javascript 函数
【发布时间】:2017-07-09 13:30:14
【问题描述】:

我有一个 Gridview,上面有一个更新链接。一旦从其中一行中选择“编辑”,网格中该行中的值就会变为文本框并且可以进行编辑。 “编辑”按钮消失并变为“更新”链接。然后用户编辑一个(或多个)文本框的内容并选择“更新”。将出现一个 JavaScript 确认框,询问用户是否要更新值,并根据所选选项保存或放弃更改。这是由“OnClientClick”执行的 但是,如果验证失败,我想验证更改服务器端并将定义的错误消息传回给用户。例子;如果已将错误格式的注册公司名称输入到所选行中的文本框之一。 然后网格行将保持可编辑状态,直到用户更正错误。可以通过从网格行中选择“取消”来放弃该操作(我已经有了这个功能)。 C#的rowcommand函数是:

  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();


        }
  } 

ASPX 是

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

javascript是

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

感谢您的回复

【问题讨论】:

    标签: asp.net datagridview


    【解决方案1】:

    使用此代码处理服务器端验证。

    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);
                }
            }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 2020-01-13
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多