DotNetSlackers: ASP.NET News for lazy Developers

Wednesday, August 3, 2011

How to create a Dynamic Registration Form in Aspnet

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

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14.   
  15. public partial class Dynamic_controls : System.Web.UI.Page  
  16. {  
  17.     SqlConnection con;  
  18.     SqlCommand com;  
  19.   protected void Page_Load(object sender, EventArgs e)  
  20.     {  
  21.         if (IsPostBack)  
  22.         {  
  23.            Createnewaccount();                       
  24.         }  
  25.          
  26.     }  
  27.   
  28.  private void Createnewaccount()  
  29.     {  
  30.         CreateDynamicTextBoxes();  
  31.         CreateDynamicTextBoxes1();  
  32.         CreateDynamicCheckBoxes();  
  33.         CreateDynamicCheckBoxes1();  
  34.         CreateDynamicRadioButtons1();  
  35.         BindDropDownLists();  
  36.     }  
  37.   
  38. protected void CreateDynamicTextBoxes()  
  39.     {  
  40.        
  41.         int c = 1;  
  42.         TextBox[] textBoxArr = new TextBox[c];//array of textboxes  
  43.         for (int i = 0; i < c; i++)  
  44.         {  
  45.              
  46.             textBoxArr[i] = new TextBox();  
  47.             textBoxArr[i].ID = "txtBox" + i.ToString();           
  48.             PH_Name.Controls.Add(textBoxArr[i]);  
  49.             RequiredFieldValidator reqfldVal = new RequiredFieldValidator();  
  50.             reqfldVal.ID = "RequiredValidator" + i;  
  51.             reqfldVal.ControlToValidate = "txtBox" + i;  
  52.             reqfldVal.ErrorMessage = "Not Empty";  
  53.             reqfldVal.SetFocusOnError = true;  
  54.             PH_Name.Controls.Add(reqfldVal);  
  55.         }  
  56.     }  
  57.     protected void CreateDynamicTextBoxes1()  
  58.     {       
  59.         int c = 1;  
  60.         TextBox[] textBoxArr1 = new TextBox[c];//array of textboxes  
  61.         for (int i = 0; i < c; i++)  
  62.         {  
  63.   
  64.             textBoxArr1[i] = new TextBox();  
  65.             textBoxArr1[i].ID = "txtBox1" + i.ToString();  
  66.             PH_pwd.Controls.Add(textBoxArr1[i]);  
  67.             RequiredFieldValidator reqfldVal1 = new RequiredFieldValidator();  
  68.             reqfldVal1.ID = "RequiredValidator1" + i;  
  69.             reqfldVal1.ControlToValidate = "txtBox1" + i;  
  70.             reqfldVal1.ErrorMessage = "Not Empty";  
  71.             reqfldVal1.SetFocusOnError = true;  
  72.             PH_pwd.Controls.Add(reqfldVal1);  
  73.              
  74.         }  
  75.     }  
  76.     private void BindDropDownLists()  
  77.     {  
  78.         SqlDataSource sqlDS = new SqlDataSource();  
  79.         sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString();  
  80.         sqlDS.SelectCommand = "Select countryid, country_name from countrynames";  
  81.         PH_country.Controls.Add(sqlDS);  
  82.         DropDownList ddl = new DropDownList();  
  83.         ddl.ID = "ddlrank";  
  84.         ddl.DataSource = sqlDS;  
  85.         ddl.DataTextField = "country_name";  
  86.         ddl.DataValueField = "countryid";  
  87.         PH_country.Controls.Add(ddl);  
  88.         foreach (Control ctl in PH_country.Controls)  
  89.         {  
  90.             if (ctl is DropDownList)  
  91.             {  
  92.                 (ctl as DropDownList).DataBind();  
  93.             }  
  94.         }  
  95.     }  
  96.     protected void CreateDynamicCheckBoxes()  
  97.     {  
  98.                
  99.             CheckBox chk;       
  100.             chk = new CheckBox();  
  101.             chk.ID = "chkhobbies";             
  102.             chk.Text = "savings";  
  103.             chk.AutoPostBack = false;  
  104.             PH_trans.Controls.Add(chk);             
  105.          
  106.     }   
  107.     protected void CreateDynamicCheckBoxes1()  
  108.     {  
  109.        
  110.         int c = 1;  
  111.         CheckBox[] chk = new CheckBox[c];  
  112.         for (int i = 0; i < c; i++)  
  113.         {  
  114.             chk[i] = new CheckBox();  
  115.             chk[i].ID = "chkhobbies1" + i.ToString();  
  116.             chk[i].Text = "Current";           
  117.             chk[i].AutoPostBack = false;  
  118.             PH_trans.Controls.Add(chk[i]);  
  119.         }  
  120.     }     
  121.     protected void CreateDynamicRadioButtons()  
  122.     {  
  123.             RadioButton male = new RadioButton();  
  124.             RadioButton female = new RadioButton();  
  125.             male = new RadioButton();  
  126.             female = new RadioButton();  
  127.             male.Text = "Male";  
  128.             female.Text = "Female";  
  129.             male.Checked = false;  
  130.             female.Checked = false;  
  131.             male.GroupName = "gender";  
  132.             female.GroupName = "gender";  
  133.             male.ID = "malegender" ;  
  134.             female.ID = "gender";  
  135.             male.AutoPostBack = false;  
  136.             female.AutoPostBack = false;  
  137.             PH_gender.Controls.Add(male);  
  138.             PH_gender.Controls.Add(female);  
  139.     }  
  140.     protected void CreateDynamicRadioButtons1()  
  141.     {  
  142.         RadioButtonList radio = new RadioButtonList();  
  143.         radio.ID = "rblRow";  
  144.         radio.Items.Add(new ListItem("Male"));  
  145.         radio.Items.Add(new ListItem("Female"));       
  146.         PH_gender.Controls.Add(radio);  
  147.          
  148.     }  
  149.     protected void CreateDynamicPanel()  
  150.     {  
  151.        
  152.         int c = 1;  
  153.         Panel TextPanel = new Panel();  
  154.         for (int i = 0; i < c; i++)  
  155.         {  
  156.             TextPanel.ID = "txtPanel" + i.ToString();  
  157.             PlaceHolder1.Controls.Add(TextPanel);  
  158.         }  
  159.     }  
  160.   
  161. protected void btn_submit_Click(object sender, EventArgs e)  
  162.     {  
  163.         for (int i = 0; i < 1; i++)  
  164.         {  
  165.              
  166.             string txtvalue = "txtBox" +i.ToString();  
  167.             string txtvalue1 = "txtBox1" +i.ToString();  
  168.             string ddlvalue = "ddlrank";             
  169.             string chkvalue = "chkhobbies";  
  170.             string chkvalue1 = "chkhobbies1" + i.ToString();  
  171.             string rbvalue = "rblRow" ;  
  172.              
  173.             TextBox txt = (TextBox)PlaceHolder1.FindControl(txtvalue);  
  174.             TextBox txt1 = (TextBox)PlaceHolder1.FindControl(txtvalue1);  
  175.             DropDownList Dddl=(DropDownList)PlaceHolder1.FindControl(ddlvalue);  
  176.             CheckBox Dchk=(CheckBox)PlaceHolder1.FindControl(chkvalue);  
  177.             CheckBox Dchk1 = (CheckBox)PlaceHolder1.FindControl(chkvalue1);  
  178.             RadioButtonList Drd=(RadioButtonList)PlaceHolder1.FindControl(rbvalue);  
  179.             if (txt != null )  
  180.             {  
  181.                 try  
  182.                 {  
  183.                     string strText = txt.Text;  
  184.                     string strText1 = txt1.Text;  
  185.                     string strddl = Dddl.SelectedItem.Text;  
  186.                     string strchk = Dchk.Text;  
  187.                     string strchk1 = Dchk1.Text;  
  188.                     string strrdb = Drd.Text;  
  189.                     // store your textbox value in database  
  190.                     con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString());  
  191.                     con.Open();  
  192.                     com = new SqlCommand("insert into Dynamic_Form(UserName,LastName,DropdownlistValue,Chksavings,Chkcurrent,Radiovalue)values('" + strText + "','" + strText1 + "','" + strddl + "','" + strchk + "','" + strchk1 + "','" + strrdb + "')", con);  
  193.                     com.ExecuteNonQuery();  
  194.                     con.Close();  
  195.                     lbl_error.Text = "Successfully Account Created ";  
  196.                      
  197.                 }  
  198.                 catch(Exception ex)  
  199.                 {  
  200.                     throw ex;  
  201.                     //lbl_error.Text = "There is a Problem while creating Account ";  
  202.                      
  203.                 }  
  204.             }  
  205.         }  
  206.     }  

No comments:

Post a Comment