javascript 如何在页面完全加载后显示来自代码背后的警报?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22404248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show alert from code behind after page loads completely?
提问by
I am showing alert at button click and have tab control at page . Alert appears but tab disappears at alert. I want to show alert so that tab will not disappear at alert. Is there any technique how to handle this thing ?
我在按钮单击时显示警报并在页面上有选项卡控制。警报出现但选项卡在警报时消失。我想显示警报,以便选项卡不会在警报时消失。有什么技巧可以处理这件事吗?
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
<asp:TabPanel ID="pael1" HeaderText="IP text" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="panel2" HeaderText="text" runat="server">
<ContentTemplate>
// Button Click Event
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
protected _Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "alert('Connection Successful!')", true);
}
采纳答案by AndreCruz
First of all, you should be using the following command according to the MSDN website (http://msdn.microsoft.com/en-us/library/asz8zsxy(v=vs.110).aspx):
首先,您应该根据 MSDN 网站 ( http://msdn.microsoft.com/en-us/library/asz8zsxy(v=vs.110).aspx)使用以下命令:
Note: the script should be within <script></script>
.
注意:脚本应该在<script></script>
.
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>alert('Connection Successful!');</script>", true);
The script is added to the top of your page, before the content, so the alert is displayed before your page is rendered. In order to fix this issue, use:
该脚本被添加到页面顶部的内容之前,因此在呈现页面之前显示警报。为了解决这个问题,请使用:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>window.onload = function(){alert('Connection Successful!');}</script>", true);
By doing so, you ensure that only when the page (JavaScript window
object) loads the alert is displayed.
通过这样做,您可以确保仅在页面(JavaScriptwindow
对象)加载时显示警报。
If you are using jquery, you can also use:
如果您使用的是 jquery,您还可以使用:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>$(window).load(function() {alert('Connection Successful!');});</script>", true);
回答by evannah
You can use this :
你可以使用这个:
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "alert('"+ ex.Message +"');", true);