You are on page 1of 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class dynamic_control : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

int count = Convert.ToInt32(Txtcontrol.Text);


foreach (ListItem lst in CheckBoxList1.Items)
{
if (lst.Selected)
{
if (lst.Value =="TextBox")
{
for (int i = 1; i <= count; i++)
{
TextBox txt = new TextBox();
txt.Text = "TextBox" + i.ToString();

Panel1.Controls.Add(txt);
Setprompt(txt);

}
}
if (lst.Value == "RadioButton")
{
for (int i = 1; i <= count; i++)
{
RadioButton rdb = new RadioButton();
rdb.Text = "RadioButton" + i.ToString();
Panel2.Controls.Add(rdb);
}
}
if (lst.Value == "DropDown")
{
for (int i = 1; i <= count; i++)
{
DropDownList dbr = new DropDownList();
dbr.Text = "DropDown" + i.ToString();
Panel3.Controls.Add(dbr);
dbr.Items.Add("--Select--");

}
}

if (lst.Value == "Button")
{
for (int i = 1; i <= count; i++)
{
Button btn = new Button();
btn.Text = "Button" + i.ToString();
Panel4.Controls.Add(btn);
}
}

if (lst.Value == "Label")
{
for (int i = 1; i <= count; i++)
{
Label lbl = new Label();
lbl.Text = "Label" + i.ToString();
Panel5.Controls.Add(lbl);
}
}

if (lst.Value == "CheckBox")
{
for (int i = 1; i <= count; i++)
{
CheckBox chk = new CheckBox();
chk.Text = "CheckBox" + i.ToString();
Panel6.Controls.Add(chk);
}
}
}
}
}
public string ControlRenderer(Control control)
{
StringWriter writer = new StringWriter();
control.RenderControl(new HtmlTextWriter(writer));
return writer.ToString();
}
private void Setprompt(TextBox txt)
{
string onFocusAction = "if (this.value == \"{0}\") {{ this.value = \"\";
this.style.color = \"black\"; }} ";
string onBlurAction = " if (this.value == \"\") {{ this.value = \"{0}\";
this.style.color = \"gray\"; }} else this.style.color = \"black\";";
onBlurAction = string.Format(onBlurAction, txt.Text);
onFocusAction = string.Format(onFocusAction, txt.Text);
txt.Attributes["onblur"] = onBlurAction;
txt.Attributes["onfocus"] = onFocusAction;

You might also like