Thursday, February 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,Select one New Asp.Net Web application

Next,Open the Design Default.aspx page

Next,Drag and Drop one DropDown list and button from the Tool box.

Next,Add one new item( i.e., New Registration Page) for the Drop down list.

Procedure for adding items : Right click on the DropDown List and select properties and select items and add NewRegistration in Text Field.Thats it

Next,Change the properties of the Dropdown list

1.Kept AutopostBack = True;

2.select the SelectedIndexChanged Event for that..

Next,Open the code page that is Default.aspx.cs and and create a method in SelectedIndexChanged event of dropdownlist

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)
    {       
       
    }
protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        Createnewaccount(); 
    }
 
 

Next, Create new methods for Different controls by using the main method(i.e., Createnewaccount)

private void Createnewaccount()
    {
        CreateDynamicTextBoxes();
        CreateDynamicTextBoxes1();
        CreateDynamicCheckBoxes();
        CreateDynamicCheckBoxes1();
        CreateDynamicRadioButtons1();
        BindDropDownLists();
    }


Next,Create all types of dynamic controls.

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);
        }
    }

Next,Add the following code to store the data in database
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 ";
                   
                }
            }
        }
    }

That's it .I hope this article will helps you..if you like this article please share...
developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , ,