如何在C#中的动态页面中添加动态字段

时间:2012-04-27 06:41:03

标签: c# asp.net

我想添加一个动态字段作为"添加更多技能"显示一些文本框和标签将点击此链接。你可以在一些Shine.com,TimesJob等上看到这种例子......

1 个答案:

答案 0 :(得分:0)

这是开始的事情。根据您的需要进行修改。创建一个新按钮并单击按钮可动态创建新控件。我在下面提到的代码中动态创建了一个标签和文本框 ASP按钮

<asp:Button ID="AddMoreSkills" runat="server" Text="Add More Skills" 
            onclick="AddMoreSkills_Click" />

C#中的OnClick事件

protected void AddMoreSkills_Click(object sender, EventArgs e)
{
    Table tblmain = new Table();
    tblmain.ID = "tblmain";
    tblmain.Width = Unit.Percentage(100);
    tblmain.Attributes.CssStyle.Add("margin-top", "5px");
    tblmain.Attributes.CssStyle.Add("margin-bottom", "5px");

    TableCell tblTCell;
    TableRow tblRow = new TableRow();
    TableCell tblCell = new TableCell();

    tblRow = new TableRow();

    //Create Label Dynamically
    tblCell = new TableCell();
    Label lblTown = new Label();
    lblTown.ID = "lblSkill";
    lblTown.Text = "Skill";

    //Add label to table cell
    tblCell.Controls.Add(lblTown);
    tblRow.Cells.Add(tblCell);

    //Create TextBox Dynamically
    TextBox txtSkill = new TextBox();
    txtSkill.ID = "txtSkill";

    //Add TextBox to table cell
    tblTCell = new TableCell();
    tblTCell.Controls.Add(txtSkill);
    tblRow.Cells.Add(tblTCell);
    tblmain.Rows.Add(tblRow);

    form1.Controls.Add(tblmain);
}