从onclick c#调用2个方法

本文关键字:2个 方法 调用 onclick | 更新日期: 2023-09-27 17:57:30

我一直试图通过单击一次按钮来传递2个SQL查询,所以这就是我一直在尝试做的事情,如下所示,但不起作用。。。。请帮忙。。。提前感谢

所以这是前面的代码

<asp:Button ID="btnWedRecInsert" runat="server" Text="Insert" OnClick="btnWedRecInsert_Click1; btnWedRecInsert_Click2;" />

后面的代码低于

protected void btnWedRecInsert_Click1(object sender, EventArgs e)
{ 
    string Function = ddlFunction.Text;
    string FunctionDate = txtFunctionDate.Text;
    string FunctionTime = ddlFunctionTime.Text;
    string groomName = txtGroomName.Text;
    string groomFatherName = txtGroomFatherName.Text;
    string groomAge = txtGroomAge.Text;
    string groomPhone = txtGroomPhone.Text;
    string groomAddress = txtGroomAddress.Text;
    string brideName = txtBrideName.Text;
    string brideFatherName = txtBrideFatherName.Text;
    string brideAge = txtBrideAge.Text;
    string bridePhone = txtBridePhone.Text;
    string brideAddress = txtBrideAddress.Text;
    string registerName = txtRegisterName.Text;
    string registerPhone = txtRegisterPhone.Text;
    string registerAddress = txtRegisterAddress.Text;
    string referenceName = txtReferenceName.Text;
    string referencePhone = txtReferencePhone.Text;
    string referenceAddress = txtReferenceAddress.Text;

    string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
    OleDbConnection connection = new OleDbConnection(connString);
    string insertQuery = "INSERT INTO wedding(RegisteredDate, Function, FunctionDate, FunctionTime, groomName, groomFatherName, groomAge, groomPhone, groomAddress, brideName, brideFatherName, brideAge, bridePhone, brideAddress, registerName, registerPhone, registerAddress, referenceName, referencePhone, referenceAddress) VALUES( @Date, @Function, @FunctionDate, @FunctionTime, @groomName, @groomFatherName, @groomAge, @groomPhone, @groomAddress, @brideName, @brideFatherName, @brideAge, @bridePhone, @brideAddress, @registerName, @registerPhone, @registerAddress, @referenceName, @referencePhone, @referenceAddress) ";
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = insertQuery;
    command.CommandType = CommandType.Text;
    command.Parameters.AddWithValue("@Date", DateTime.Now.Date);
    command.Parameters.AddWithValue("@Function", Function);
    command.Parameters.AddWithValue("@FunctionDate", FunctionDate);
    command.Parameters.AddWithValue("@FunctionTime", FunctionTime);
    command.Parameters.AddWithValue("@groomName", groomName);
    command.Parameters.AddWithValue("@groomFatherName", groomFatherName);
    command.Parameters.AddWithValue("@groomAge", groomAge);
    command.Parameters.AddWithValue("@groomPhone", groomPhone);
    command.Parameters.AddWithValue("@groomAddress", groomAddress);
    command.Parameters.AddWithValue("@brideName", brideName);
    command.Parameters.AddWithValue("@brideFatherName", brideFatherName);
    command.Parameters.AddWithValue("@brideAge", brideAge);
    command.Parameters.AddWithValue("@bridePhone", bridePhone);
    command.Parameters.AddWithValue("@brideAddress", brideAddress);
    command.Parameters.AddWithValue("@registerName", registerName);
    command.Parameters.AddWithValue("@registerPhone", registerPhone);
    command.Parameters.AddWithValue("@registerAddress", registerAddress);
    command.Parameters.AddWithValue("@referenceName", referenceName);
    command.Parameters.AddWithValue("@referencePhone", referencePhone);
    command.Parameters.AddWithValue("@referenceAddress", referenceAddress);

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
        lblMessage.Text = "Record inserted successfully";
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Unable to insert record";
    }
    finally
    {
        connection.Close();
    }
}

    protected void btnWedRecInsert_Click2(object sender, EventArgs e)
    {
        string Function = ddlReception.Text;
        string FunctionDate = txtReceptionDate.Text;
        string FunctionTime = ddlReceptionTime.Text;
        string groomName = txtGroomName.Text;
        string groomFatherName = txtGroomFatherName.Text;
        string groomAge = txtGroomAge.Text;
        string groomPhone = txtGroomPhone.Text;
        string groomAddress = txtGroomAddress.Text;
        string brideName = txtBrideName.Text;
        string brideFatherName = txtBrideFatherName.Text;
        string brideAge = txtBrideAge.Text;
        string bridePhone = txtBridePhone.Text;
        string brideAddress = txtBrideAddress.Text;
        string registerName = txtRegisterName.Text;
        string registerPhone = txtRegisterPhone.Text;
        string registerAddress = txtRegisterAddress.Text;
        string referenceName = txtReferenceName.Text;
        string referencePhone = txtReferencePhone.Text;
        string referenceAddress = txtReferenceAddress.Text;

        string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
        OleDbConnection connection = new OleDbConnection(connString);
        string insertQuery = "INSERT INTO wedding(RegisteredDate, Function, ReceptionTime, FunctionDate, FunctionTime, groomName, groomFatherName, groomAge, groomPhone, groomAddress, brideName, brideFatherName, brideAge, bridePhone, brideAddress, registerName, registerPhone, registerAddress, referenceName, referencePhone, referenceAddress) VALUES( @Date, @Function, @FunctionDate, @FunctionTime, @groomName, @groomFatherName, @groomAge, @groomPhone, @groomAddress, @brideName, @brideFatherName, @brideAge, @bridePhone, @brideAddress, @registerName, @registerPhone, @registerAddress, @referenceName, @referencePhone, @referenceAddress) ";
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = insertQuery;
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@Date", DateTime.Now.Date);
        command.Parameters.AddWithValue("@Function", Function);
        command.Parameters.AddWithValue("@FunctionDate", FunctionDate);
        command.Parameters.AddWithValue("@FunctionTime", FunctionTime);
        command.Parameters.AddWithValue("@groomName", groomName);
        command.Parameters.AddWithValue("@groomFatherName", groomFatherName);
        command.Parameters.AddWithValue("@groomAge", groomAge);
        command.Parameters.AddWithValue("@groomPhone", groomPhone);
        command.Parameters.AddWithValue("@groomAddress", groomAddress);
        command.Parameters.AddWithValue("@brideName", brideName);
        command.Parameters.AddWithValue("@brideFatherName", brideFatherName);
        command.Parameters.AddWithValue("@brideAge", brideAge);
        command.Parameters.AddWithValue("@bridePhone", bridePhone);
        command.Parameters.AddWithValue("@brideAddress", brideAddress);
        command.Parameters.AddWithValue("@registerName", registerName);
        command.Parameters.AddWithValue("@registerPhone", registerPhone);
        command.Parameters.AddWithValue("@registerAddress", registerAddress);
        command.Parameters.AddWithValue("@referenceName", referenceName);
        command.Parameters.AddWithValue("@referencePhone", referencePhone);
        command.Parameters.AddWithValue("@referenceAddress", referenceAddress);

        try
        {
            connection.Open();
            command.ExecuteNonQuery();
            lblMessage.Text = "Record inserted successfully";
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Unable to insert record";
        }
        finally
        {
            connection.Close();
        }
    }

从onclick c#调用2个方法

您应该将标记更改为:

<asp:Button ID="btnWedRecInsert" runat="server" Text="Insert" OnClick="btnWedRecInsert_Click" />

并将您的代码隐藏更改为:

protected void btnWedRecInsert_Click(object sender, EventArgs e)
{
btnWedRecInsert_Click1();
btnWedRecInsert_Click2();
}

为了可读性,也许您应该将插入函数的名称更改为

insertWedding1();
insertWedding2();

到目前为止,我看不出这两个insertfunction之间有任何区别。也许你以后会改变它们,但它们很相似。用它们做一个带有参数的函数怎么样,这样你就不必写那么多代码了:)例如

insertWedding(object paramWedding1);
insertWedding(object paramWedding2);

将onClick事件更改为btn_{NameOfYourButton}Click然后进行

public void btn_{NameOfYourButton}Click() {
  btnWedRecInsert_Click1(); 
  btnWedRecInsert_Click2();  
}