Set width of <Table> from code-behind

I have a table
    <table id="myTBL">
        <tr><td>My Text</td></tr>

From code behind, in the PreRender event, how do I set the table's width?

Meaning, I want to "EFFECT"
this result ...
    <table id="myTBL" width="200px">
or this result ...
    <table id="myTBL" width="400px">

from page.prerender in code behind

Who is Participating?
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
You'll need to add a "runat" attribute to the table in order to make it a server control. You will then be able to modify its attributes from code-behind:

   <table id="myTBL" runat="server" width="200px">

  // code-behind
  myTBL.Attributes["width"] = "400px";

Open in new window

Carl TawnSystems and Integration DeveloperCommented:
Oops, didn't see the VB tag.

Code-behind for VB:

    myTBL.Attributes("width") = "400px"

Open in new window

Shaun KlineLead Software EngineerCommented:
An option would be to add the runat="server" attribute to your table.
This would allow you to access the table from the code-behind.
volkingAuthor Commented:
Thanks ... was too easy ... of course my mind went to all sorts of CTYPE(), FindControl(), etc, instead of the SIMPLE solution. Adding runat="server" worked great.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.