How do you access Table cells from code behind?

R

Richard Dixson

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
 
R

Richard Dixson

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
 
K

Ken Cox [Microsoft MVP]

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
 
K

Ken Cox [Microsoft MVP]

It adds the table automatically. You manipulate the cells through code.
 
R

Richard Dixson

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
 
R

Richard Dixson

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top