Senior & Expert .Net Developers Discussion Forum by Nyros Technologies

HIRE .Net Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Dynamically adding required number of fields.

 
Post new topic   Reply to topic    Senior & Expert .Net Developers Discussion Forum by Nyros Technologies Index -> Programming with C#
View previous topic :: View next topic  
Author Message
rajesh



Joined: 26 Mar 2010
Posts: 43

PostPosted: Fri May 14, 2010 4:36 am    Post subject: Dynamically adding required number of fields. Reply with quote

Hi all..,

I hope this may helpful in your projects., In so many projects we required this repeatedly.

So I am giving you a small example to on generating n number of fields at a time and Inserting the n number of fields data into the data base at a time.

just take text box control and a button control in you design page.

MyTask.aspx.cs

using System.Data.SqlClient;

public partial class NumberOFfeildsAtATime : System.Web.UI.Page
{
    static Button btnSubmit = new Button();
    static Button btnReset = new Button();

    static Label lblSno = new Label();
    static Label lblEname = new Label();
    static Label lblPlatform = new Label();
    static Label lblSalary = new Label();
    static Label lblMobileNumber = new Label();
    static Label lblEmailID = new Label();
    static Label lblSurName = new Label();
    static Label lblStatus = new Label();

    static TextBox[] txtEname;
    static TextBox[] txtPlatform;
    static TextBox[] txtSalary;
    static TextBox[] txtMobileNumber;
    static TextBox[] txtEmailid;
    static TextBox[] txtSurName;
   
    static CheckBox []chkStatus;
   
    SqlConnection conn;
    static Table tblDynamic = new Table();
    RequiredFieldValidator[] RfvFirstName;
    RequiredFieldValidator[] RfvLastName;
    RequiredFieldValidator[] RfvPlatform;
    RequiredFieldValidator[] RfvSalary;
    RequiredFieldValidator[] RfvMobile;
    RequiredFieldValidator[] RfvEmailId;

