假设代码后面有一个字符串变量,其中包含一些 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)
检查这个Call javascript function from asp.net code behind after server side code executes
关于javascript - 将代码隐藏中的字符串输出到新页面或窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33191303/