By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

How do you access Table cells from code behind?

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 18 '05 #2

P: n/a
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" <re***@here.com> wrote in message
news:Lc********************@comcast.com...
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


Nov 18 '05 #3

P: n/a
It adds the table automatically. You manipulate the cells through code.
"Richard Dixson" <re***@here.com> wrote in message
news:JL********************@comcast.com...
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


Nov 18 '05 #4

P: n/a
> 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]" <BA************@sympatico.ca> wrote in message
news:#N**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #5

P: n/a
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]" <BA************@sympatico.ca> wrote in message
news:#k**************@TK2MSFTNGP10.phx.gbl...
It adds the table automatically. You manipulate the cells through code.
"Richard Dixson" <re***@here.com> wrote in message
news:JL********************@comcast.com...
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

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.