473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you access Table cells from code behind?

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
5 36878
OK, figured it out. I had to declare the table cell variable in the code
behind page. For example:
protected System.Web.UI.W ebControls.Tabl eCell 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
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="1p x"
BorderColor="Bl ack">
<asp:TableRow BorderWidth="1p x" BorderColor="Bl ack">
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></asp:TableCell>
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></asp:TableCell>
</asp:TableRow>
<asp:TableRow BorderWidth="1p x" BorderColor="Bl ack">
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></asp:TableCell>
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></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.EventArg s) _
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******** ************@co mcast.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
It adds the table automatically. You manipulate the cells through code.
"Richard Dixson" <re***@here.com > wrote in message
news:JL******** ************@co mcast.com...
OK, figured it out. I had to declare the table cell variable in the code
behind page. For example:
protected System.Web.UI.W ebControls.Tabl eCell 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
> 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("my row1"). 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******** ******@TK2MSFTN GP09.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="1p x"
BorderColor="Bl ack">
<asp:TableRow BorderWidth="1p x" BorderColor="Bl ack">
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></asp:TableCell>
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></asp:TableCell>
</asp:TableRow>
<asp:TableRow BorderWidth="1p x" BorderColor="Bl ack">
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></asp:TableCell>
<asp:TableCel l BorderWidth="1p x" BorderColor="Bl ack"></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.EventArg s) _
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
Right. But what I don't get is why the code writter doesn't automatically
add a declaration like:
protected System.Web.UI.W ebControls.Tabl eCell myCellVal;

If I put in a cell like:
<asp:TableCel l ID="myCellVal" BorderWidth="1p x"
BorderColor="Bl ack"></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******** ******@TK2MSFTN GP10.phx.gbl...
It adds the table automatically. You manipulate the cells through code.
"Richard Dixson" <re***@here.com > wrote in message
news:JL******** ************@co mcast.com...
OK, figured it out. I had to declare the table cell variable in the code behind page. For example:
protected System.Web.UI.W ebControls.Tabl eCell 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1641
by: Greg Linwood | last post by:
How does one access HTML elements from code-behind? I've got a mix of asp:Labels & <font> elements - I'd like to continue using <font> elements, but be able to access them from code-behind to set properties such as InnerHTML, InnerText etc.. Is this possible? Regards, Greg Linwood
3
2551
by: Ryan Taylor | last post by:
Hi. I need to be able to access a public code behind variable within my ASPX page. The reason is that I have a user control "Header" which defines a table layout, and is included in all my pages, at various levels in the website heirarchy. The table requires a background image. My images folders is at the root of the website, effectively "~/images/". I need the background image to show whether I at a page in the root of my website or in...
3
2206
by: Pierre | last post by:
Hello, I always get a nothing object when retrieving a table tag in the aspx page. I wonder why this piece of code doesn't work : the mytable object is still nothing. Here is the code : In the aspx page : <body>
2
11160
by: travhale | last post by:
in a new project using .net 2005, c#. getting err message "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." source RDBMS is oracle 8i. I add a new dataset to the project and drag a datatable from server explorer onto the dataset design surface. In the configuration wizard, under advanced options the "refresh data table" is disabled ie I am not able to check it (note if the source db is sql...
3
573
by: vivekian | last post by:
Hi , I have a table which needs to be populated at runtime. Is there a way to do this without using a server side control like repeater or datagrid ? How can an HTML control be accessed in code behind ? Thanks in advance ..
3
3563
by: David A. Osborn | last post by:
I have a web project inVS2005 that I copied some existing pages from a website that I previously created. To clarify the website was originally created in Visual Web Developer Express and not I have created a web project (not a website) in VS2005 and add a few of the pages to this web project One of the pages I am currently working with is not allowing me to reference the controls from the design page in the code behind. Do I need to make...
1
2113
by: BlueJumper.com | last post by:
Does anyone know how to access a users Profile from the code behind. When using code in-front I can simple Type Profile.Blah but this isn't available in Code Behind Cheers Fran
3
2926
by: markpringle | last post by:
This is my sqldatasource this is on the page: <asp:sqldatasource runat="server" ID="DS_CheckUserName" ConnectionString="<%$ ConnectionStrings:SQL2005_355184_ConnectionString %>" SelectCommand="SELECT FROM WHERE ( = @UserName)"> <SelectParameters> <asp:FormParameter FormField="UserName" Name="UserName" Type="String" /> </SelectParameters> How do I access the selected UserName in in the Code Behind that...
40
7746
by: Rebecca McCallan | last post by:
I have some code which saves emails into a database, creates new folders for new senders etc etc but what I now want to do is when a new email comes in, i want any attachments to be saved into a table (by saving the location and then having the ability to go back in and open it from that location)... All I really need is the code which will show the filepath... does anybody know how to do this? Any replies appreciated.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.