471,316 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 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 1589
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jonathan Williams | last post: by
6 posts views Thread by Earl Teigrob | last post: by
2 posts views Thread by Rubble | last post: by
7 posts views Thread by =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post: by
1 post views Thread by Alexey Smirnov | last post: by
reply views Thread by rosydwin | last post: by

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.