Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 28th, 2006, 04:34 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rpeters83
Default Need urgent help loading user controls at runtime

I have a series of custom controls such as a DateBox, States/Country dropdown, etc in order to save me time for creating forms as well as allow me to load these controls into a form-builder i'm creating.

i have a class, FormField.cs that, depending on the element's type (input field, textbox, dropdown, DateBox, StateList, etc) dynamically create and return that control to be loaded into a placeholder.

I am able to set properties of the native controls with no issue. my issue is trying to set the properties on my custom user controls at runtime. currently, i use the LoadControl command and set it as a Control object (where I set its ID, thats all). I would like ot be able to set its object properties and this is where i'm having trouble.

I tried to cast it as cntStateList (NOT shown below), but the error I keep getting is that it cannot find the class.

Please help! Thanks!

Examples:

cntStateList.cs is the codebehind for cntStateList.ascx

cntStateList.cs
Code:
// C# Document

using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

public class cntStateList : System.Web.UI.UserControl
{
    protected DropDownList drpStates;

    protected void page_Load (Object s, EventArgs e)
    {
        DataSet dsStates = new DataSet();
        dsStates.ReadXml(MapPath("../../XML/states.xml"));
        drpStates.DataSource = dsStates;
        drpStates.DataValueField = "abbrev";
        drpStates.DataTextField = "name";
        drpStates.DataBind();
    }

    public String Text
    {
        get { return drpStates.Text; }
    }

    public String SelectedValue
    {
        get { return drpStates.SelectedValue; }
    }
}
Below is the FormField.cs class

FormField.cs
Code:
// inside a switch statement to get the field type...

case 5:
                RadioButtonList rblRadioList = new RadioButtonList();
                rblRadioList.ID = _strFieldLabel;
                rblRadioList.DataSource = _cn.ExecQuery("select * from tblFieldValues where field_id = " + _iFieldId);
                rblRadioList.DataValueField = "value";
                rblRadioList.DataTextField = "value";
                rblRadioList.DataBind();
                return rblRadioList;
                break;

            case 6:
                FileUpload fuFileUpload = new FileUpload();
                fuFileUpload.ID= _strFieldLabel;
                return fuFileUpload;
                break;

                        // PROBLEM BEGINS HERE...
            case 7:
                Control cntTmpDateBox = (Control)Page.LoadControl("/cms/controls/cntDateBox/cntDateBox.ascx");
                cntTmpDateBox.ID= _strFieldLabel;
                return cntDateBox;
                break;

            case 8:
                Control cntStateList = (Control)Page.LoadControl("/cms/controls/cntStateList/cntStateList.ascx");
                cntStateList.ID= _strFieldLabel;
                return cntStateList;
                break;
                  // .....
 
Old August 31st, 2006, 11:33 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rpeters83
Default

Just an update:

I am currently using Session variables to set the properties of my custom controls, but would still like to be able to access the properties directly.

I would like to be able to do this...

Code:
Control myDateBox = Page.LoadControl("DateBoxControl.ascx");
((DateBoxControl)myDateBox).CurrDate = DateTime.Now;
But when I do, it says it cant find the class. If i put it into a namespace, it says it cant find the namespace. Any ideas? Anyone? Thanks!

 
Old August 31st, 2006, 11:43 AM
Authorized User
 
Join Date: Apr 2005
Location: Spanish Fork, UT, USA.
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DudeBori82 Send a message via MSN to DudeBori82
Default

Maybe try casting it as the control of your custom control type:
cntStateList cntTmpDateBox = (cntStateList)Page.LoadControl("/cms/controls/cntDateBox/cntDateBox.ascx");
cntTmpDateBox.ID= _strFieldLabel;
cntTmpDateBox.CurrDate = DateTime.Now;


Actions are a direct result of your desires. Desire for good things.
 
Old August 31st, 2006, 12:24 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rpeters83
Default

DudeBori,

I tried that. It keeps saying that the type or namespace cannot be found. Everything else is in a namespace I defined.

I don't know what else to do.

 
Old August 31st, 2006, 12:28 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rpeters83
Default

I should also mention that while I am able to cast my user controls appropriately and manipulate their properties inside of an ASPX page directy, I want to be able to programmically load this control AND set its properties at runtime (inside of another class file). I can currently load it, but cant set its properties inside this class.

 
Old August 31st, 2006, 12:59 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rpeters83
Default

Nevermind!

I found out I had to use an abstract base class and set it up in my App_Code folder. thanks anyway folks.

 
Old September 3rd, 2006, 06:42 AM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you should add a reference on your aspx file like:

<%@ Reference VirtualPath="../UserControlFile.ascx"%>

and thats it.
 
Old September 3rd, 2006, 10:17 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rpeters83
Default

True, but that was if I was calling the class constructor from an ASPX file,which, in my case, I wasn't. I needed it to be an actual class that I could initilize inside another class. An abstract base class in App_Code did the trick!

Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
Loading a WinForm using at Runtime ashu_from_india General .NET 0 October 3rd, 2006 04:04 AM
Load User Controls at Runtime Duncan VB.NET 3 November 4th, 2005 07:05 PM
Loading and Destroying web user controls into a pa see07 ASP.NET 1.x and 2.0 Application Design 5 February 4th, 2005 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.