如何在 asp.net 按钮 onclick 上 运行 2 个函数
How to Run 2 functions on asp.net button onclick
我想要 运行 2 个函数,即 displayTable() 和 ExportCSV() on RunKDF_OnClick按钮,但我只能 运行 一个 函数,即 ExportCSV()。
下面是我的代码。
public void RunKDF_OnClick(object sender, EventArgs e)
{
ConnectionInfo connectionInfo = new ConnectionInfo("xx.xx.xx.xxx", xx, "root", new
AuthenticationMethod[]
{
// Pasword based Authentication
new PasswordAuthenticationMethod("root","xxxx"),
});
using (SshClient ssh = new SshClient(connectionInfo))
{
ssh.Connect();
ssh.RunCommand("perl -w /tmp/ThermalValue/bin/kdf2csv/thermalKDF.pl");
ssh.RunCommand("exit");
ssh.Disconnect();
}
displayTable();
ExportCSV();
}
public void displayTable()
{
string connectionString = @"Data Source = xxxxx; port = xxxx; Initial Catalog = xxxx; User Id = xxxxx; password = xxxxx";
using (MySqlConnection sqlCon = new MySqlConnection(connectionString))
{
sqlCon.Open();
MySqlDataAdapter sqlDa = new MySqlDataAdapter("SELECT UID AS UNIT_ID ,Tvalue AS Temperature_Value , File AS KDF_File_Name FROM OEE_PROD.thermal", sqlCon);
DataTable table = new DataTable();
sqlDa.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
}
//这是ExportCSV方法。
public void ExportCSV()
{
字符串 conn = @"Data Source = xxxxx; port = xxxx; Initial Catalog = OEE_PROD; User Id = xxxx; password = xxxxx";
使用 (MySqlConnection con = new MySqlConnection(conn))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT UID AS UNIT_ID, Tvalue AS Temperature_Value, File AS KDF_File_Name FROM OEE_PROD.thermal"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
foreach (DataColumn column in dt.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "\r\n";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=KDFExport_"+DateTime.Now+".csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
}
}
}
}
//这是我的gridview的aspx
<asp:GridView CssClass="GridView" Width="100%" ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings FirstPageText="First" LastPageText="Last" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
问题是您提前致电 Response.End
。
https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.end?view=netframework-4.8
ends all currently buffered output to the client, stops execution of
the page, and raises the EndRequest event
我想要 运行 2 个函数,即 displayTable() 和 ExportCSV() on RunKDF_OnClick按钮,但我只能 运行 一个 函数,即 ExportCSV()。 下面是我的代码。
public void RunKDF_OnClick(object sender, EventArgs e)
{
ConnectionInfo connectionInfo = new ConnectionInfo("xx.xx.xx.xxx", xx, "root", new
AuthenticationMethod[]
{
// Pasword based Authentication
new PasswordAuthenticationMethod("root","xxxx"),
});
using (SshClient ssh = new SshClient(connectionInfo))
{
ssh.Connect();
ssh.RunCommand("perl -w /tmp/ThermalValue/bin/kdf2csv/thermalKDF.pl");
ssh.RunCommand("exit");
ssh.Disconnect();
}
displayTable();
ExportCSV();
}
public void displayTable()
{
string connectionString = @"Data Source = xxxxx; port = xxxx; Initial Catalog = xxxx; User Id = xxxxx; password = xxxxx";
using (MySqlConnection sqlCon = new MySqlConnection(connectionString))
{
sqlCon.Open();
MySqlDataAdapter sqlDa = new MySqlDataAdapter("SELECT UID AS UNIT_ID ,Tvalue AS Temperature_Value , File AS KDF_File_Name FROM OEE_PROD.thermal", sqlCon);
DataTable table = new DataTable();
sqlDa.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
}
//这是ExportCSV方法。
public void ExportCSV() { 字符串 conn = @"Data Source = xxxxx; port = xxxx; Initial Catalog = OEE_PROD; User Id = xxxx; password = xxxxx"; 使用 (MySqlConnection con = new MySqlConnection(conn)) {
using (MySqlCommand cmd = new MySqlCommand("SELECT UID AS UNIT_ID, Tvalue AS Temperature_Value, File AS KDF_File_Name FROM OEE_PROD.thermal"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
foreach (DataColumn column in dt.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "\r\n";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=KDFExport_"+DateTime.Now+".csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
}
}
}
}
//这是我的gridview的aspx
<asp:GridView CssClass="GridView" Width="100%" ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings FirstPageText="First" LastPageText="Last" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
问题是您提前致电 Response.End
。
https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.end?view=netframework-4.8
ends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event