Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > dynamically adding table rows and cells...

Reply
Thread Tools

dynamically adding table rows and cells...

 
 
Rob Meade
Guest
Posts: n/a
 
      11-24-2003
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




 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      11-24-2003
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



"Rob Meade" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
>



 
Reply With Quote
 
 
 
 
Rob Meade
Guest
Posts: n/a
 
      11-24-2003
"Rob Meade" wrote ...

[..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


 
Reply With Quote
 
Danny Bloodworth
Guest
Posts: n/a
 
      11-24-2003
> 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
 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      11-25-2003
"Danny Bloodworth" wrote...

[..snip..]

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

Regards

Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving dynamically created table rows up and down in an HTML table T.G. Javascript 2 10-14-2008 12:56 PM
Dynamically adding rows to asp.net table control using javascript Komalid ASP .Net 0 12-21-2007 09:27 PM
Adding rows to html table dynamically Valli ASP .Net 3 09-21-2007 01:18 PM
Dynamically adding rows to table web server control Geary ASP .Net 2 07-26-2007 01:03 PM
Adding Rows Dynamically into the Table Web Control but only one row is getting inserted lucky ASP .Net 0 01-12-2005 10:45 AM



Advertisments