Friday, February 10, 2012

Generate Controls Dynamically and adding to the page in asp.net

Getting data from database and generate controls based on table(database) columns and rows, generate label and textbox controls with in a html table dynamically and added to the Page in asp.net.....

private void getallowance()
    {
        DataTable dt = BLayer.getAllowColumns();
        if (dt.Rows.Count > 0)
        {
            //HtmlTableRow htr = new HtmlTableRow();

            Table tbl = new Table();
            tbl.ID = "table1";
            Page.Form.Controls.Add(tbl);
            foreach (DataRow dr in dt.Rows)
            {
                Label lbl = new Label();
                HtmlTableRow htr = new HtmlTableRow();
                HtmlTableCell htc = new HtmlTableCell();
                //htc.ColSpan = 2;
                for (int irow = 0; irow < 6; irow++)
                {
                    htc = new HtmlTableCell();

                 

                    string strcolname = dr["columnname"].ToString();
                    string[] strtestcol = strcolname.Split("_".ToCharArray());
          
                   
                    if (strtestcol.Length <= 1)
                    {
                        if (strcolname.ToUpper() == "CUG" || strcolname.ToUpper() == "TDS" || strcolname.ToUpper() == "LOAN")
                        {
                            if (irow == 4)
                            {
                                lbl.ID = "lbl" + strcolname;
                                lbl.Text = dr["Heading"].ToString();
                                htc.Controls.Add(lbl);
                                htc.Width = "265";
                            }
                            if (irow == 5)
                            {
                                TextBox TextBox1 = new TextBox();
                                TextBox1.ID = "txt" + strcolname;
                                TextBox1.CssClass = "formstyle01";
                                TextBox1.Enabled = false;
                                TextBox1.Font.Bold = true;
                                TextBox1.Text = string.Empty;
                                TextBox1.Style.Add("text-align", "right");
                                htc.Controls.Add(TextBox1);
                                htc.Width = "150";
                            }

                            htr.Cells.Add(htc);
                            tblcolded.Rows.Add(htr);
                        }
                    }
                    else
                    {
                        //htr = new HtmlTableRow();
                        if (irow == 2)
                        {
                            htc.Style["bgcolor"] = "#807d7d";
                            //htc.Controls.Add(cell);
                        }
                        if (irow == 3)
                        {
                         
                            //row.Style["bgcolor"] = "#807d7d";
                            //htc.Controls.Add(row);
                        }
                        if (strtestcol[1].ToString() == "a")
                        {
                            if (irow == 0)
                            {
                                lbl.ID = "lbl" + strcolname;
                                lbl.Text = dr["Heading"].ToString();
                                htc.Controls.Add(lbl);
                                htc.Width = "265";
                            }
                            if (irow == 1)
                            {
                                TextBox TextBox1 = new TextBox();
                                TextBox1.ID = "txt" + strcolname;
                                TextBox1.CssClass = "formstyle01";
                                TextBox1.Enabled = false;
                                TextBox1.Font.Bold = true;
                                TextBox1.Text = string.Empty;
                                TextBox1.Style.Add("text-align", "right");
                                htc.Controls.Add(TextBox1);
                                //htr.Cells.Add(htc);
                                htc.Width = "150";
                                htc.VAlign = "middle";
                                htc.Align = "left";                              
                            }
                            htr.Cells.Add(htc);
                            htr.VAlign = "top";
                            tblcol.Rows.Add(htr);
                          
                        }
                        else if (strtestcol[1].ToString() == "d")
                        {
                            if (irow == 4)
                            {
                                //Label lbl = new Label();
                                lbl.ID = "lbl" + strcolname;
                                lbl.Text = dr["Heading"].ToString();
                                htc.Controls.Add(lbl);
                                htc.Width = "265";
                            }
                            if (irow == 5)
                            {
                                TextBox TextBox1 = new TextBox();
                                TextBox1.ID = "txt" + strcolname;
                                TextBox1.CssClass = "formstyle01";
                                TextBox1.Enabled = false;
                                TextBox1.Font.Bold = true;

                                TextBox1.Style.Add("text-align", "right");
                                TextBox1.Text = string.Empty;
                                htc.Controls.Add(TextBox1);
                                //htr.Cells.Add(htc);
                                htc.Width = "150";
                                //tblcol.Rows.Add(htr);
                            }

                            htr.Cells.Add(htc);
                            //tblcol.Rows.Add(htr);
                            tblcolded.Rows.Add(htr);
                        }
                    }              
                 
                }              

            }
          
        }
      
    }

No comments:

Post a Comment

Comments

Search

Custom Search
Protected by Copyscape Plagiarism Software