Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 June 24th, 2005, 09:39 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Dynamic Controls to Web Form

Dear All,
i want to add controls dynamically to my web form with the following code:

string sCtrl;
ArrayList alNewList=new ArrayList();
protected System.Web.UI.WebControls.RadioButtonList rdbtnlst;
protected System.Web.UI.WebControls.Label lblexec;
protected System.Web.UI.WebControls.PlaceHolder plholder;
ArrayList ctrlList=new ArrayList();



private void Page_Load(object sender, System.EventArgs e)
{
    if(! IsPostBack)
    {
        ctrlList.Add("Drop Down List");
        ctrlList.Add("Check Box List");
        ctrlList.Add("Radio Button List");
        ctrlList.Add("List Box");

        rdbtnlst.DataSource = ctrlList;
        rdbtnlst.DataBind();
        rdbtnlst.SelectedIndex =0;
    }
            alNewList.Add("Blue");
            alNewList.Add("Green");
            alNewList.Add("Red");
            alNewList.Add("Orange");
            alNewList.Add("Pink");
}


private void btnGetSelection_Click(object sender, System.EventArgs e)
{
    sCtrl = rdbtnlst.SelectedItem.Text;
    if (sCtrl !="")
    {
        switch(sCtrl)
        {
        case "DropDownList":
        {
        DropDownList objDDL=new DropDownList();
        objDDL.DataSource=alNewList;
        objDDL.DataBind();
        plholder.Controls.Add(objDDL);
        objDDL.SelectedIndex =1;
        break;
        }

        case "RadioButtonList":
        {
        RadioButtonList objRdbtn=new RadioButtonList();
        objRdbtn.DataSource =alNewList;
        objRdbtn.DataBind();
        plholder.Controls.Add(objRdbtn);
        objRdbtn.SelectedIndex =0;
        break;
        }

        case "CheckBoxList":
        {
        CheckBoxList objChkLst=new CheckBoxList();
        objChkLst.DataSource=alNewList;
        objChkLst.DataBind();
        plholder.Controls.Add(objChkLst);
        objChkLst.SelectedIndex =2;
        break;
        }

        case "ListBox":
        {
        ListBox objLstBox=new ListBox();
        objLstBox.DataSource=alNewList;
        objLstBox.DataBind();
        plholder.Controls.Add(objLstBox);
            objLstBox.SelectedIndex=3;
        break;
        }

        }//switch
    }//if
}

  private void btnClearSelection_Click(object sender, System.EventArgs e)
{
        sCtrl ="";
}


But am not able to display the controls.
Can anyone pls help me out.

Thanks & Regards,
Muskaan.

 
Old June 24th, 2005, 11:59 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You are not adding the controls to your form anywhere in your code.

this example shows how to add a textbox to a form:

Dim tb as new TextBox
tb.text = "This is my TextBox"
Me.FindControl("Form1").Controls.Add(tb)



 
Old June 27th, 2005, 02:50 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks but am not getting what exactly you are trying to say and what controls you are saying to add.
i already have a Radiobuttonlist, 2 buttons and a placeholder in the design view of the form as follows :

protected System.Web.UI.WebControls.RadioButtonList rdbtnlst;
protected System.Web.UI.WebControls.Label lblexec;
protected System.Web.UI.WebControls.PlaceHolder plholder;

string sCtrl;
ArrayList alNewList=new ArrayList();
ArrayList ctrlList=new ArrayList();


Cud you pls elaborate.

Regards,
Muskaan.


 
Old June 27th, 2005, 08:53 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

do you have autopostback = true on your rdbtnlst object?

 
Old June 27th, 2005, 09:50 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, it is set to true.

 
Old June 27th, 2005, 11:31 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

So when the page loads or on a post back, none of your dynamic controls appear, is this correct?

 
Old June 27th, 2005, 11:39 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I found your problem:

you define your array like this:
       ctrlList.Add("Drop Down List");
        ctrlList.Add("Check Box List");
        ctrlList.Add("Radio Button List");
        ctrlList.Add("List Box");

and bind it to your radiobutton list

Then, in your case statement you are checking like this:
case "DropDownList":

 case "RadioButtonList":
.. etc

Your case will never evaluate to a true statement.

you must use
case "drop down list"
case "check box list"

etc...

In other words .. "CheckBoxList" will never equal "Check Box List"

 
Old June 28th, 2005, 02:04 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey THanks a lot.
its working fine now.

 
Old June 28th, 2005, 08:55 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Glad it is working.






Similar Threads
Thread Thread Starter Forum Replies Last Post
adding controls on vba form from an addin narendrapawar Pro VB 6 1 April 25th, 2010 04:59 PM
Adding controls to a form Scootterp Access VBA 2 August 4th, 2009 02:32 PM
Adding controls to a form based on data received jontheniceguy Visual Basic 2005 Basics 0 April 7th, 2006 10:07 AM
Adding text/controls from one form to another Paula222 Access VBA 1 February 24th, 2006 10:10 AM
Adding Form controls in Datagrid (How to) chiefouko VS.NET 2002/2003 5 July 13th, 2004 12:24 AM





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