472,809 Members | 4,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.Controls.Add(UC1);

System.Web.UI.Control UC =
Page.LoadControl("../UserControls/ISJob/uctlJobAdmin.ascx");

plhISAdmin.Controls.Add(UC);

}

else

{

System.Web.UI.Control UC1 =
Page.LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");

plhISGeneral.Controls.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 1646
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)LoadControl("../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********@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP15.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.Controls.Add(UC1);

System.Web.UI.Control UC =
Page.LoadControl("../UserControls/ISJob/uctlJobAdmin.ascx");

plhISAdmin.Controls.Add(UC);

}

else

{

System.Web.UI.Control UC1 =
Page.LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");

plhISGeneral.Controls.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.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using AgogeNet.COM;

using AgogeNet.UserControls.ISJob;

namespace AgogeNet.ISJob

{

/// <summary>

/// Summary description for _default.

/// </summary>

public class _default : System.Web.UI.Page

{

protected System.Web.UI.HtmlControls.HtmlForm logout;

protected Microsoft.Web.UI.WebControls.TabStrip TabISJob;

protected Microsoft.Web.UI.WebControls.MultiPage mpISJob;

protected System.Web.UI.WebControls.LinkButton lbtnLogout;

protected System.Web.UI.WebControls.PlaceHolder plhISAdmin;

protected System.Web.UI.WebControls.Button Submit;

protected System.Web.UI.WebControls.PlaceHolder plhISGeneral;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//loading one of the controls dynamically

//and placing it in a placeholder

if(!Page.IsPostBack)

{

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

if (UserRole == "IS Administrator")

{

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

plhISGeneral.Controls.Add(UC1);

uctlJobAdmin UC =
(uctlJobAdmin)LoadControl("../UserControls/ISJob/uctlJobAdmin.ascx");

plhISAdmin.Controls.Add(UC);

}

else

{

System.Web.UI.Control UC1 =
Page.LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");

plhISGeneral.Controls.Add(UC1);

}

}

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

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

//

InitializeComponent();

base.OnInit(e);

}
/// <summary>

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

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

/// </summary>

private void InitializeComponent()

{

this.Submit.Click += new System.EventHandler(this.Submit_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void Submit_Click(object sender, System.EventArgs e)

{

string JobType = UC1.JobTypeValue.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****************@TK2MSFTNGP15.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)LoadControl("../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********@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP15.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.Controls.Add(UC1);

System.Web.UI.Control UC =
Page.LoadControl("../UserControls/ISJob/uctlJobAdmin.ascx");

plhISAdmin.Controls.Add(UC);

}

else

{

System.Web.UI.Control UC1 =
Page.LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");

plhISGeneral.Controls.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
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() {...
6
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...
2
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...
1
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...
1
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...
6
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...
7
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. ...
1
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...
2
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.