Table not showing

Discussion in 'ASP .Net' started by Epetruk, Feb 11, 2005.

  1. Epetruk

    Epetruk Guest

    Hi all,

    I'm trying to write a simple page where a table's properties and contained
    controls are generated dynamically.

    I have the following code:



    In Test.aspx:

    <%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false"
    Inherits="Test.TestForm" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Test</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="TestForm" method="post" runat="server">
    <table id="TestTable" runat="server">
    </table>
    </form>
    </body>
    </HTML>



    In test.aspx.cs:

    public class TestForm : System.Web.UI.Page
    {
    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    TestTable = new HtmlTable();
    TestTable.Width = "100%";
    HtmlTableRow rw = new HtmlTableRow();
    HtmlTableCell cl = new HtmlTableCell();
    cl.InnerText = "Test";
    rw.Cells.Add(cl);
    TestTable.Rows.Add(rw);
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    protected System.Web.UI.HtmlControls.HtmlTable TestTable;
    }



    Like I said, very simple.

    However, when I run the code, nothing shows up - I would expect to see a
    single cell with 'Test' displayed in it.

    Can anyone explain what's going on here?

    TIA,
     
    Epetruk, Feb 11, 2005
    #1
    1. Advertisements

  2. You're creating a new HTML Table, that could be causing the problem. The
    Table already exists that's part of the form so just create a local
    protected variable for the page class like so:

    protected HtmlTable TestTable;

    If this is being generated by VS.Net, it should already be creating a
    protected variable for you. Then all you do is add the row to the table just
    like you already do and it should be good. When you set the TestTable = new
    HtmlTable() in code behind you're now pointing TestTable to a different
    object, meaning it isn't referencing the one in the form anymore.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage
     
    Mark Fitzpatrick, Feb 11, 2005
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.