Print all steps of asp:Wizard control

972 Views Asked by At

I have a asp:Wizard control in my Web Application.I need to be able to print at any step within the wizard , and print all steps up to that step not just the current step.

I've added a print button to every step page , and tried to call the javascript:window.Print(), but only the current step gets printed.

How do i get all the steps to print in 1 page?

i'd like to try and get this working in javascript first before i go down the PDF route . I've tried doing somehting like this :

protected void Page_Load(object sender, EventArgs e) 
    { 
        StringWriter sw = new StringWriter(); 
        HtmlTextWriter tw = new HtmlTextWriter(sw); 
        this.WizardStep2.RenderControl(tw); 
        string wizardHtmlContent = sw.ToString().Replace("\r\n", ""); 

        string printScript = @"function printDiv(printpage) 
                                { 
                                var headstr = '<html><head><title></title></head><body>'; 
                                var footstr = '</body>'; 
                                var newstr = printpage; 
                                var oldstr = document.body.innerHTML; 
                                document.body.innerHTML = headstr+newstr+footstr; 
                                window.print();  
                                document.body.innerHTML = oldstr; 
                                return false; 
                                }"; 


        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "PrentDiv", printScript, true); 
        this.Button1.Attributes.Add("onclick", "printDiv('" + wizardHtmlContent + "');"); 

    }

and for the aspx:

<form id="form1" runat="server"> 
<div> 
     <asp:Wizard ID="Wizard1" runat="server"> 
        <WizardSteps> 
            <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1"> 
                step1 
            </asp:WizardStep> 
            <asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2"> 
                step2 
            </asp:WizardStep> 
        </WizardSteps> 
    </asp:Wizard> 
    <asp:Button ID="Button1" runat="server" Text="Button" /> 
</div> 
</form>

But i'm getting a missing runat=server error on line 3 , when i attempt to render the wizard control , so i think i may need to create a new window, then output the string before i print it , but cant seem to get that working ...Anyone any ideas ?

2

There are 2 best solutions below

1
On BEST ANSWER

i have found a solution for my problem , i didnt manage to accomplish it client side , but ive managed to solve it server side which is better than going down the PDF route which i didnt want to do. I found a great article here : Printing in ASP.NET

which i ammended to print all steps of my wizard control in one go. thanks for all your help.

1
On

The javascript print method you're already using will work if you put the wizard steps in to a single page so they all render ...

the other way I guess is to simply browse to each step and hit your print button.

the way I would do it is use something like pdfsharp and give it the markup generates by each step and tell it to create a pdf page for each steps worth of markup ... from there the user has a pdf doc which they can simply view save or print using their usual pdf viewer.

The problem is that the javascript method is using a dom based api call to ask the browser to print the page which of course ultimately means you can only print the wizard step you're currently looking at ... using the pdf method means the user can preview the expected print out before printing and you have more control over what's printed.

It does require a bit more code though ...

pdfsharp can be found here: http://www.pdfsharp.net/Downloads.ashx

As you can see its free and open source.