Solved

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

Posted on 2011-02-14
6
990 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Json and asp.net mvc 2 44
Allow space in this pattern 2 47
SQL Server Data Tools for Visual Studio 2015 - backward compatibility. 2 84
Help with AsEnumerable(), LINQ 4 22
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

25 Experts available now in Live!

Get 1:1 Help Now