1

I am using Bootstrap alert and i'm trying to change the alert class from a static method in the code behind but i'm experiencing an error when trying to do so, which is:

An object reference is required for a non static field.

Im pretty new to this so any help would be much appreciated

aspx.cs:

public static void alert()
{
     wallboardAlert.Visible = alertVisable;
     wallboardAlert.Attributes["class"] = alertClassType;
}

.aspx

<div class="" id="wallboardAlert" runat="server">
    <h1 id="wallboardAlertTitle" runat="server"><strong></strong></h1>
    <h4 id="wallboardAlertBody" runat="server"></h4>
</div>

1 Answers1

0

You could define a static variable to hold the current instance of the form:

private static MyFormClassName currentForm = null; // Use the real class name

protected override void OnInit(EventArgs e)
{
    currentForm = this;
    base.OnInit(e);
}

The static function could then use that variable to access the form and its controls:

public static void alert()
{
     if (currentForm != null)
     {
         currentForm.wallboardAlert.Visible = currentForm.alertVisable;
         currentForm.wallboardAlert.Attributes["class"] = currentForm.alertClassType;
     }
}

Please note that this will work only if an instance of the form exists when the static function is called.

ConnorsFan
  • 70,558
  • 13
  • 122
  • 146