Programatically Modifying a User Control's Color Attribute

Posted on 2005-05-05
Last Modified: 2013-11-19
Hello!  Any advice appreciated, as I am stuck.  I have a simple user control (properties.ascx) in C# builder that looks like this:

<script language="C#" runat="server">
      public string Color = "cyan";
      public string Text = "This is a user control... really!";

<font color="<%= Color %>">
<%= Text %>

I am calling the control from Default.aspx page like so:

 <%@ Page language="c#" Debug="true" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="CraftingUserControls.WebForm1" %>

<%@ Register TagPrefix="InformIT" TagName="SomeText"
      Src="properties.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<script language="C#" runat="server">
      private void Page_Load(Object sender, EventArgs e) {

            UserCtrl1.Color = "green";
            UserCtrl1.Text = "This control's properties were set programmatically!";

    <meta name="GENERATOR" content="Borland ASP.NET Designer for c# Package Library 7.1">

  <body ms_positioning="GridLayout">
  <input style="Z-INDEX: 1; LEFT: 86px; POSITION: absolute; TOP: 115px">
  <informit:header id=userControl1 runat="server">
  <informit:statusbar id=userControl2 runat="server">
  <informit:sometext id=userControl3 runat="server">
  <informit:sometext id=userControl4 runat="server" color="red">
  <informit:sometext id=userControl5 runat="server"
                     text="This is quite cool!">
  <informit:sometext id=userControl6 runat="server" color="blue"
                     text="Ain't It?">
  <informit:sometext id=UserCtrl1 runat="server">
  <form runat="server">

Here is the mystery:  every instance of the control works, except the last one, where I attempt to modify the color and text attribeaut's  (attributes)  programatically.

What am I doing wrong?

Question by:DHolland6
    LVL 33

    Expert Comment

    Ahh let me guess, you are new to and you are coming from an ASP background?

    It's good you are using usercontrols, but I wouldn't start there first.  Everything in revolves around controls, there really isn't any need to use syntax such as <% %> anymore, in fact you should avoid it at all costs.  The proper way to do this would be something like this, using a label control.

    <script language="C#" runat="server">
         public string Color
           get{return lblTest.ForeColor;}
           set{lblTest.ForeColor = value;}

         public string Text
           get{return lblTest.Text;}

    <asp:label id="lblTest" runat="server" />
    LVL 33

    Accepted Solution

    by the way, Welcome to Experts-Exchange!
    LVL 12

    Expert Comment

    The real reson is that Page_Load is not even called.

    Try change

    However, I think raterus's idea is for the long run, you should change your programming paradigm in

    Author Comment

    Thanks, Guys!  I figured it out independently, but feel a tad more welcome now.

    - DH

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now