Accessing properties when control loaded dynamically

V

Vivek Sharma

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
 
K

Karl Seguin

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
 
V

Vivek Sharma

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.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top