3

I have used a Multiview control in my code where i display 5 views on my page. Currently, I am trying to implement "Print" functionality" using CSS and what I want is to display all the 5 views together so it is easy to print.

So, is it possible to display all the views in a multiview at once. something like show all views or display all views.

I can work around my print functionality if I can display all the views at once. Please help me.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

1 Answers1

2

I believe that the MultiView control doesn't support this feature by default. What you can do is to implement own MultiView control inherited from the existing one and add to it desired functionality:

[DefaultProperty("Text")]
[ToolboxData("<{0}:MyMultiView runat=server></{0}:MyMultiView>")]
public class MyMultiView : MultiView
{
    [Category("Behavoir")]
    [DefaultValue(false)]
    public bool RenderAllAtOnce
    {
        get
        {
            return (bool)(ViewState["RenderAllAtOnce"]?? false);
        }

        set
        {
            ViewState["RenderAllAtOnce"] = value;
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (!RenderAllAtOnce)
        {
            base.Render(writer);
        }
        else
        {
            foreach (View view in this.Views)
            {
                this.SetActiveView(view);
                view.RenderControl(writer);
            }
        }
    }
}

Then you can use that control instead of the default one and set RenderAllAtOnce property to true on some Print button click.

Also you can don't involving in new control creation and just render all views to string and pass it to some literal on a page:

.print
{
     display: none;
}

@media print
{
     .noPrint
     {
          display: none;
     }

     .print
     {
          display: block !important;
     }
}

<div class="noPrint">
     <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0" >
          <asp:View ID="View1" runat="server">
               AAAA
          </asp:View>
          <asp:View ID="View2" runat="server">
               BBBB
          </asp:View>
          <asp:View ID="View3" runat="server">
               CCCC
          </asp:View>
     </asp:MultiView>
     <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
</div>
<div class="print">
     <asp:Literal runat="server" ID="Literal1" Mode="PassThrough" />
</div>

protected void btnPrint_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();
    using (var writer = new StringWriter(sb))
    using (var htmlWriter = new HtmlTextWriter(writer))
    {
        foreach (View view in MultiView1.Views)
        {
            view.RenderControl(htmlWriter);
        }
    }

    Literal1.Text = sb.ToString();
    ClientScript.RegisterStartupScript(this.GetType(), "print", "window.print();", true);
}
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68