Hi,
I am create one web application in which i have to create dyanamically panel and table,tr,td,labels and textboxes. but when i split the string and create tr but more tr created i want 4 tds in 1 tr but they create 4 tr dynamically..
how to solve this problem..

private void GetLevels(string InstituteId, Int64 AdmissionProcessId, Int64 StepId)
    {
        int Col1=0,Col2=0;

        try
        {
            DataSet ds;
            ds = BAdmi_AdmissionApplicationMetadata.GetLevels(InstituteId, AdmissionProcessId, StepId);

            string PnlName, MetaFieldGroup_string;
            int MetaFieldGroupSequence_Int;

            if (ds.Tables[0].Rows.Count > 0)
                tdMetaDataTitle.Visible = true;
            else
                tdMetaDataTitle.Visible = false;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                MetaFieldGroupSequence_Int = Convert.ToInt16(ds.Tables[0].Rows[i]["MetaFieldGroupSequence"].ToString());
                PnlName = "pnl_" + ds.Tables[0].Rows[i]["MetaFieldGroupSequence"].ToString();
                Panel pnlLevel = new Panel();
                pnlLevel.ID = PnlName;
                pnlLevel.Attributes.Add("style", "overflow: auto; width: 100%; height: 100% HorizontalAlign=Left");
                pnlLevel.GroupingText = ds.Tables[0].Rows[i]["MetaFieldGroup"].ToString();
                MetaFieldGroup_string = ds.Tables[0].Rows[i]["MetaFieldGroup"].ToString();
                pnlLevel.TabIndex = (short)(17 + i);
                datapanel.Controls.Add(pnlLevel);
                pnlLevel.CssClass = "PanelStyle";
                Table tblFields = new Table();
                tblFields.ID = "tbl_" + ds.Tables[0].Rows[i]["MetaFieldGroupSequence"].ToString();
                tblFields.Attributes.Add("style", "Align:Left");
                tblFields.BorderColor = Color.Black;
                pnlLevel.Controls.Add(tblFields);

                DataSet dsFields = new DataSet();
                dsFields = BAdmi_AdmissionStudentApplication.GetApplicationMetaFiledName(InstituteId, Convert.ToInt64(ddlAdmissionProcessName.SelectedValue.ToString()), Convert.ToInt64(lblStepId_New.Text), MetaFieldGroup_string, MetaFieldGroupSequence_Int);
                if (dsFields.Tables[0].Rows.Count > 0)
                {

                    for (int j = 0; j < dsFields.Tables[0].Rows.Count; j++)
                    {
                        string FieldNameTotalValue = dsFields.Tables[0].Rows[0]["Meta_FieldName"].ToString();

                        string[] MetaFiled_Sub;
                        string[] MetaFiled_SubData;

                        MetaFiled_Sub = FieldNameTotalValue.Split(',');
                        string Metafiled_SubLength = MetaFiled_Sub.Count().ToString();

                        for (int k = 0; k < Convert.ToInt64(Metafiled_SubLength) - 1; k++)
                        {
                            MetaFiled_SubData = MetaFiled_Sub[k].Split('-');
                            TableRow tr = new TableRow();
                            tr.ID = "tr_" + ds.Tables[0].Rows[i]["MetaFieldGroupSequence"].ToString() + j.ToString();
                            tr.Attributes.Add("style", "Align:Left");
                            TableCell tc1 = new TableCell();
                            tc1.ID = "tclabel_" + MetaFiled_SubData[1];
                            //tc1.ID = "tclabel_" + dsFields.Tables[0].Rows[j]["Meta_FieldName"].ToString();
                            tc1.Attributes.Add("style", "Align:Left");
                            tc1.CssClass = "style5";
                            Label lblFieldName1 = new Label();

                            lblFieldName1.ID = "field_" + MetaFiled_SubData[1];

                            //   lblFieldName1.ID = "field_" + dsFields.Tables[0].Rows[j]["Meta_FieldName"].ToString();
                            //  lblFieldName1.Text = dsFields.Tables[0].Rows[j]["Meta_FieldName"].ToString();

                            lblFieldName1.Text = MetaFiled_SubData[1];

                            tc1.Controls.Add(lblFieldName1);
                            tr.Cells.Add(tc1);
                            Col1 = Col1 + 1;

                            TableCell tc2 = new TableCell();

                            tc2.ID = "tctxt_" + MetaFiled_SubData[1];
                            // tc2.ID = "tctxt_" + dsFields.Tables[0].Rows[j]["Meta_FieldName"].ToString();
                            tc2.Attributes.Add("style", "Align:Left");
                            TextBox txtField1 = new TextBox();

                            txtField1.ID = "txt_" + MetaFiled_SubData[1];

                            // txtField1.ID = "txt_" + dsFields.Tables[0].Rows[j]["Meta_FieldName"].ToString();
                            tc2.Attributes.Add("style", "Width:199px");
                            tc2.Controls.Add(txtField1);
                            tr.Cells.Add(tc2);
                            Col2 = Col2 + 1;

                            if (dsFields.Tables[0].Rows.Count >= j + 2)
                            {

                                TableCell tc3 = new TableCell();
                                tc3.ID = "tclabel_" + MetaFiled_SubData[1];
                                //tc3.ID = "tclabel_" + dsFields.Tables[0].Rows[j + 1]["Meta_FieldName"].ToString();
                                tc3.Attributes.Add("style", "Align:Left");
                                Label lblFieldName2 = new Label();
                                //lblFieldName2.ID = "field_" + dsFields.Tables[0].Rows[j + 1]["Meta_FieldName"].ToString();
                                //lblFieldName2.Text = dsFields.Tables[0].Rows[j + 1]["Meta_FieldName"].ToString();

                                lblFieldName2.ID = "field_" + MetaFiled_SubData[1];
                                lblFieldName2.Text = MetaFiled_SubData[1];


                                tc3.Controls.Add(lblFieldName2);
                                tc3.CssClass = "style5";
                                tr.Cells.Add(tc3);

                                TableCell tc4 = new TableCell();
                                tc4.ID = "tctxt_" + MetaFiled_SubData[1];
                                //tc4.ID = "tctxt_" + dsFields.Tables[0].Rows[j + 1]["Meta_FieldName"].ToString();
                                tc4.Attributes.Add("style", "Align:Left");
                                TextBox txtField2 = new TextBox();
                                //txtField2.ID = "txt_" + dsFields.Tables[0].Rows[j + 1]["Meta_FieldName"].ToString();
                                txtField2.ID = "txt_" + MetaFiled_SubData[1];
                                tc4.Controls.Add(txtField2);
                                tr.Cells.Add(tc4);
                            }
                            j = j+ 1;
                            tblFields.Rows.Add(tr);

                        }
                    }
                }
            }
        }
        catch (Exception)
        {
        }
    }

