Solved

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

Posted on 2011-02-14
6
976 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 125 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 125 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Assisted Solution

by:soujanya_g
soujanya_g earned 125 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 125 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now