Click here to Skip to main content
14,766,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am trying to print
a Dynamically Created HTML Table. On button Click
also the button is created Dynamically .
But am Not getting any Idea.
Can Anyone guide me.
Updated 10-Aug-12 21:37pm
Volynsky Alex 11-Aug-12 4:04am
Good question.

The following code dynamically creates a table with five rows and four cells per
row, sets their colors and text, and shows all this on the page. The interesting detail is that no control tags are declared in the .aspx file. Instead, everything is generated programmatically.
protected void Page_Load(object sender, System.EventArgs e)
        // Create a new HtmlTable object.
        HtmlTable table1 = new HtmlTable();

        // Set the table's formatting-related properties.
        table1.Border = 1;
        table1.CellPadding = 3;
        table1.CellSpacing = 3;
        table1.BorderColor = "red";

        // Start adding content to the table.
        HtmlTableRow row;
        HtmlTableCell cell;
        for (int i=1; i<=5; i++)
                // Create a new row and set its background color.
                row = new HtmlTableRow();
                row.BgColor = (i%2==0 ? "lightyellow" : "lightcyan");
                for (int j=1; j<=4; j++)
                        // Create a cell and set its text.
                        cell = new HtmlTableCell();
                        cell.InnerHtml = "Row: " + i.ToString()+ "<br />Cell: " + j.ToString();
                        // Add the cell to the current row.

                // Add the row to the table.

        // Add the table to the page.

you can insert it in the your Button_Click...
Volynsky Alex 11-Aug-12 4:06am
also you can read it:
Abdul Quader Mamun 11-Aug-12 4:43am
good work!
Karwa_Vivek 11-Aug-12 5:24am
My query Doesn't Deal About the Solution you posted.
Anyways thanks.
Volynsky Alex 11-Aug-12 7:08am
ok. maybe it will help you:
Volynsky Alex 11-Aug-12 7:10am
or it:

function CallPrint(strid) {
            var prtContent = document.getElementById(strid);
            var WinPrint ='', '', 'letf=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');

and in dynamic table
Karwa_Vivek 11-Aug-12 2:33am
not working for me
Ganesh Nikam 11-Aug-12 2:40am
strid is nothing but id of that control .
and add onClientClick function
Karwa_Vivek 11-Aug-12 2:47am
Have tried your Solution "onClientClick " event is not working with
the html button created dynamically. i tried using onclick event .
but am getting a blank page. i also set the Id of the control / div to print.
Ganesh Nikam 11-Aug-12 3:08am
this is working solution i my project .
Karwa_Vivek 11-Aug-12 3:13am
Did you have created the HTML table Dynamically in your Project.
I have created the html table using String Builder .and appended the HTML tags with in . Am sure you are not using this code on Dynamically created Table in your project.this Code is working Fine when am using on a Static page.Anyways thanks for your attention
Finally I have added a asp button in a Div
placed the Div at required place
means just on the Dynamically created table(html).

and on button click
<script type="text/javascript">
     function PrintContent() {
         var DocumentContainer = document.getElementById('divPrint');
         var WindowObject ='', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
         var strHtml = "<html>\n<head>\n<link rel="\"stylesheet\"" type="\"text/css\"" href="\"print.css\"">\n</head><body>\n<div style="\"testStyle" documentcontainer.innerhtml="" mode="hold" />         WindowObject.document.writeln(strHtml);
         // WindowObject.print();
         // WindowObject.close();


called this javasript Function
<asp:imagebutton id="ImageButton1" runat="server" xmlns:asp="#unknown">
                ImageUrl="~/Images/print2.gif" OnClientClick="javascript:PrintContent('divPrint');"  />


divPrint is the Id of the Table/div (dynamic)
and Got it working.Interestingly now my page contains only one asp button.That is a Print Button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900