最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to show alert from code behind after page loads completely? - Stack Overflow

programmeradmin4浏览0评论

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

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);
        }
Share Improve this question edited Mar 14, 2014 at 12:21 Barney 16.5k5 gold badges65 silver badges80 bronze badges asked Mar 14, 2014 at 11:52 user2178738user2178738 12
  • 1 Which alert? JavaScript? You marked your question as C# and ASP.Net. It would also be useful to have some code to understand exactly what you are trying to do. – AndreCruz Commented Mar 14, 2014 at 11:56
  • can you not use JavaScript to do the alert before the post back then the page and everything it contains will remain. – JamesMarc Commented Mar 14, 2014 at 12:00
  • @AndreCruz Yes JavaScript alert. – user2178738 Commented Mar 14, 2014 at 12:05
  • @user3374409 In my scenario I want to show alert at ButtonClick Event – user2178738 Commented Mar 14, 2014 at 12:06
  • @AndreCruz I have posted code too. kindly review. – user2178738 Commented Mar 14, 2014 at 12:09
 |  Show 7 more ments

2 Answers 2

Reset to default 1

First of all, you should be using the following mand according to the MSDN website (http://msdn.microsoft./en-us/library/asz8zsxy(v=vs.110).aspx):

Note: the script should be within <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.

If you are using jquery, you can also use:

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>$(window).load(function() {alert('Connection Successful!');});</script>", true);

You can use this :

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

评论列表(0)

  1. 暂无评论