Click here to Skip to main content
15,886,798 members
Articles / Web Development / HTML

Changing Page Style for Printing

Rate me:
Please Sign up or sign in to vote.
3.19/5 (9 votes)
17 Aug 20062 min read 46.4K   787   36  
Using a stylesheet to change the page style for printing.
using System;
using System.Xml;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class XmlDataMain : Page
{
	public DataGrid XmlDataGrid;
	
	// using ViewState for SortField property
	// For more information about using ViewState bags read the following reference
	// Reference:- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp11222001.asp
    string SortField {

        get {
            object o = ViewState["SortField"];
            if (o == null) {
                return String.Empty;
            }
            return (string)o;
        }

        set {
            if (value == SortField) {
                //if ascending change to descending or vice versa.
                SortAscending = !SortAscending;
            }
            ViewState["SortField"] = value;
        }
    }

    // using ViewState for SortAscending property
    bool SortAscending {

        get {
            object o = ViewState["SortAscending"];
            if (o == null) {
                return true;
            }
            return (bool)o;
        }

        set {
            ViewState["SortAscending"] = value;
        }
    }
    
	protected void Page_Load(Object src, EventArgs e)
	{
		if(!IsPostBack)
		{
			BindGrid();
		}
	}
	
	protected void BindGrid()
	{
		string dataUrl = "books.xml";
		string schemaUrl = "books.xsd";
		
		DataSet ds = new DataSet();
		ds.ReadXmlSchema(Server.MapPath(schemaUrl));
		ds.ReadXml(Server.MapPath(dataUrl));
		
		DataView dv = new DataView(ds.Tables[0]);
		DataTable dt = dv.Table;
		
		//Custom names for the columns
		dt.Columns[0].ColumnName = "Item Description";
		dt.Columns[1].ColumnName = "Item Id";
		dt.Columns[2].ColumnName = "Amount";
		
		//Sorting criteria
		dv.Sort = SortField;
        if (!SortAscending)
        {
            dv.Sort += " DESC";	//append "DESC" to the sort field name in order to sort descending
        }
        
		XmlDataGrid.DataSource = dv;
		XmlDataGrid.DataBind();
	}
	
	protected void SortGrid(Object src, DataGridSortCommandEventArgs e)
	{
		XmlDataGrid.CurrentPageIndex = 0;
   		SortField = e.SortExpression;	//get the requested sorting field name and set it to SortField
   		BindGrid();
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am Roopesh Rajendran, I have 3 + years expirience in asp.net C#. Currently i am working in U S Technology Techopark.i am an ative member in Code project,.net spider,Expert Exchange....
i have MCP's in C# web,XML Web service, % brain bech certifications...published lot of articles in Code project..


Comments and Discussions