How do you access Table cells from code behind?

Discussion in 'ASP .Net' started by Richard Dixson, May 18, 2004.

  1. I created a new C# web application. Basically all I am trying to do is
    create a table that consists of a few rows with two columns in each. And
    then to set those columns to text values from my code behind. However I am
    not able to do this at all, I am going about this wrong, I think and need
    guideance.

    If this was just straight HTML I would do this:

    <table>
    <tr><td>field 1</td><td>value 1 set by code behind</td></tr>
    <tr><td>field 2</td><td>value 2 set by code behind</td></tr>
    </table>

    I dragged a Table object from the Webform control box onto my page. Then I
    used the "..." in the properties to create some rows and columns.

    But what I cannot figure out is how to set the columns from my code behind
    page to text values I want displayed. I can't even figure out how to
    reference the columns. I could reference the Table object from code behind,
    but wasn't sure how to get at the rows or columns. Do I need to create text
    label objects inside the column objects? How am I supposed to access these
    columns via code behind?

    I searched around and all I found was indepth info on data grid table
    display. But this should be much simplier. I'm just trying to create a
    static table (do not need to create rows/columns on the fly) that I can set
    the columns to values at run time from code behind.

    Also I would like to add that I would like these labels inside the table
    cells that I set via the code behind to persist. Is that possible?

    For example then first time the page is run (via a GET) I want to display
    the table and set the column value labels from my code behind page.

    Then after the table I will have a short form that the visitor can fill how
    and then press the Submit button.

    When the submit button is pressed and the page is resubmitted to itself,
    this time via the POST, I would like ASP.NET to AUTOMATICALLY pick up the
    same labels inside my table that I set via the GET request. Will that
    happen automatically, or will the values fall back to their defaults?
    Remember for these values they are in a table column label and not in a web
    form.

    Can someone please advise? A tiny code sample would help best. Thanks!

    Richard
    Richard Dixson, May 18, 2004
    #1
    1. Advertising

  2. OK, figured it out. I had to declare the table cell variable in the code
    behind page. For example:
    protected System.Web.UI.WebControls.TableCell myCellVal;

    Definately non-obvious to those just getting started in webforms like
    myself. I'm really surprised that the designer does not automatically add
    those declarations for you...

    Richard
    Richard Dixson, May 18, 2004
    #2
    1. Advertising

  3. Hi Richard,

    You just need to track the rows in the tables and the cells in each row. For
    example, if your HTML looks like this:

    <asp:Table id="Table1" runat="server" BorderWidth="1px"
    BorderColor="Black">
    <asp:TableRow BorderWidth="1px" BorderColor="Black">
    <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    </asp:TableRow>
    <asp:TableRow BorderWidth="1px" BorderColor="Black">
    <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    </asp:TableRow>
    </asp:Table>

    You can provide the content in the code behind using this:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    'Put user code to initialize the page here
    Dim tblrow As TableRow
    Dim tblcell As TableCell
    tblrow = Table1.Rows(0)
    tblcell = tblrow.Cells(0)
    tblcell.Text = "field 1"
    tblrow = Table1.Rows(1)
    tblcell = tblrow.Cells(0)
    tblcell.Text = "field 2"
    tblrow = Table1.Rows(0)
    tblcell = tblrow.Cells(1)
    tblcell.Text = "value 1 set by code behind"
    tblrow = Table1.Rows(1)
    tblcell = tblrow.Cells(1)
    tblcell.Text = "value 2 set by code behind"
    End Sub

    Does this help?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto


    "Richard Dixson" <> wrote in message
    news:...
    >I created a new C# web application. Basically all I am trying to do is
    > create a table that consists of a few rows with two columns in each. And
    > then to set those columns to text values from my code behind. However I
    > am
    > not able to do this at all, I am going about this wrong, I think and need
    > guideance.
    >
    > If this was just straight HTML I would do this:
    >
    > <table>
    > <tr><td>field 1</td><td>value 1 set by code behind</td></tr>
    > <tr><td>field 2</td><td>value 2 set by code behind</td></tr>
    > </table>
    >
    > I dragged a Table object from the Webform control box onto my page. Then
    > I
    > used the "..." in the properties to create some rows and columns.
    >
    > But what I cannot figure out is how to set the columns from my code behind
    > page to text values I want displayed. I can't even figure out how to
    > reference the columns. I could reference the Table object from code
    > behind,
    > but wasn't sure how to get at the rows or columns. Do I need to create
    > text
    > label objects inside the column objects? How am I supposed to access
    > these
    > columns via code behind?
    >
    > I searched around and all I found was indepth info on data grid table
    > display. But this should be much simplier. I'm just trying to create a
    > static table (do not need to create rows/columns on the fly) that I can
    > set
    > the columns to values at run time from code behind.
    >
    > Also I would like to add that I would like these labels inside the table
    > cells that I set via the code behind to persist. Is that possible?
    >
    > For example then first time the page is run (via a GET) I want to display
    > the table and set the column value labels from my code behind page.
    >
    > Then after the table I will have a short form that the visitor can fill
    > how
    > and then press the Submit button.
    >
    > When the submit button is pressed and the page is resubmitted to itself,
    > this time via the POST, I would like ASP.NET to AUTOMATICALLY pick up the
    > same labels inside my table that I set via the GET request. Will that
    > happen automatically, or will the values fall back to their defaults?
    > Remember for these values they are in a table column label and not in a
    > web
    > form.
    >
    > Can someone please advise? A tiny code sample would help best. Thanks!
    >
    > Richard
    >
    >
    Ken Cox [Microsoft MVP], May 18, 2004
    #3
  4. It adds the table automatically. You manipulate the cells through code.


    "Richard Dixson" <> wrote in message
    news:...
    > OK, figured it out. I had to declare the table cell variable in the code
    > behind page. For example:
    > protected System.Web.UI.WebControls.TableCell myCellVal;
    >
    > Definately non-obvious to those just getting started in webforms like
    > myself. I'm really surprised that the designer does not automatically add
    > those declarations for you...
    >
    > Richard
    >
    >
    Ken Cox [Microsoft MVP], May 18, 2004
    #4
  5. > tblrow = Table1.Rows(0)

    Assuming I assign a unique ID to each row, like "ID=myrow1" Is there a way
    to dynamically access a row of the table by its ID.

    For example Table1.Rows(0) only allows you to access it by its index as an
    int. Doesn't look like you can do Table1.Rows("myrow1"). Is there
    something similar that will allow me to access a row dynamically via code
    behind given a particular ID I assigned to the row in the .aspx page?

    Richard

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:#NF$...
    > Hi Richard,
    >
    > You just need to track the rows in the tables and the cells in each row.

    For
    > example, if your HTML looks like this:
    >
    > <asp:Table id="Table1" runat="server" BorderWidth="1px"
    > BorderColor="Black">
    > <asp:TableRow BorderWidth="1px" BorderColor="Black">
    > <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    > <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow BorderWidth="1px" BorderColor="Black">
    > <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    > <asp:TableCell BorderWidth="1px" BorderColor="Black"></asp:TableCell>
    > </asp:TableRow>
    > </asp:Table>
    >
    > You can provide the content in the code behind using this:
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > 'Put user code to initialize the page here
    > Dim tblrow As TableRow
    > Dim tblcell As TableCell
    > tblrow = Table1.Rows(0)
    > tblcell = tblrow.Cells(0)
    > tblcell.Text = "field 1"
    > tblrow = Table1.Rows(1)
    > tblcell = tblrow.Cells(0)
    > tblcell.Text = "field 2"
    > tblrow = Table1.Rows(0)
    > tblcell = tblrow.Cells(1)
    > tblcell.Text = "value 1 set by code behind"
    > tblrow = Table1.Rows(1)
    > tblcell = tblrow.Cells(1)
    > tblcell.Text = "value 2 set by code behind"
    > End Sub
    >
    > Does this help?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    > Toronto
    >
    Richard Dixson, May 18, 2004
    #5
  6. Right. But what I don't get is why the code writter doesn't automatically
    add a declaration like:
    protected System.Web.UI.WebControls.TableCell myCellVal;

    If I put in a cell like:
    <asp:TableCell ID="myCellVal" BorderWidth="1px"
    BorderColor="Black"></asp:TableCell>

    It took me a few hours trying to figure out how I could directly access
    myCellVal from the code behind. Once I figured out I needed to add the
    declaration "protection System...." for it, it was simple from there. It
    declares the table object, so why not the row and cell objects you create
    for the table given the UI? No need to answer - I assume this is just how
    it works. But it seems like it would be more intuitive if it declared all
    the objects in the aspx and not just some or parents.

    Richard


    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:#...
    > It adds the table automatically. You manipulate the cells through code.
    >
    >
    > "Richard Dixson" <> wrote in message
    > news:...
    > > OK, figured it out. I had to declare the table cell variable in the

    code
    > > behind page. For example:
    > > protected System.Web.UI.WebControls.TableCell myCellVal;
    > >
    > > Definately non-obvious to those just getting started in webforms like
    > > myself. I'm really surprised that the designer does not automatically

    add
    > > those declarations for you...
    > >
    > > Richard
    > >
    > >

    >
    Richard Dixson, May 18, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    563
    Alphonse Giambrone
    Jun 28, 2003
  2. Replies:
    0
    Views:
    510
  3. bbxrider
    Replies:
    0
    Views:
    587
    bbxrider
    Jul 14, 2003
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    460
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Joel Finkel

    Cells[].Text or Cells[].Controls[0]

    Joel Finkel, Sep 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    293
    Joel Finkel
    Sep 1, 2003
Loading...

Share This Page