Wednesday, September 29, 2010

How to save details and print a page

Demo:
Useful links:
  1. Opening a window after postback
  2. Java script print function
  3. Apply diffrnet styles whlie printing
  4. Print fit to page and backgrounds
Example:
<%@ Page Language="C#" %>
<html>
<head runat="server">
    <script runat="server">
        private string script = @"
         window.open('{0}','',
                'scrollbars=no,menubar=no, height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        ";
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string m = this.Request.QueryString.Get("mode");
            if (!string.IsNullOrEmpty(m))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.GetType().Name,
                                                                        "window.print();"true);
                this.btnSaveAndPrint.Visible = false;
            }
            else this.btnSaveAndPrint.Visible = true;
        }
        protected void SaveAndPrint(object sender, EventArgs e)
        {
            string page = this.Request.Url.ToString();
            if (page.Contains("?")) page = page + "&mode=print";
            else page = page + "?mode=Print";
            ///
            /// Save your data...
            ///
            Page.ClientScript.RegisterClientScriptBlock(
                this.GetType(),
                this.GetType().Name,
                string.Format(this.script, page),
                true);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h1>
        Thank you for purchaing our products</h1>
    <hr />
    <asp:TextBox runat="server" ID="txtName" Text="Name" /><br />
    <asp:TextBox runat="server" ID="txtAddress" Text="Address" /><br />
    <asp:TextBox runat="server" ID="txtEmail" Text="mail@email.com" /><br />
    <asp:TextBox runat="server" ID="txtPhone" Text="0123456789" /><br />
    <hr />
    <p>
        I started this site in 2005 to write down simple but important code snippets which
        I may forget easily. Most of the posts/articles published in this site are not rock
        complicated but they are simple answers to questions, which people may find hard
        to remember How To Do them. Today in 2010, almost all posts/articles are common
        answers to the questions raised in Asp.net Forms. All of these posts/articles been
        tested against certain criteria which suits to subject of the post. But it is essential
        you to understand the domain of the post and apply them in appropriate context.
        Most of the posts have being through couple of reviews after feedback from asp.net
        community users. However I DO NOT provide any warranty for these code snippets,
        which may work in your application context. Use these examples at your own risk.
        However if you find obvious syntax error or typo, please feel free to let me know
        by commenting them, so that I can improve the post for other asp.net community users
        of this site. Please remember you are NOT ALLOWED to COPY entire articles/posts
        and publish them your context without my knowledge and permission.
    </p>
    <asp:Button runat="server" ID="btnSaveAndPrint" Text="Save and Print" OnClick="SaveAndPrint" />
    </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...