简体   繁体   English

如何防止每次单击按钮时重新加载javascript?

[英]How do I prevent a javascript from reloading every time a button is click?

I have this script which shows a message to the user when the page is loaded in ASP.NET, my problem is that every time an user clicks any button which causes I believe a PostBack the same message gets reloaded. 我有此脚本,当在ASP.NET中加载页面时,该脚本向用户显示一条消息,我的问题是,每当用户单击导致我相信PostBack的任何按钮时,都会重新加载相同的消息。 I only want to show this on First Page load. 我只想在首页加载中显示此内容。 Any help will be appreciate. 任何帮助将不胜感激。

$('.block .message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
$('.block .message .close').hover(
    function () { $(this).addClass('hover'); },
    function () { $(this).removeClass('hover'); }
);

$('.block .message .close').click(function () {
    $(this).parent().fadeOut('slow', function () { $(this).remove(); });
});

Here is my HTML code, 这是我的HTML代码,

<div class="message info">The first input field would allow you to provide the From Date, PO, Shipment or Single Date.</div>

Add runat=server and an id: 添加runat = server和一个id:

<div class="message info" runat="server" id="divmessage" >

Add code in your Page_Load to make it visible only if !IsPostback 在您的Page_Load中添加代码以使其仅在!IsPostback时可见

protected void Page_Load(object sender, EventArgs e)
{
   divmessage.visible = !IsPostBack;
}

You could add this to your page: 您可以将其添加到页面中:

<script>
    function isPostBack () {
      return <%= Page.IsPostBack %>;
    }
</script>

And now: 现在:

if ( !isPostBack() ) {
    //show message
}

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

相关问题 每次单击按钮时,如何防止我的程序覆盖 localStorage? - How do I prevent my program from overwriting localStorage every time a button is clicked? 如何从 JavaScript 中单击此按钮? - How do I click this button from JavaScript? 没有按钮时,如何防止获取请求重新加载 Javascript 中的页面? - How to prevent a fetch request from reloading the page in Javascript when there is not a button? 如何防止重新加载撤消.innerHTML操作? - How do I prevent reloading from undoing my .innerHTML action? 如何防止browser_action每次重新加载其弹出窗口? - How to prevent browser_action from reloading its popup window every time? Web 开发:如何在浏览器中“存储”页面并防止它们每次在浏览器之间导航时重新加载 - Web development: How to 'store' pages in the browsers and prevent them from reloading every time when navigating between them 每次单击按钮时如何更改价格值? - How do I chage price value every time I click button? 如何防止骆驼中的javascript重新加载? - How can I prevent reloading of javascript in camel? 如何通过单击按钮触发JavaScript - How do I trigger javascript from a button click 我如何才能防止onclick按钮多次单击,直到通过javascript if(confirm)条件进行确认 - How can i prevent onclick button from multiple click until it's confirmation from javascript if(confirm ) condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM