i've just started doing asp.net i am good in vb.net but i've encountered a challenge.First of all isnt it supposed to be private sub not protected.As you can see i want to output a message on the label which is green but i am getting an error saying green is not declared. can someone help

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Response.Write("Hey, my page is loaded ...")
        'Label1.Text = "Welcome to my First ASP.NET Page. Current Server time is " & DateTime.Now.ToString()
        Label1.ForeColor = Color.FromName("GREEN")
        Label1.Text = "Successfully processed."

    End Sub

End Class

Dani AI

Generated

Short, practical clarification and fixes based on the replies from and .

Using Protected vs Private

  • Both Private and Protected will work for a page event handler. Templates and the designer-generated partial class usually declare controls as Protected WithEvents, so Protected Sub Page_Load(...) Handles Me.Load is the common pattern and fine to keep.

Why you saw "GREEN is not declared"

  • That message usually means the compiler saw an identifier, not a string literal. Verify you used quotes when you meant a string (e.g., "Green"). If you are using the Color members and the compiler cannot find Color, make sure System.Drawing is available in scope or referenced.

Concrete, safe fixes

  • Add the namespace import at the top of the code file:
Imports System.Drawing
  • Make sure the ASPX label is a server control (so the code-behind can see it):
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  • Color.FromName will return Color.Empty for unknown names; named colors are case-insensitive. See the class reference: System.Drawing.Color and Color.FromName. Also check the control property doc: .

Alternate approach (recommended for styling)

  • Use CSS instead of setting color server-side: give the label a CssClass and define the color in a stylesheet — easier to maintain and avoids server-side type issues.

Quick troubleshooting checklist

  • Confirm runat="server" on the label.
  • Rebuild so the designer file is regenerated (or use "Convert to Web Application" if designer declarations are missing).
  • Ensure no local Color identifier shadows System.Drawing.Color.
  • Confirm System.Drawing reference exists (it normally does for WebForms).

Recommended Answers

All 2 Replies

It should be
Label1.Forecolor = Color.Green;
remove the from name , just use the enum value

and also import the System.Drawing class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.