Hi friends,In this Article i would like to Explain how to create dynamic Registration form using Aspnet
First,Open the MIcrosoft visual studio 2008
Next,Create one Asp.Net Web application
Next,Open the Design page of the Default.aspx
Next,Drag and Drop one Drop down list and add two items in this drop down list.
that is,click on the properties of the Drop Down List and select items and add NewRegistration item in that
Next,come to the code page that is Default.aspx.cs and copy the following code
First,Open the MIcrosoft visual studio 2008
Next,Create one Asp.Net Web application
Next,Open the Design page of the Default.aspx
Next,Drag and Drop one Drop down list and add two items in this drop down list.
that is,click on the properties of the Drop Down List and select items and add NewRegistration item in that
Next,come to the code page that is Default.aspx.cs and copy the following code
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.SqlClient;
- public partial class Dynamic_controls : System.Web.UI.Page
- {
- SqlConnection con;
- SqlCommand com;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (IsPostBack)
- {
- Createnewaccount();
- }
- }
- private void Createnewaccount()
- {
- CreateDynamicTextBoxes();
- CreateDynamicTextBoxes1();
- CreateDynamicCheckBoxes();
- CreateDynamicCheckBoxes1();
- CreateDynamicRadioButtons1();
- BindDropDownLists();
- }
- protected void CreateDynamicTextBoxes()
- {
- int c = 1;
- TextBox[] textBoxArr = new TextBox[c];//array of textboxes
- for (int i = 0; i < c; i++)
- {
- textBoxArr[i] = new TextBox();
- textBoxArr[i].ID = "txtBox" + i.ToString();
- PH_Name.Controls.Add(textBoxArr[i]);
- RequiredFieldValidator reqfldVal = new RequiredFieldValidator();
- reqfldVal.ID = "RequiredValidator" + i;
- reqfldVal.ControlToValidate = "txtBox" + i;
- reqfldVal.ErrorMessage = "Not Empty";
- reqfldVal.SetFocusOnError = true;
- PH_Name.Controls.Add(reqfldVal);
- }
- }
- protected void CreateDynamicTextBoxes1()
- {
- int c = 1;
- TextBox[] textBoxArr1 = new TextBox[c];//array of textboxes
- for (int i = 0; i < c; i++)
- {
- textBoxArr1[i] = new TextBox();
- textBoxArr1[i].ID = "txtBox1" + i.ToString();
- PH_pwd.Controls.Add(textBoxArr1[i]);
- RequiredFieldValidator reqfldVal1 = new RequiredFieldValidator();
- reqfldVal1.ID = "RequiredValidator1" + i;
- reqfldVal1.ControlToValidate = "txtBox1" + i;
- reqfldVal1.ErrorMessage = "Not Empty";
- reqfldVal1.SetFocusOnError = true;
- PH_pwd.Controls.Add(reqfldVal1);
- }
- }
- private void BindDropDownLists()
- {
- SqlDataSource sqlDS = new SqlDataSource();
- sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString();
- sqlDS.SelectCommand = "Select countryid, country_name from countrynames";
- PH_country.Controls.Add(sqlDS);
- DropDownList ddl = new DropDownList();
- ddl.ID = "ddlrank";
- ddl.DataSource = sqlDS;
- ddl.DataTextField = "country_name";
- ddl.DataValueField = "countryid";
- PH_country.Controls.Add(ddl);
- foreach (Control ctl in PH_country.Controls)
- {
- if (ctl is DropDownList)
- {
- (ctl as DropDownList).DataBind();
- }
- }
- }
- protected void CreateDynamicCheckBoxes()
- {
- CheckBox chk;
- chk = new CheckBox();
- chk.ID = "chkhobbies";
- chk.Text = "savings";
- chk.AutoPostBack = false;
- PH_trans.Controls.Add(chk);
- }
- protected void CreateDynamicCheckBoxes1()
- {
- int c = 1;
- CheckBox[] chk = new CheckBox[c];
- for (int i = 0; i < c; i++)
- {
- chk[i] = new CheckBox();
- chk[i].ID = "chkhobbies1" + i.ToString();
- chk[i].Text = "Current";
- chk[i].AutoPostBack = false;
- PH_trans.Controls.Add(chk[i]);
- }
- }
- protected void CreateDynamicRadioButtons()
- {
- RadioButton male = new RadioButton();
- RadioButton female = new RadioButton();
- male = new RadioButton();
- female = new RadioButton();
- male.Text = "Male";
- female.Text = "Female";
- male.Checked = false;
- female.Checked = false;
- male.GroupName = "gender";
- female.GroupName = "gender";
- male.ID = "malegender" ;
- female.ID = "gender";
- male.AutoPostBack = false;
- female.AutoPostBack = false;
- PH_gender.Controls.Add(male);
- PH_gender.Controls.Add(female);
- }
- protected void CreateDynamicRadioButtons1()
- {
- RadioButtonList radio = new RadioButtonList();
- radio.ID = "rblRow";
- radio.Items.Add(new ListItem("Male"));
- radio.Items.Add(new ListItem("Female"));
- PH_gender.Controls.Add(radio);
- }
- protected void CreateDynamicPanel()
- {
- int c = 1;
- Panel TextPanel = new Panel();
- for (int i = 0; i < c; i++)
- {
- TextPanel.ID = "txtPanel" + i.ToString();
- PlaceHolder1.Controls.Add(TextPanel);
- }
- }
- protected void btn_submit_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < 1; i++)
- {
- string txtvalue = "txtBox" +i.ToString();
- string txtvalue1 = "txtBox1" +i.ToString();
- string ddlvalue = "ddlrank";
- string chkvalue = "chkhobbies";
- string chkvalue1 = "chkhobbies1" + i.ToString();
- string rbvalue = "rblRow" ;
- TextBox txt = (TextBox)PlaceHolder1.FindControl(txtvalue);
- TextBox txt1 = (TextBox)PlaceHolder1.FindControl(txtvalue1);
- DropDownList Dddl=(DropDownList)PlaceHolder1.FindControl(ddlvalue);
- CheckBox Dchk=(CheckBox)PlaceHolder1.FindControl(chkvalue);
- CheckBox Dchk1 = (CheckBox)PlaceHolder1.FindControl(chkvalue1);
- RadioButtonList Drd=(RadioButtonList)PlaceHolder1.FindControl(rbvalue);
- if (txt != null )
- {
- try
- {
- string strText = txt.Text;
- string strText1 = txt1.Text;
- string strddl = Dddl.SelectedItem.Text;
- string strchk = Dchk.Text;
- string strchk1 = Dchk1.Text;
- string strrdb = Drd.Text;
- // store your textbox value in database
- con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString());
- con.Open();
- com = new SqlCommand("insert into Dynamic_Form(UserName,LastName,DropdownlistValue,Chksavings,Chkcurrent,Radiovalue)values('" + strText + "','" + strText1 + "','" + strddl + "','" + strchk + "','" + strchk1 + "','" + strrdb + "')", con);
- com.ExecuteNonQuery();
- con.Close();
- lbl_error.Text = "Successfully Account Created ";
- }
- catch(Exception ex)
- {
- throw ex;
- //lbl_error.Text = "There is a Problem while creating Account ";
- }
- }
- }
- }