Loading/Updating User Controls... how?

T

Tarun Mistry

Hi there, using the following code i see it is possible to load a user
control dynamically and set its properties.

Control c1 = LoadControl("MyUserControl.ascx");
((MyUserControl)c1).BackColor = "beige";
Page.Controls.Add(c1);

However, If i already have a user control on my page, as defined in the
..aspx file, it is possible to change it attributes/properties?

I have a usercontrol called "menu", one of its properties allows you to set
the title of the page. However, id like to alter this title depending on
what information has been submitted via a form, i cant figure out how to
alter its properties before it is rendered.

Please help!
Thanks everyone
Taz
 
S

sam

In Page Load or whatever:
this.menu = (Menu) LoadControl("Menu.ascx")

In OnPreRender()
this.menu.Title = "Title"
 
T

Tarun Mistry

In Page Load or whatever:
this.menu = (Menu) LoadControl("Menu.ascx")

In OnPreRender()
this.menu.Title = "Title"

Hi, thanks for the reply.

Ok this code compiles, but doesnt actually do anything. Any ideas?

Kind regards,
Tarun
 
G

Guest

Hi,
Just include the control as a field in your page class, the page will load
the control and assign a refference to that field ...

public class MyPage : System.Web.UI.Page
{
protected MyUserControl m_control; //Must be the same as the id of your
//usercontrol.

private void Page_Load(object sender, System.EventArgs e)
{
m_control.BackColor = "beige"; //Will set the control's background color
}

}

Your control's Page_Load event handler will fire after your hosting Page
one, so you can initialize your control in your hosting page Page_Load event
handler.

Hope this helps.
 
S

sam

Oh, Im sorry.

add:
this.Controls.Add(this.menu) right after the LoadControl() call.

And you should declare:
private Menu menu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top