【问题标题】:how to open a html file on a click of button in new tab in html dynamically?如何在 html 的新选项卡中单击按钮动态打开 html 文件?
【发布时间】:2016-12-07 13:36:23
【问题描述】:

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

我在客户端的代码:

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

我在服务器端的目录代码中创建了一个文件,如下所示: protected void btnAddnew_Click(对象发送者,EventArgs e) { 字符串 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>"

【问题讨论】:

    标签: html asp.net filestream streamwriter system.io.file


    【解决方案1】:

    您可以将锚a标签的target属性设置为_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;" />
    

    【讨论】:

    • strHTMLGrid = strHTMLGrid + "";这不起作用阿迪尔点击按钮它只是刷新不做任何事情..他们像window.open这样的东西我们可以在这里写并传递url..这可能在后面的代码中..请告诉我。谢谢
    • Page.ClientScript.RegisterStartupScript(this.GetType(), "", "fncpopup();", true);在客户端,然后创建一个函数 function fncpopup() { window.open('abc.html', '_blank');这对我很有效,谢谢
    • 但这不会在按钮单击时打开?您可以通过在我的回答中给出的按钮上绑定单击事件来在按钮上运行此脚本。
    【解决方案2】:

    我觉得你应该改成

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

    【讨论】:

    • Kate 感谢您的回复,但这段代码只是刷新了页面..是他们的任何方法,我可以在 window.open 中打开 html 文件。感谢您的回复
    • Page.ClientScript.RegisterStartupScript(this.GetType(), "", "fncpopup();", true);在客户端,然后创建一个函数 function fncpopup() { window.open('abc.html', '_blank');这对我很有用,非常感谢——
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2019-09-20
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    相关资源
    最近更新 更多