1

当用户单击按钮时,我必须创建一个功能,而不是在 VS 中的项目名称中动态生成 html 文件,然后在新选项卡中打开它。

我在客户端的代码:

 <asp:button  ID="BtnGenrateHTML" runat="server" text="   Generate HTML  " OnClick="btnAddnew_Click"  />

我在服务器端的目录代码中创建了一个文件,如下所示: protected void btnAddnew_Click(object sender, EventArgs e) { string sFileFullName; 字符串 sFilePath; 字符串 sFileName;

        string strHTMLGrid = "";
        strHTMLGrid = strHTMLGrid + "Dear Customer,<BR><BR> Please provide below OTP to complete registration <BR><BR> ";
        strHTMLGrid = strHTMLGrid + "<BR><BR> This OTP is valid for 15 minutes.";
        strHTMLGrid = strHTMLGrid + "<BR><BR> With Best Regards - Indiefy";
        This is not working //strHTMLGrid= strHTMLGrid + "<a href="abc.html/">thesitewizard.com</a>"
        sFilePath = Server.MapPath("");
       sFileName = "abc.html";
        sFileFullName = sFilePath + "\\" + sFileName;
        if (!Directory.Exists(sFileFullName))
        {
            Directory.CreateDirectory(sFilePath);
        }
        // if it exist than to delete it.
        if (System.IO.File.Exists(sFileFullName))
        {
            System.IO.File.Delete(sFileFullName);
        }

        // If it deleted than we need to create it again
        FileStream fs = new FileStream(sFileFullName, FileMode.Create);
        using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
        {
           w.WriteLine(strHTMLGrid);
        }

        fs.Close();
    }

如何abc.html通过单击按钮打开我的文件?请指导我该怎么做。

这在服务器端不起作用:

  strHTMLGrid= strHTMLGrid + "<a href="abc.html/">thesitewizard.com</a>"
4

2 回答 2

0

您可以将锚标记标签的目标属性设置为_blank新选项卡中打开链接。

<a href="abc.html" target="_blank"></a>

您在双引号中使用双引号会导致错误,您需要使用反斜杠转义双引号,如下所示。

strHTMLGrid= strHTMLGrid + "<a href=\"abc.html\" target=\"_blank\"></a>"; 

编辑该按钮似乎不在 abc.html 中,您正在尝试在 abc.html 中添加锚点,这将打开页面 abc.html 这可能不是您要寻找的。如果您在其他一些 html 文件中有按钮说 test.html 并想从中打开 abc.html 然后将按钮更改为锚并将其样式表看起来像按钮。

改变

<asp:button  ID="BtnGenrateHTML" runat="server" text="   Generate HTML  " OnClick="btnAddnew_Click"  />

<a href="abc.html" target="_blank">Generate HTML</a>

如果你想使用按钮,那么你可以使用 window.open

 <asp:button  ID="BtnGenrateHTML" runat="server" text="   Generate HTML  " 
  OnClick="btnAddnew_Click" OnClientClick="window.open('abc.html', '_blank'); return false;" />
于 2016-08-02T09:18:29.080 回答
0

我认为您应该将其更改为

strHTMLGrid= strHTMLGrid + "<a href='abc.html'>thesitewizard.com</a>"
于 2016-08-02T09:21:18.437 回答