简体   繁体   English

页面完全加载后如何显示来自代码背后的警报?

[英]How to show alert from code behind after page loads completely?

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);
        }

您可以使用:

ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "alert('"+ ex.Message +"');", true);

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. 这样,可以确保仅在页面(JavaScript window对象)加载时才显示警报。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM