473,662 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing properties when control loaded dynamically

Hi There,

I have a situation where I wish to load the controls dynamically on the
basis of user role. Hence, I am using this code.
if (UserRole == "IS Administrator")

{

Control UC1 = LoadControl("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

System.Web.UI.C ontrol UC =
Page.LoadContro l("../UserControls/ISJob/uctlJobAdmin.as cx");

plhISAdmin.Cont rols.Add(UC);

}

else

{

System.Web.UI.C ontrol UC1 =
Page.LoadContro l("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

}

My problem is on the click of the button (which is not included within the
control) I am unable to access the properties of the user controls. How can
I access the properties?

Please help.

Viverk
Nov 19 '05 #1
2 1704
You need to cast the controls to their appropriate type, else they are just
Controls and only expose Control properties and methods.
if ur uctlJobGeneral. ascs looks like:

public class JobGeneral : UserControl
{
}

you could do

JobGeneral UC1 =
(JobGeneral)Loa dControl("../UserControls/ISJob/uctlJobGeneral. ascx");
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Vivek Sharma" <vi********@hot mail.com> wrote in message
news:uH******** *****@TK2MSFTNG P15.phx.gbl...
Hi There,

I have a situation where I wish to load the controls dynamically on the
basis of user role. Hence, I am using this code.
if (UserRole == "IS Administrator")

{

Control UC1 = LoadControl("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

System.Web.UI.C ontrol UC =
Page.LoadContro l("../UserControls/ISJob/uctlJobAdmin.as cx");

plhISAdmin.Cont rols.Add(UC);

}

else

{

System.Web.UI.C ontrol UC1 =
Page.LoadContro l("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

}

My problem is on the click of the button (which is not included within the
control) I am unable to access the properties of the user controls. How
can I access the properties?

Please help.

Viverk

Nov 19 '05 #2
Hi There,

I tried what you suggested. This is my complete code on the page
using System;

using System.Collecti ons;

using System.Componen tModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.Sess ionState;

using System.Web.UI;

using System.Web.UI.W ebControls;

using System.Web.UI.H tmlControls;

using AgogeNet.COM;

using AgogeNet.UserCo ntrols.ISJob;

namespace AgogeNet.ISJob

{

/// <summary>

/// Summary description for _default.

/// </summary>

public class _default : System.Web.UI.P age

{

protected System.Web.UI.H tmlControls.Htm lForm logout;

protected Microsoft.Web.U I.WebControls.T abStrip TabISJob;

protected Microsoft.Web.U I.WebControls.M ultiPage mpISJob;

protected System.Web.UI.W ebControls.Link Button lbtnLogout;

protected System.Web.UI.W ebControls.Plac eHolder plhISAdmin;

protected System.Web.UI.W ebControls.Butt on Submit;

protected System.Web.UI.W ebControls.Plac eHolder plhISGeneral;

private void Page_Load(objec t sender, System.EventArg s e)

{

// Put user code to initialize the page here

//loading one of the controls dynamically

//and placing it in a placeholder

if(!Page.IsPost Back)

{

string UserRole = Session["UserRole"].ToString();

if (UserRole == "IS Administrator")

{

uctlJobGeneral UC1 =
(uctlJobGeneral )LoadControl(". ./UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

uctlJobAdmin UC =
(uctlJobAdmin)L oadControl("../UserControls/ISJob/uctlJobAdmin.as cx");

plhISAdmin.Cont rols.Add(UC);

}

else

{

System.Web.UI.C ontrol UC1 =
Page.LoadContro l("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

}

}

}

#region Web Form Designer generated code

override protected void OnInit(EventArg s e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeCompo nent();

base.OnInit(e);

}
/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeCompo nent()

{

this.Submit.Cli ck += new System.EventHan dler(this.Submi t_Click);

this.Load += new System.EventHan dler(this.Page_ Load);

}

#endregion

private void Submit_Click(ob ject sender, System.EventArg s e)

{

string JobType = UC1.JobTypeValu e.ToString();

}

}

}

When I try to run the website it gives me an error saying that UC1 is not
defined.

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
You need to cast the controls to their appropriate type, else they are
just Controls and only expose Control properties and methods.
if ur uctlJobGeneral. ascs looks like:

public class JobGeneral : UserControl
{
}

you could do

JobGeneral UC1 =
(JobGeneral)Loa dControl("../UserControls/ISJob/uctlJobGeneral. ascx");
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Vivek Sharma" <vi********@hot mail.com> wrote in message
news:uH******** *****@TK2MSFTNG P15.phx.gbl...
Hi There,

I have a situation where I wish to load the controls dynamically on the
basis of user role. Hence, I am using this code.
if (UserRole == "IS Administrator")

{

Control UC1 =
LoadControl("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

System.Web.UI.C ontrol UC =
Page.LoadContro l("../UserControls/ISJob/uctlJobAdmin.as cx");

plhISAdmin.Cont rols.Add(UC);

}

else

{

System.Web.UI.C ontrol UC1 =
Page.LoadContro l("../UserControls/ISJob/uctlJobGeneral. ascx");

plhISGeneral.Co ntrols.Add(UC1) ;

}

My problem is on the click of the button (which is not included within
the control) I am unable to access the properties of the user controls.
How can I access the properties?

Please help.

Viverk


Nov 19 '05 #3

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

Similar topics

5
1707
by: Jonathan Williams | last post by:
Hi, I have an object which inherits from WebControl (CUSTOM : WebControl) In this object I have code in which I add child contols: protected override void CreateChildControls() { //this.Controls.Clear(); CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil(); DataTable objDT = objCustomFieldUtil.GetDataTable("custom"); for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
6
2566
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my user control folder so that they can be selected and loaded at run time also. These new controls will need to have access to many of the classes with there properties and methods within the complied code. Is this possible? This seems to be the...
2
1339
by: Rubble | last post by:
Hello, Ive searched all over the net trying to find an answer to this...so anybody with some expertise in this area would be greatly appreciated. Background: I have a webform that loads a datagrid. This datagrid has a button in a column that loads another datagrid(user control) when clicked. This new grid also has a column with a button that loads another user control. So, there could be 3 user controls on this form if all the
1
2414
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named EditStuff.ascx which reads from an xml file and loads controls to its self based on string value in xml nodes collection of the xml. There are several controls that can be loaded and for each one there exists a public method called 'IntiControl'...
1
1478
by: Brian | last post by:
Thanks for your time. I've created a web user control that has some properties available. I'm able to add the control dynamically (at run time) with no problem(.controls.add(). Is it possible to set property values of a dynamically added web user control at run time? Can you provide a link or code snippet? I can do it for a typical web control (textbox, label, etc), but I can't figure out how to do it for a web user control(ascx...
6
11068
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
7
2129
by: =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post by:
I have a problem with accessing controls that I have loaded dynamically and added to a web page. The scenario: I have a webpage that displays multiple instances of a user control on the page. The number of controls that are displayed can be adjusted by the user so the controls are added to the page dynamically using the following code in the Page_Init event handler: For i as Integer = 1 to NumberOfControls
1
1471
by: Alexey Smirnov | last post by:
I have a web form, which load a web control by using the LoadControl method void Page_Init(...) { Control ctl = LoadControl(Request.QueryString+".ascx"); // url = default.aspx?module=control1 Panel1.Controls.Add(ctl); }
2
3106
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the dynamically loaded assembly) so far so good (per my code below)... but here is where I'm getting hung up: 3. Call methods of that type (see comments in my code) If the types in the dynamically loaded assembly were in the same namespace
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.