RegisterStartupScript不起作用,这表示更新面板无法在asp.net的新选项卡中打开文件 [英] RegisterStartupScript not working indise an Update Panel to open file in new tab in asp.net
问题描述
我正在尝试使用更新面板中的ScriptManager.RegisterStartupScript在新选项卡中打开文件,但无法正常工作.代码如下:
I am trying to open a file in new tab using ScriptManager.RegisterStartupScript inside update panel, but its not working. The Code is as below :
filename = Server.UrlEncode(filename);
string js = "<script>window.open('ViewReports.aspx?filename=" + Server.UrlEncode(filename) + "', '_newtab');</script>";
ScriptManager.RegisterStartupScript(UpdatePanel1,UpdatePanel1.GetType(),"Pop up",js,true);
这也行不通:
This also not working:
ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), js, true);
当我在更新面板之外使用下面的代码时,它可以正常工作:
And when I am using below code outside Update Panel it works:
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(cstype, "dateSrpt", "<script>window.open('ViewReports.aspx?filename=" + Server.UrlEncode(filename) + "', '_newtab');</script>");
推荐答案
制作一个javascript函数以使用Window.Open()打开文件,然后从服务器端调用该函数...
Make a javascript function to open a file using Window.Open(), and call the function from server side...
protected void Button1_Click(object sender, EventArgs e)
{
string js = "javascript:openFile('http://localhost:58237/WebSite14/textfile.txt')";
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop up", js, true);
}
我假设此代码位于Button_Click中,并且按钮位于Updatepanel内...
I Assumed this code is placed in a Button_Click and the button is inside Updatepanel...
<asp:UpdatePanel ID="upnl" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Open File" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
javascript函数如下:
the javascript function is as follows:
<script>
function openFile(fileName) {
window.open(fileName, '_blank');
}
</script>
如果您确实正在寻找它,它就可以正常工作.
编码愉快
it works fine if you are exactly looking for this..
Happy coding
这篇关于RegisterStartupScript不起作用,这表示更新面板无法在asp.net的新选项卡中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!