从后台代码输出字符串到新页面或窗口

Output a string from code behind to a new page or window

本文关键字:新页面 窗口 字符串 后台 代码 输出      更新时间:2023-09-26

假设代码中有一个字符串变量,其中包含一些HTML内容,例如

Dim str1 As String = "<h3>hello</h3><p>some paragraph</p><table><tr><td>some table content</td></tr></table>"

保存的文件可以通过注册一个脚本来打开,例如

Dim script1 As String = "window.open('popupPage.html', 'myPopup')"
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someId", script1, true)

是否有办法从后面的代码在新的选项卡或窗口中输出此内容,而不将其保存到文件?

可以传递一个空字符串作为文件名

'Declare a script with your variable
Dim script1 As String = "<script>var popwin = window.open('','myPopup');popwin.document.write('" & str1 & "');</script>"
'Call your script with ScriptManager for async postback from UpdatePanel
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "poptest", script1, true)
'Call your script with ClientScript for synchronous postback outside/without UpdatePanel
ClientScript.RegisterStartupScript(Me.GetType, "popuptest", script1, True)

在服务器端代码执行后从asp.net代码调用javascript函数