Link to home
Start Free TrialLog in
Avatar of DHolland6
DHolland6

asked on

Programatically Modifying a User Control's Color Attribute

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!";
</script>

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


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!";
      }
</script>

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

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

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?

TIA,
Dave
Avatar of raterus
raterus
Flag of United States of America image

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;}
       set{lblTest.Text=value;}
     }
</script>

<p>
<asp:label id="lblTest" runat="server" />
</p>
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of laotzi2000
laotzi2000

The real reson is that Page_Load is not even called.

Try change
AutoEventWireup="false"
to
AutoEventWireup="true"

However, I think raterus's idea is for the long run, you should change your programming paradigm in asp.net
Avatar of DHolland6

ASKER

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

- DH