dynamically adding table rows and cells...

R

Rob Meade

Hi all,

I'm desperately trying to get this to work and I just cant do it - new to
ASP.net - probably the reason!

I'm using visual studio for this - I have this in the html page :

<asp:Table id="Table1" runat="server"></asp:Table>

there is other code too but I believe this to be the relevant part...

In my button on click event I am trying to do this :

Dim tRow As New TableRow()
Table1.Rows.Add(tRow)

which I have copied EXACTLY from the visual studio help and MS site (both
examples the same etc)..

2 problems..

1: The brackets get removed from the first line
2: the tRow within the brackets gets underlined and has the following error
:

Value of type 'System.Web.UI.WebControls.TableRow' cannot be converted to
'System.Web.UI.HtmlControls.HtmlTableRow'.

I have no idea what the problems is or how to fix it - however I do suspect
the item 2 is a direct result of the missing brackets in item 1 - and that
these are probably missing because I've not done some form of 'IMPORT' at
the top of the page???

But to be honest I have no real clue....

If someone could suggest a reason / way forward I would be most grateful.

Regards

Rob
 
M

Mark

Hey Rob,

Try one of the following two:

1. (In a C# code behind for the button click event)

System.Web.UI.WebControls.TableRow tr = new
System.Web.UI.WebControls.TableRow();
System.Web.UI.WebControls.TableCell tc = new
System.Web.UI.WebControls.TableCell();
tc.Text = "blah";
tr.Cells.Add(tc);
myTable.Rows.Add(tr); //This assumes you have a asp:table server control
with an id="myTable"

(In the .aspx file)

<asp:Table ID="neat" Runat="server">
<asp:TableRow>
<asp:TableCell>
slick
</asp:TableCell>
</asp:TableRow>
</asp:Table>

2.
Dim tRow As New TableRow()
Table1.Rows.Add(tRow)

Change this to use explicit declarations like I did above. It appears that
there are two different libraries with two different methods of creating
TableRows, and you HAVE to be consistent with the table type you've created.
Based on your error message, it appears that there are:

a) System.Web.UI.WebControls.TableRow
b) System.Web.UI.HtmlControls.HtmlTableRow

Hope you find this moderately useful. Good luck!

Mark
www.dovetaildatabases.com
 
R

Rob Meade

...

[..snip..]

Found part of the problem by the looks of it...

Having added a couple of tables previously, the Protected with crap that
gets generated automatically by VS was still there, I had then removed the
table which was wrong and added a new one, gave it the same name and it used
the first lots of protected with stuff....as soon as i changed that - no
errors or scribbly lines....

Anyone know why VS doesn't remove these lines of code that it adds on your
behalf?

Regards

Rob
 
D

Danny Bloodworth

Value of type 'System.Web.UI.WebControls.TableRow' cannot be converted to
'System.Web.UI.HtmlControls.HtmlTableRow'.

Rob,
The error you are getting is because you are declaring your table row
as an HTML control then trying to add it to a WebControl.

I personally do not use the web control for tables. I simply add the
runat="server" tag to a regular HTML Table decalration.
Like this...
<Table id="Table1" runat="server"></Table>

Then, in your code behind page, make sure that you add the following

Imports System.Web.UI.HtmlControls
(above the class declaration)

and

Protected WithEvents Table1 As HtmlTable
(above the page_load sub but below the class declaration.
The key being that this is an HTMLTable not a WebControl)


Then in your code that dynamically creates the table row and cell,

Dim tRow as TableRow (Remember this is a
System.Web.UI.HtmlControls.TableRow)
Dim tCell as TableCell (System.Web.UI.HtmlControls.TableCell)

tCell.Attributes.Add("nowrap",True) 'Add any attributes you may
need
tCell.InnerHtml = "<B>Hello World</B>" 'Add HTML to the Cell

tRow.Cells.Add(tCell) 'Add the cell to the row
Table1.Rows.Add(tRow) 'Add the row to the table

Hope this helps.

Danny
 
R

Rob Meade

...

[..snip..]

Many thanks for a very informative reply Danny, your help is appreciated.

Regards

Rob
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top