    RegularExpressionValidator[] RevSalary;
    RegularExpressionValidator[] RevMobile;
    RegularExpressionValidator[] RevEmailId;

   
   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            tblDynamic.Rows.Clear();
        conn = new SqlConnection("Data Source=RAJESH\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
        form1.Controls.Add(tblDynamic);
        btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
        btnReset.Click += new EventHandler(this.btnReset_Click);     
    }
    public void GenerateFeilds(int n)
    {
        tblDynamic.Rows.Clear();

        txtSurName = new TextBox[n];
        txtEname = new TextBox[n];
        txtPlatform = new TextBox[n];
        txtSalary = new TextBox[n];
        chkStatus = new CheckBox[n];
        txtMobileNumber = new TextBox[n];
        txtEmailid = new TextBox[n];
        RfvFirstName = new RequiredFieldValidator[n];
        RfvLastName = new RequiredFieldValidator[n];
        RfvEmailId = new RequiredFieldValidator[n];
        RfvMobile = new RequiredFieldValidator[n];
        RfvPlatform = new RequiredFieldValidator[n];
        RfvSalary = new RequiredFieldValidator[n];
        RevSalary = new RegularExpressionValidator[n];
        RevMobile = new RegularExpressionValidator[n];
        RevEmailId = new RegularExpressionValidator[n];

        TableRow []tr = new TableRow[n+2];
        TableCell [ , ]td=new TableCell[n+2,8];
       
        for (int r = 0; r < n + 2; r++)
            for (int i = 0; i < 8; i++)
                td[r, i] = new TableCell();


        tr[0] = new TableRow();

        lblSno.Text = "S.No";
        lblSurName.Text = "Employee First Name";
        lblEname.Text = "Employee Last Name";
        lblPlatform.Text="Platform";
        lblSalary.Text = "Salary";
        lblStatus.Text = "Working Status";
        lblMobileNumber.Text = "Mobile Number";
        lblEmailID.Text = "Email Id";
       
        td[0,0].Controls.Add(lblSno);
        td[0,1].Controls.Add(lblSurName);
        td[0,2].Controls.Add(lblEname);
        td[0,3].Controls.Add(lblPlatform);
        td[0,4].Controls.Add(lblStatus);
        td[0,5].Controls.Add(lblMobileNumber);
        td[0,6].Controls.Add(lblEmailID);
        td[0,7].Controls.Add(lblSalary);

       
        for (int i = 0; i < 8; i++ )       
            tr[0].Cells.Add(td[0,i]);
       
        tblDynamic.Rows.Add(tr[0]);       

        for (int r = 1; r < n + 1; r++)
        {
            txtSurName[r - 1] = new TextBox();
            txtEname[r - 1] = new TextBox();           
            txtPlatform[r - 1] = new TextBox();
            txtPlatform[r - 1].Width = 75;           
            txtSalary[r - 1] = new TextBox();
            txtSalary[r-1].Width=75;           
            chkStatus[r - 1] = new CheckBox();           
            txtMobileNumber[r-1] = new TextBox();
            txtMobileNumber[r - 1].Width = 75;
            txtEmailid[r-1] = new TextBox();

            txtSurName[r - 1].ID = "txtSurName" + r;
            txtEname[r - 1].ID = "txtEname" + r;
            txtPlatform[r - 1].ID = "txtPlatform" + r;
            txtSalary[r - 1].ID = "txtSalary" + r;
            chkStatus[r - 1].ID = "chkStatus" + r;           
            txtMobileNumber[r-1].ID = "txtMobileNumber"+r;
            txtEmailid[r-1].ID = "txtEmailid"+r;

            txtSurName[r - 1].ValidationGroup = "g1";
            txtEname[r - 1].ValidationGroup = "g1";
            txtPlatform[r - 1].ValidationGroup = "g1";
            txtSalary[r - 1].ValidationGroup = "g1";
            txtMobileNumber[r - 1].ValidationGroup = "g1";
            txtMobileNumber[r - 1].MaxLength = 10;
            txtEmailid[r - 1].ValidationGroup = "g1";

            RfvFirstName[r - 1] = new RequiredFieldValidator();
            RfvFirstName[r - 1].ControlToValidate = txtSurName[r - 1].ID;
            RfvFirstName[r - 1].ValidationGroup = "g1";
            RfvFirstName[r - 1].Text = "*";
            RfvLastName[r - 1] = new RequiredFieldValidator();
            RfvLastName[r - 1].ControlToValidate = txtEname[r - 1].ID;
            RfvLastName[r - 1].ValidationGroup = "g1";
            RfvLastName[r - 1].Text = "*";
            RfvPlatform[r - 1] = new RequiredFieldValidator();
            RfvPlatform[r - 1].ControlToValidate = txtPlatform[r - 1].ID;
            RfvPlatform[r - 1].ValidationGroup = "g1";
            RfvPlatform[r - 1].Text = "*";
            RfvSalary[r - 1] = new RequiredFieldValidator();
            RfvSalary[r - 1].ControlToValidate = txtSalary[r - 1].ID;
            RfvSalary[r - 1].ValidationGroup = "g1";
            RfvSalary[r - 1].Text = "*";
            RfvMobile[r - 1] = new RequiredFieldValidator();
            RfvMobile[r - 1].ControlToValidate = txtMobileNumber[r - 1].ID;
            RfvMobile[r - 1].ValidationGroup = "g1";
            RfvMobile[r - 1].Text = "*";
            RfvEmailId[r - 1] = new RequiredFieldValidator();
            RfvEmailId[r - 1].ControlToValidate = txtEmailid[r - 1].ID;
            RfvEmailId[r - 1].ValidationGroup = "g1";
            RfvEmailId[r - 1].Text = "*";
            RevEmailId[r - 1] = new RegularExpressionValidator();
            RevEmailId[r - 1].ControlToValidate = txtEmailid[r - 1].ID;
            RevEmailId[r - 1].ValidationGroup = "g1";
            RevEmailId[r - 1].Text = "*";
            RevEmailId[r - 1].ValidationExpression = "^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$";
            RevMobile[r - 1] = new RegularExpressionValidator();
            RevMobile[r - 1].ControlToValidate = txtMobileNumber[r - 1].ID;
            RevMobile[r - 1].ValidationGroup = "g1";
            RevMobile[r - 1].Text = "*";
            RevMobile[r - 1].ValidationExpression = "^\\d{10}$";
            RevSalary[r - 1] = new RegularExpressionValidator();
            RevSalary[r - 1].Text = "*";
            RevSalary[r - 1].ControlToValidate = txtSalary[r - 1].ID;
            RevSalary[r - 1].ValidationGroup = "g1";
            RevSalary[r - 1].ValidationExpression = "^([0-9]*|\\d*\\.\\d{2})$";

            td[r, 0].Text = r.ToString();
            td[r, 1].Controls.Add(txtSurName[r - 1]);
            td[r, 1].Controls.Add(RfvFirstName[r - 1]);
            td[r, 2].Controls.Add(txtEname[r-1]);
            td[r, 2].Controls.Add(RfvLastName[r-1]);
            td[r, 3].Controls.Add(txtPlatform[r - 1]);
            td[r, 3].Controls.Add(RfvPlatform[r - 1]);
            td[r, 4].Controls.Add(chkStatus[r - 1]);           
            td[r, 5].Controls.Add(txtMobileNumber[r - 1]);
            td[r, 5].Controls.Add(RfvMobile[r - 1]);
            td[r, 5].Controls.Add(RevMobile[r - 1]);
            td[r, 6].Controls.Add(txtEmailid[r - 1]);
            td[r, 6].Controls.Add(RfvEmailId[r - 1]);
            td[r, 6].Controls.Add(RevEmailId[r - 1]);
            td[r, 7].Controls.Add(txtSalary[r - 1]);
            td[r, 7].Controls.Add(RfvSalary[r-1]);
            td[r, 7].Controls.Add(RevSalary[r - 1]);

            tr[r] = new TableRow();

            for (int i = 0; i < 8; i++)
                tr[r].Cells.Add(td[r, i]);

            tblDynamic.Rows.Add(tr[r]);
           
        }
        btnSubmit.Text="Submit";
        btnReset.Text="Reset";
        btnSubmit.ValidationGroup = "g1";
       
        tr[n + 1] = new TableRow();
        td[n+1,6].Controls.Add(btnSubmit);
        td[n+1,7].Controls.Add(btnReset);
        tr[n + 1].Cells.Add(td[n + 1, 6]);
        tr[n + 1].Cells.Add(td[n + 1, 7]);
        tblDynamic.Rows.Add(tr[n + 1]);
        form1.Controls.Add(tblDynamic);
       
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt16(txtNumber.Text) <= 0)
        {               
            Response.Write("<script>alert('Please enter Greterthan Zero values only')</script>");
        }
        else
        {
            btnCreate.Visible = false;
            txtNumber.Visible = false;
            btnClear.Visible = true;
            lblRequest.Visible = false;
            GenerateFeilds(Convert.ToInt16(txtNumber.Text));
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            conn.Open();
            for (int r = 0; r < tblDynamic.Rows.Count - 2; r++)
            {
                SqlCommand cmd = new SqlCommand("insert into Testing2 (FirstName, Name, salary, Platform, MobileNumber, EmailId, WorkingStatus) values('" + txtSurName[r].Text + "','" + txtEname[r].Text + "','" + txtSalary[r].Text + "','" + txtPlatform[r].Text + "','" + txtMobileNumber[r].Text + "','" + txtEmailid[r].Text + "','" + chkStatus[r].Checked + "')", conn);
                cmd.ExecuteNonQuery();

            }
            Response.Write("<script>alert('Inserted succesfully')</script>");
            conn.Close();
            btnReset_Click(sender, e);
        }
        catch
        {
            Response.Write("<script>alert('Not Inserted succesfully')</script>");
            conn.Close();
        }
       

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        int n=tblDynamic.Rows.Count;
        for (int r = 0; r < n-2; r++)
        {
            txtEname[r].Text="";
            txtPlatform[r].Text="";
            txtSalary[r].Text="";
            chkStatus[r].Checked=false;
            txtSurName[r].Text = "";
            txtMobileNumber[r].Text = "";
            txtEmailid[r].Text = "";           
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        tblDynamic.Rows.Clear();
        form1.Controls.Add(tblDynamic);
        txtNumber.Visible = true;
        btnClear.Visible = false;
        btnCreate.Visible = true;
        lblRequest.Visible = true;
    }
   
   
}


