将按钮单击时定义的变量传递到另一个按钮单击c#
本文关键字:单击 按钮 另一个 定义 变量 | 更新日期: 2023-09-27 18:22:10
我正在编写一个ASP.NET应用程序,单击按钮即可执行以下操作
- 使用GUID在App_Data文件夹中创建唯一目录
- 将exe文件和excel模板复制到唯一目录中
- 根据网页输入编写一个txt文件
- 运行exe文件
- exe生成一个输出txt文件
- 将输出数据放入网页上的asp表中
- 将txt文件中的输出数据写入excel模板
第二个按钮点击
- 下载带有数据的唯一excel文件
现在所有涉及的路径都是变量。我想做的是点击第二个按钮,让你下载充满数据的独特excel文件。然而,无论我尝试什么,我都无法在第二次点击按钮时使用excel文件的唯一路径。
那么,我如何在第二次点击中传递第一次点击中定义的变量呢。重要的是,在第一次单击按钮时创建唯一的目录。
这是一些代码。有很多,所以我试图只张贴重要的东西
public void Button1_Click(object sender, EventArgs e)
{
UniqueDirectory d = new UniqueDirectory();
string dirPath = Request.MapPath("~/App_Data/"); // point to the directory of wispp on the server
string wisppExe = Server.MapPath("~/App_Data/WisppNew.exe"); // point to the location of wispp on the server
string fortranLib = Server.MapPath("~/App_Data/libf60rts.dll"); // point to the location of the fortran libray file on the server
string excelTemplate = Server.MapPath("~/App_Data/WISPP_Template.xls");
do
{
Guid guid = Guid.NewGuid();
string uniqueSubFolderName = guid.ToString();
string uniquePath = dirPath + uniqueSubFolderName;
}
while (Directory.Exists(d.uniquePath));
Directory.CreateDirectory(d.uniquePath);
//copy files to new unique directory
string wisppUnique = Path.Combine(d.uniquePath, "WisppNew.exe");
string fortranUnique = Path.Combine(d.uniquePath, "libf60rts.dll");
string wisppGraphUnique = Path.Combine(d.uniquePath, "WISPPGRA");
string excelUnique = Path.Combine(d.uniquePath, "WISPP_Template.xls");
string excelOutput = Path.Combine(d.uniquePath, "WISPP_Output.xls");
File.Copy(wisppExe, wisppUnique, true);
File.Copy(fortranLib, fortranUnique, true);
File.Copy(excelTemplate, excelUnique, true);
//write data into excel
FileStream outputFile = new FileStream(excelOutput, FileMode.OpenOrCreate, FileAccess.Write);
xssfworkbook.Write(outputFile);
outputFile.Close();
}
public void Button2_Click(object sender, EventArgs e)
{
//here I want to use variable excelOutput
FileInfo file = new FileInfo(excelOutput);
if(file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "Application/x-msexcel";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
}
}
希望这是有道理的。我曾尝试将代码的响应部分放在第一次单击按钮中,但由于某种原因,这使得数据表不可见。此外,我希望用户可以选择下载excel文件,所以我理想情况下需要第二次点击按钮。
你的方法是不对的,因为按钮可以按任何顺序单击,但如果你要走这条路,你可以这样做。
//hide second button
yourButton2.Visible = false;
public void Button1_Click(object sender, EventArgs e)
{
UniqueDirectory d = new UniqueDirectory();
string dirPath = Request.MapPath("~/App_Data/"); // point to the directory of wispp on the server
string wisppExe = Server.MapPath("~/App_Data/WisppNew.exe"); // point to the location of wispp on the server
string fortranLib = Server.MapPath("~/App_Data/libf60rts.dll"); // point to the location of the fortran libray file on the server
string excelTemplate = Server.MapPath("~/App_Data/WISPP_Template.xls");
do
{
Guid guid = Guid.NewGuid();
string uniqueSubFolderName = guid.ToString();
string uniquePath = dirPath + uniqueSubFolderName;
}
while (Directory.Exists(d.uniquePath));
Directory.CreateDirectory(d.uniquePath);
//copy files to new unique directory
string wisppUnique = Path.Combine(d.uniquePath, "WisppNew.exe");
string fortranUnique = Path.Combine(d.uniquePath, "libf60rts.dll");
string wisppGraphUnique = Path.Combine(d.uniquePath, "WISPPGRA");
string excelUnique = Path.Combine(d.uniquePath, "WISPP_Template.xls");
string excelOutput = Path.Combine(d.uniquePath, "WISPP_Output.xls");
File.Copy(wisppExe, wisppUnique, true);
File.Copy(fortranLib, fortranUnique, true);
File.Copy(excelTemplate, excelUnique, true);
//write data into excel
FileStream outputFile = new FileStream(excelOutput, FileMode.OpenOrCreate, FileAccess.Write);
xssfworkbook.Write(outputFile);
outputFile.Close();
yourButton2.Visible = true;
yourButton2.Click += (_sender, _e) =>
{
//excelOutput is still in scope
FileInfo file = new FileInfo(excelOutput);
if(file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "Application/x-msexcel";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
}
};
}
谢谢@metal_man会话真是太棒了。漂亮又简单。
我在定义了我独特的excel路径后添加了这个。
Session["excelPath"] = excelOutput;
这是我的按钮2点击
string excelOutput = (string)Session["excelPath"];
关于会话状态的好文章http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net