[英]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.