Dani AI

Generated

A common cause of the symptom described is creating a new TableRow for every single field iteration, so each label/textbox pair ends up in its own <tr>. Creating the row once and then adding up to two label+input pairs (four <td> total) before adding the row to the Table fixes the layout. This follows ’s suggestion; ’s Literal idea is a valid alternative when purely static HTML is acceptable, but server-side controls need the Table/TableRow approach to preserve events and ViewState.

Suggested pattern (build a flat list of field names, then loop two fields per row):

// build a cleaned list of fields
var fields = rawFieldString
    .Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)
    .Select(s => s.Trim())
    .ToList();

// create rows with up to two fields (label+input each => 4 tds)
for (int i = 0; i < fields.Count; i += 2)
{
    var tr = new TableRow();
    for (int f = 0; f < 2; f++)
    {
        if (i + f < fields.Count)
        {
            tr.Cells.Add(new TableCell { Controls = { new Label { ID = "lbl_" + (i + f), Text = fields[i + f] } } });
            tr.Cells.Add(new TableCell { Controls = { new TextBox { ID = "txt_" + (i + f) } } });
        }
        else
        {
            // filler cells to keep layout consistent
            tr.Cells.Add(new TableCell());
            tr.Cells.Add(new TableCell());
        }
    }
    tblFields.Rows.Add(tr);
}

Troubleshooting notes: do not increment the loop index inside inner logic (avoid modifying the loop variable manually). Use the correct data-row index when reading from the DataSet (avoid always using row 0). Ensure control IDs are globally unique (include group/sequence + index). Recreate dynamic controls early on every postback (Page_Init) so ViewState and event wiring work. Inspect the generated HTML with browser dev tools to confirm each <tr> has exactly four <td>.

Recommended Answers

All 2 Replies

Hi,

You are creating the <tr> within the for loop. Thus it creating multiple table rows.

Try to put it before the for loops.

you can use Ramesh' idea or you can use Literal control to setup wotever you need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.