473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Loading/Updating User Controls... how?

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
Feb 6 '06 #1
4 935
sam
In Page Load or whatever:
this.menu = (Menu) LoadControl("Menu.ascx")

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

Feb 6 '06 #2
> 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
Feb 6 '06 #3
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.

--
_________________________
Kostas Pantos [MCP]
http://kostas.pantos.name
"Tarun Mistry" wrote:
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

Feb 6 '06 #4
sam
Oh, Im sorry.

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

And you should declare:
private Menu menu

Feb 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Richardson | last post by:
I've been bothered for some time about my DataGrid not populating my rows very quickly. I have about 10K rows loading into the grid. I create a datatable dt with 2 columns, an ID and a display. ...
7
by: Tim | last post by:
I am trying to load both server and user controls into placeholder controls on a aspx template page at runtime. These values would be strings that are returned from a database query. I know I can...
0
by: Phl | last post by:
Hi, I am trying to create an webform which loads usercontrols dyanamically. I know exactly what to load for some of these controls but for some, I dont want to load it until the user has press a...
1
by: sekisho | last post by:
I'm dynamically adding a column of labels and a column of text boxes to a panel on a webform, based on data returned from an SQL query, which the user builds by selecting options from a few...
0
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables...
2
by: Dave A | last post by:
I just don't get this... If I need to dynamically load controls into a web page I simply need to go PlaceHolder1.Controls.Add(new Button()); or similar. However when I need to dynamically...
1
by: Bob Rock | last post by:
Hello, I'm new to ASP.NET and I've been looking into the topic of dynamically loading (typically accomplished with a LoadControl followed by a MyControl.Controls.Add()) both user controls and...
0
by: Chris | last post by:
Hello, I have a problem with re-loading datasets. As a simple example, if I have an SQL table of addresses comprising active and inactive addresss, I wish to load either sub-set by clicking on...
2
by: adiel_g | last post by:
I added a user control to a webform in Asp.net 2.0. I am also adding several other user controls to this webform. Now I am trying to find a way to stop the user controls from loading up when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.