Accessing properties when control loaded dynamically

Discussion in 'ASP .Net' started by Vivek Sharma, Oct 30, 2005.

  1. Vivek Sharma

    Vivek Sharma Guest

    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
     
    Vivek Sharma, Oct 30, 2005
    #1
    1. Advertising

  2. Vivek Sharma

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
     
    Karl Seguin, Oct 30, 2005
    #2
    1. Advertising

  3. Vivek Sharma

    Vivek Sharma Guest

    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:%...
    > 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" <> wrote in message
    > news:...
    >> 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
    >>
    >>

    >
    >
     
    Vivek Sharma, Oct 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smith
    Replies:
    1
    Views:
    522
    Martin Dechev
    Oct 11, 2004
  2. =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=

    Accessing Dynamically Loaded UserControls

    =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=, Feb 5, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    460
    =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=
    Feb 6, 2007
  3. Alexey Smirnov
    Replies:
    1
    Views:
    305
    Alexey Smirnov
    Mar 27, 2007
  4. Vivek Sharma

    Accessing the properties of a dynamically loaded control

    Vivek Sharma, Oct 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    108
    Phillip Williams
    Oct 31, 2005
  5. ..:: Kevin ::..

    Accessing Dynamically Loaded UserControls

    ..:: Kevin ::.., Feb 5, 2007, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    157
    ..:: Kevin ::..
    Feb 6, 2007
Loading...

Share This Page