 |
| 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
|
|
|
|

June 24th, 2005, 09:39 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

June 24th, 2005, 11:59 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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)
|
|

June 27th, 2005, 02:50 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

June 27th, 2005, 08:53 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
do you have autopostback = true on your rdbtnlst object?
|
|

June 27th, 2005, 09:50 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes, it is set to true.
|
|

June 27th, 2005, 11:31 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
So when the page loads or on a post back, none of your dynamic controls appear, is this correct?
|
|

June 27th, 2005, 11:39 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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"
|
|

June 28th, 2005, 02:04 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey THanks a lot.
its working fine now.
|
|

June 28th, 2005, 08:55 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Glad it is working.
|
|
 |