0

我想匿名执行以下功能。我该怎么做-

            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);

更新

就像我们一样——

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

3 回答 3

1

没有匿名方法可以做你想做的事。

您最接近的是类初始化程序,您在问题中提到了它:

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

类初始化器允许你做的是{ }你在上面一行看到的东西。因此,您可以用更少的代码行添加控件,但您并没有真正获得任何好处,但这是一种偏好。

编辑:

为了回应关于在一行代码中执行此操作的评论,让我在此阻止您。您需要在这里查看更大的图景 - 您认为一行代码对于另一位开发人员来说有多容易理解,如果您不在,他们可能必须照顾该代码?

还要考虑调试。如果您正在单步执行代码并且其中一个控件出现问题,则能够将鼠标悬停在变量名称上会更容易,也更方便,那么您必须将头绕在单行上并尝试通过即时/本地窗口挖掘属性。

相信我,我以前使用过类似的代码,最终结果是我希望找到负责的开发人员并将他们的手放在设置为 170C 的 George Foreman 烤架上。

如果您是唯一会查看此代码的开发人员,那么就足够公平了。但是,据我所知,没有办法将此代码重构为单行代码。

于 2013-01-15T09:44:30.060 回答
0

假设您的意思是自主:由于您可以创建一个表格行,因此代码有效。要创建多个,请使用 for 循环。

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);
}
于 2013-01-15T09:51:41.150 回答
0
//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()));

        }

您可以在表格内添加表格控件和控件

于 2013-01-15T09:54:04.970 回答