匿名添加控件到asp表

时间:2013-01-15 09:33:56

标签: 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);
            tblMain.Rows.Add(tr);

更新

和我们一样 -

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

3 个答案:

答案 0 :(得分:1)

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

你最接近的是课程初始化者,你在问题中提到过:

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

初级用户允许您执行的操作是您在上面一行中看到的{ }内容。所以你可以用更少的代码行添加控件,但你并没有真正获得任何东西,但这是一个偏好的例子。

编辑:

在回复关于在一行代码中执行此操作的评论时,让我阻止您。你需要在这里看一下更大的图片 - 你认为一行代码对于另一个开发人员来说是多么容易理解,如果你不在身边,他们可能需要照看那些代码?

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

相信我,我之前使用过这样的代码,最终的结果是我想让开发人员负责并将他们的手放在设置为170C的George Foreman烧烤架中。

如果您是唯一一位会看到此代码的开发人员,那么公平。但是,就我所见,没有办法将这些代码重构为单行代码。

答案 1 :(得分: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);
}

答案 2 :(得分: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()));

        }

您可以在表格

中添加表格控件和控件
相关问题