简体   繁体   中英

Adding Controls to asp Table Anonymously

I want to do the following functionalities anonymously. How should I do this -

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            Label lbl = new Label();
            lbl.Text = link;
            tc.Controls.Add(lbl);
            tr.Cells.Add(tc);
            tblMain.Rows.Add(tr);

Update

as we do -

tblMain.Rows.Add(new TableRow(/* Here I will add a TableCell */))

There is no anonymous method way of doing what you want.

The closest you will come is class initialisers, which you touched upon in your question:

tblMain.Rows.Add(new TableRow() { Property = "SomeValue" })

What class initialisers allow you do is the { } stuff that you see in the above line. So you could add the controls in less lines of code, but you don't really gain anything really, but it's a case of preference.

EDIT:

In response to the comment about doing this in one line of code, let me stop you right there. You need to look at the bigger picture here - how comprehensible do you think that one line of code would be to another developer who, may, have to look after that code if you're not around?

Also think about debugging. If you're stepping through the code and there is a problem with one of the controls, being able to hover the mouse over the variable name is a lot easier, and convenient, then having to wrap your head around a one-liner and trying to dig into properties via the immediate/locals window.

Trust me, I've worked with code like that before and the end result was I wanted find the developer responsible and place their hands in a George Foreman grill set to 170C.

If you are the only developer who will ever look at this code, then fair enough. However, there is no way to refactor this code into a one-liner as far as I can see.

Assuming you mean autonomously: Since you can create one table row, the code works. To create many, use a for loop.

int iterationTimes = 10;

for (int i = 1; i <= iterationTimes ; i++)
{
    TableRow tr = new TableRow();
    TableCell tc = new TableCell();
    Label lbl = new Label();
    lbl.Text = link;
    tc.Controls.Add(lbl);
    tr.Cells.Add(tc);
    tblMain.Rows.Add(tr);
}
//Declare a new table and add it as child of tdparent
        Table table1 = new Table();
        table1.ID = "tablename";
        table1.Style.Add(HtmlTextWriterStyle.Width, "auto");
        tdparent.Controls.Add(table1);


        //Decalre a new table row and add it as child of tableskills
        TableRow tr1 = new TableRow();
        tr1.ID = "tr1Skills";
        table1.Controls.Add(tr1);

        CompanyBAL objBAL = new CompanyBAL();

        int id;


        if (ddlFunctional.SelectedValue == "All")
        {
            id = -99;
        }
        else
        {
            id = Convert.ToInt32(ddlFunctional.SelectedValue.ToString());
        }

        DataSet ds = objBAL.GetSkillSets(id);

        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
        {
            TableCell td = new TableCell();


            td.ID = "td" + ds.Tables[1].Rows[i]["skilltype"].ToString();
            td.Style.Add(HtmlTextWriterStyle.VerticalAlign, "top");
            td.Style.Add(HtmlTextWriterStyle.Width, "auto");


            tr1.Controls.Add(td);


            // add CheckBoxList to tabelCell
            CheckBoxList cbl = new CheckBoxList();


            cbl.ID = "cbl" + ds.Tables[1].Rows[i]["skilltype"].ToString();
            cbl.Style.Add(HtmlTextWriterStyle.Width, "auto");


            td.Controls.Add(cbl);
        }

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TableCell tbCell = ((TableCell)tr1.FindControl("td" + ds.Tables[0].Rows[i]["skilltype"].ToString()));

            CheckBoxList cb= ((CheckBoxList)tbCell.FindControl("cbl" + ds.Tables[0].Rows[i]["skilltype"].ToString()));

            cb.Items.Add(new ListItem(ds.Tables[0].Rows[i]["skillname"].ToString(), ds.Tables[0].Rows[i]["skillname"].ToString()));

        }

You can add table controls and controls inside table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM