C# 向asp表匿名添加控件

C# 向asp表匿名添加控件,c#,asp.net,anonymous-methods,C#,Asp.net,Anonymous Methods,我想匿名完成以下功能。我应该怎么做- TableRow tr = new TableRow(); TableCell tc = new TableCell(); Label lbl = new Label(); lbl.Text = link; tc.Controls.Add(lbl); tr.Cells.Add(tc); tbl

我想匿名完成以下功能。我应该怎么做-

            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 */))

做你想做的事没有匿名的方法

最接近您的是类初始值设定项,您在问题中提到过:

tblMain.Rows.Add(新表行(){Property=“SomeValue”})

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

编辑:

关于在一行代码中执行此操作的评论,请允许我在这里停止。你需要从更大的角度来看——你认为一行代码对另一个开发人员来说有多容易理解,如果你不在的话,他们可能需要看管这些代码

还要考虑调试。如果您正在单步执行代码,并且其中一个控件出现问题,那么将鼠标悬停在变量名上会更容易、更方便,然后必须将头绕在一行上,并尝试通过“立即/局部变量”窗口挖掘属性

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


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

假设您的意思是自动: 由于可以创建一个表行,因此代码可以正常工作。要创建多个,请使用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);
}
int迭代次数=10;
for(int i=1;i
//声明一个新表并将其添加为tdparent的子表
表1=新表();
table1.ID=“tablename”;
表1.Style.Add(HtmlTextWriterStyle.Width,“自动”);
tdparent.Controls.Add(表1);
//标记一个新表行并将其添加为表的子行
TableRow tr1=新的TableRow();
tr1.ID=“tr1Skills”;
表1.控制添加(tr1);
CompanyBAL objBAL=新CompanyBAL();
int-id;
如果(ddlFunctional.SelectedValue==“全部”)
{
id=-99;
}
其他的
{
id=Convert.ToInt32(ddlFunctional.SelectedValue.ToString());
}
数据集ds=objBAL.GetSkillSets(id);
对于(int i=0;i

您可以在表中添加表控件和控件

您的代码是否无效?您所说的“匿名”是什么意思?@ryadavilli:它正在工作,但它每次都创建一行,我需要动态添加多行,是什么阻止了你添加多行?你不能多次调用代码吗?好吧,然后循环运行,或者按照你的逻辑告诉你的方式运行。仍然看不到实际问题是什么?是的,你是对的。但我想这样做这是一行代码。你能推荐一些。。。
//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()));

        }