Programatically Modifying a User Control's Color Attribute

Posted on 2005-05-05
Medium Priority
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
  • 2
LVL 33

Expert Comment

ID: 13939942
Ahh let me guess, you are new to ASP.net and you are coming from an ASP background?

It's good you are using usercontrols, but I wouldn't start there first.  Everything in asp.net 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

raterus earned 750 total points
ID: 13939988
by the way, Welcome to Experts-Exchange!
LVL 12

Expert Comment

ID: 13940508
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 asp.net

Author Comment

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

- DH

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

850 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