_________________
Cool***************Cool

Best Regards
Rajesh Mani.
Nyros Technologies.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert .Net Developers Discussion Forum by Nyros Technologies Index -> Programming with C#
Page 1 of 1


 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts How to Add Update Delete for gridview using Webservice
1 sanjay 31 Fri Sep 10, 2010 7:05 am
setty View latest post
No new posts Is there any free webhosts that support ASP.NET. ?
2 Balaji 634 Tue Sep 07, 2010 10:24 am
chrisadam View latest post
No new posts Hostings For ASP.NET 2.0 with SQL Server
2 Avash 676 Tue Sep 07, 2010 10:21 am
chrisadam View latest post
No new posts Exceptional Manger
1 suraj 51 Thu Sep 02, 2010 8:52 am
Rahul View latest post
No new posts background image for UITableView
0 kumar 55 Wed Sep 01, 2010 10:34 am
kumar View latest post
No new posts Types of Run time exceptions
1 Dheeraj 52 Wed Sep 01, 2010 8:58 am
venkat View latest post
No new posts Access Web Page content in Windows Forms without WebBrowser
3 mouli 273 Wed Sep 01, 2010 7:31 am
rajesh View latest post
No new posts Explain Interlocked Class & Daemon Thread's?
1 mukundh 76 Wed Sep 01, 2010 7:19 am
setty View latest post
No new posts Life Cycle Of Delegate
0 vinay 40 Wed Sep 01, 2010 7:14 am
vinay View latest post
No new posts Accessing methods defined in another page
1 suraj 37 Tue Aug 31, 2010 12:45 pm
Rahul View latest post




Hire an expert .Net developer / coder / programmer or development team from India now!!

Other Forums : Ruby on Rails   ::   PHP   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About .Net Experts   |   More