How to access .ascx control from my aspx page

1.8k Views Asked by At

I have following code in my ascx page

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs"       Inherits="WebUserControl" %>


<li id="firstry" runat="server"> first </li>

And aspx page contains :

<uc:Spinner id="Spinner" 
    runat="server" 
    MinValue="1" 
    MaxValue="10" />

This simply prints my li into my aspx page.. but I want to access the the control in ascx so that i can apply inline or a css class into that control ..Can any one guide me ?

1

There are 1 best solutions below

0
On

In the Control's code aside add:

public Control FirstTryControl
{
   get { return firsttry; }
}

Then you can access it normally from the page...

Spinner.FirstTryControl.Styles.Add(...)

That is a brute force approach, you might want to consider adding properties specific to what you need instead. In the code aside add something like:

private _spinnerClass = string.empty;
public string SpinnerClass 
{
   get { return _spinnerClass; }
   set { _spinnerClass = value; }
}

protected void Page_Render(o,e)
{
   Spinner.Attributes.Add('class', _spinnerClass);
}

Then in the page you can define these properties right from the markup:

<uc:Spinner id="Spinner" 
    runat="server" 
    MinValue="1" 
    MaxValue="10"
    SpinnerClass="green" />