【问题标题】:Pass variable defined on button click to another button click c#将按钮单击定义的变量传递给另一个按钮单击c#
【发布时间】:2015-11-17 13:00:44
【问题描述】:

我正在编写一个 ASP.NET 应用程序,通过单击按钮执行这些操作

  1. 使用 GUID 在 App_Data 文件夹中创建一个唯一目录
  2. 将exe文件和excel模板复制到唯一目录中
  3. 从网页输入写入一个 txt 文件
  4. 运行exe文件
  5. exe 生成一个输出 txt 文件
  6. 将输出数据放到网页上的asp表格中
  7. 将txt文件中的输出数据写入excel模板中

点击第二个按钮

  1. 下载带有数据的唯一 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 文件,因此理想情况下我需要单击第二个按钮。

【问题讨论】:

  • 最简单的方法是使用您在 btn1_click 中设置并进入 btn2_click 的全局变量?
  • 使用Session["path"],第一个按钮被点击后首先赋值,第二个按钮被点击后读取变量。

标签: c# asp.net button directory unique


【解决方案1】:

您的方法不正确,因为可以按任何顺序单击按钮,但如果您要走这条路线,您可以这样做。

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

【讨论】:

  • 使用这种方法,您将仅在单击第一个按钮后为第二个按钮设置按钮单击事件。我还建议在单击第一个按钮之前隐藏第二个按钮。
  • 感谢您的帮助。它几乎可以工作。执行前面的代码后出现按钮 2,但是当我单击它时,文件不会下载。当我从 Button2.click 事件中取出文件时,文件下载正常。
  • 我刚刚进行了测试,第二个按钮工作正常。你能用你现在所拥有的来更新你的问题吗?可能还有其他问题。
【解决方案2】:

感谢@metal_man Session 工作很愉快。漂亮又简单。

我在定义我的唯一 Excel 路径后添加了这个。

    Session["excelPath"] = excelOutput;

这是我的 button2 点击

    string excelOutput = (string)Session["excelPath"];

关于会话状态的好文章 http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 2019-04-07
    • 2019-06-13
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多