Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a label or other control based on database table value

Tags:

c#

asp.net

c#-4.0

I want to create a label or textbox based on database column value.. For example: I have a database table column value Casual Leave,Medical Leave,Annual leave,etc.. I want to create dynamic label and corresponding textbox for the above column value.

I want the control like this..

         Casual Leave :  Textbox1
         Medical Leave:  Textbox2
         Annul Leave  :  Textbox3
         etc based on table value

what i do? I can't do this. please help me...

like image 391
Fernando Avatar asked Oct 08 '22 14:10

Fernando


1 Answers

Try this

<asp:Panel Id="pnl" runat ="server">
    </asp:Panel>


Label lblT = null;
            TextBox txt = null;
            Table tb = new Table();
            pnl.Controls.Add(tb);
            DataTable table = DT_GeneratedOp();
            foreach (DataColumn dr in table.Columns)
            {
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                TableCell tc2 = new TableCell();
                lblT = new Label();
                txt = new TextBox();
                lblT.Text = dr.ColumnName + ":";
                txt.Text = table.Rows[0][dr].ToString();
                tc.Controls.Add(lblT);
                tc2.Controls.Add(txt);
                tr.Cells.Add(tc);
                tr.Cells.Add(tc2);
                tb.Rows.Add(tr);
            }
like image 149
Sanjay Goswami Avatar answered Oct 13 '22 11:10

Sanjay Goswami