开发者

Adding controls to a page?

protected void Page_Load(object sender, EventArgs e)
{
    Button quote = new Button();
    Button reply = new Button();
    Button edit = new Button();
    Label UsersMessage = new Label();
    UsersMessage.Text = "Users Message: question was submitted";


    Table tabl = new Table();
    TableRow tablRow=new TableRow();
    TableCell tablCell=new TableCell();



    tablCell.Controls.Add(quote);
    tablCell.Con开发者_如何学Pythontrols.Add(reply);
    tablCell.Controls.Add(edit);
    tablCell.Controls.Add(UsersMessage);

    tablRow.Cells.Add(tablCell);
    tabl.Rows.Add(tablRow);
}

I am trying to add a table with a row, cell with buttons and a label in it. Why nothing is added, when I launch the page? (There is nothing added in the source code, when I view it)


You also have to add the Table control itself to the page's form:

Page.Form.Controls.Add(tabl);


you do not add the controls to the page itself ;)

controlContainer.Controls.Add(tabl);

would do the trick

where controlContainer is a control you've created on the ASPX like a asp:placeholder or asp:panel. In this way you can define the location where the controls must appear in a specific manner.

important note: however when you add controls in this way, you will enter a worrisome area. If you are intending to use the buttons for postback this approach will not work quite as shown above. Please mind/google/bing terms as asp.net page-lifecycle, viewstate, eventwire control-id's etc... a nice tutorial is here


You didn't add the table to the page controls collection.

this.Controls.Add(tabl);

Or, if you have a specific control you want to add the table to, use that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