开发者

ASP C# | MessageBox Keeps Displaying

开发者 https://www.devze.com 2023-01-30 09:59 出处:网络
I have a JavaScript MessageBox that keeps displaying when I click events in a Repeater or refresh the curre开发者_如何学运维nt page.

I have a JavaScript MessageBox that keeps displaying when I click events in a Repeater or refresh the curre开发者_如何学运维nt page.

Here is the function:

public myFunctions()
{

}
/** Display Successs/Error Message **/
public void DisplayUserMessage(string messageType, string messageString, Label ErrorMessageLabel)
{
    ErrorMessageLabel.Visible = true;
    ErrorMessageLabel.Text = "<b>" + messageType.Substring(0, 1).ToUpper() + messageType.Substring(1).ToLower() + ":</b> " + messageString;
    ErrorMessageLabel.CssClass = messageType.ToLower() + "_message";
}
public void HideUserMessage(Label ErrorMessageLabel)
{
    ErrorMessageLabel.Visible = false;
    ErrorMessageLabel.Text = "";
    ErrorMessageLabel.CssClass = "";
}

Here is the jquery to make it fade out:

$(document).ready(function () {
/** Success/Error Messages **/
$('.success_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');
$('.error_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');

});

Here it is on the MasterPage:

<!-- Message Box -->
<div id="msgBox" runat="server">
      <asp:Label ID="ErrorMessageLabel" CssClass="" runat="server" Visible="false"></asp:Label>
</div>

Here is the script in the code-behind when a success occurs:

Label ErrorMessageLabel = (Label)Master.FindControl("ErrorMessageLabel");
new myFunctions().DisplayUserMessage("success", "Administrator Updated!", ErrorMessageLabel);

Anyone know how I can stop it from continually showing up after I click another button or refresh the page?


Use a hidden input and set the value when success function is called. All the other times reset the value. In your document ready function check the value of the hidden element and then call the animate function.

<input id="txtHidSuccess" type="hidden" runat="server" />

In page load

txtHidSuccess.Value = "0";

In the success/error function

txtHidSuccess.Value = "1";

jQuery

$(function(){
    if ($("#txtHidSuccess").val() === "1") {
        /** Success/Error Messages **/
       $('.success_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');
       $('.error_message').animate({ opacity: 1.0 }, 2000).fadeOut('slow');    
    }
});


I see where you're calling DisplayUserMessage(), but not HideUserMessage(). My guess is that when you set the ErrorMessageLabel properties, they're stored in the View State persisted across postbacks.

Rather than use an ASP.NET Label control, you could try just wrapping some text with a regular div and use CSS to hide it. When the success event occurs in your app, you can use ClientScriptManager.RegisterStartupScript() to write a script to the client that shows the div. Then, your animate functions can fade out the div if it's visible.


if(IsPostBack)
         new myFunctions().HideUserMessage(ErrorMessageLabel);

This worked. Thanks @Harie

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号