?
Solved

vb.net How can I add rows to an existing html table in code behind..

Posted on 2011-02-14
6
Medium Priority
?
1,350 Views
Last Modified: 2012-06-27
Hi experts..

I have a template that I load that is comprised of a simple html table structure..

structure is similar to below..

<table id="MainForm">
   <tr>
      <th>
         Column1
      </th>
      <th>
         Column2
      </th>
      <th>
         Column3
      </th>
   </tr>
   .
   .
   .
</table>

In the dotted section I want to prgramatically insert <tr> and <td> elements..

Reason is that I am building an email template on the fly that will contain a variable number of rows retrieved from a database..

The table structure is fairly basic and static (column headings etc..), but the nbumber of rows returns will be varialble..

Can someone help in pointing me to the right way to achieve this please?

Thanks in advance..

D
   
0
Comment
Question by:jcm4101
6 Comments
 
LVL 6

Expert Comment

by:kswathi
ID: 34886151
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 34886152
set the runat=server property to table tag like this and id

<table id="MainForm" runat= "server">

then in code behind

 //Instantiate Row Object and Cell Object

       

        HtmlTableRow htRow = new HtmlTableRow();

        HtmlTableCell htCell = new HtmlTableCell();        

 

        //Add Text to cell and add Cell to Row

 

        htCell.InnerText = "This is Implementation of HTML Control";

        htRow.Cells.Add(htCell);

        MainForm.Rows.Add(htRow);

 

        //Add Second Row in a Table

       

        htRow = new HtmlTableRow();

        htCell = new HtmlTableCell();

 

        TextBox txttext = new TextBox();

        txttext.Visible = true;

        txttext.Text = "Inside Cell";

       

        htCell.Controls.Add(txttext);

        htRow.Cells.Add(htCell);

        MainForm.Rows.Add(htRow);



0
 

Assisted Solution

by:spooky07
spooky07 earned 500 total points
ID: 34886163
You have to create a  a Label Control or PlaceHolder Control in your HTML

string htmlTable = "<table><tr><td>" + yourData + </td></tr></table>;

YourLabel.Text = htmlTable;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Assisted Solution

by:soujanya_g
soujanya_g earned 500 total points
ID: 34886169

Hi
Just look this code
protected void Page_Load(object sender, EventArgs e)
{
 
        //Instantiate Row Object and Cell Object
       
        HtmlTableRow htRow = new HtmlTableRow();
        HtmlTableCell htCell = new HtmlTableCell();      
 
        //Set the sytle of a table
       
        table1.BgColor = "#FCDDFE";
        table1.Align = "left";
        table1.Border = 1;
        table1.Attributes["Style"] = "font-family:Verdana;font-size:11px;font-style:italic;";
 
        //Add Text to cell and add Cell to Row
 
        htCell.InnerText = "This is Implementation of HTML Control";
        htRow.Cells.Add(htCell);
        table1.Rows.Add(htRow);
 
        //Add Second Row in a Table
       
        htRow = new HtmlTableRow();
        htCell = new HtmlTableCell();
 
        TextBox txttext = new TextBox();
        txttext.Visible = true;
        txttext.Text = "Inside Cell";
       
        htCell.Controls.Add(txttext);
        htRow.Cells.Add(htCell);
        table1.Rows.Add(htRow);
 
}
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 34886313
One way this could be done is that you store the header only in the template like below

<table id="MainForm">
   <tr>
      <th>
         Column1
      </th>
      <th>
         Column2
      </th>
      <th>
         Column3
      </th>
   </tr>


Note the missing </table> tag. Now you can just append text to the template and then at the end, append the </table></body></html> nodes when finished.



If there is more than </table></body></html> at the end, you can store it in a separate file and append that file at the end.
0
 

Author Closing Comment

by:jcm4101
ID: 34939858
Hi all..
Thanx for the responses..
To be honest, all solutions presented would have worked, so I split the points equally..
I ended up using a solution that used placeholdes in a standard HTML form, then replacing these placeholders with valid HTML in code behind, that I built on the fly after pulling the data from the DB..(Just like PHP)
Once again, thanx for the solutions..All very helpful.

D
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question