Friday, 19 October 2012

Site Map in ASP.NET 3.5 and 4.0


The SiteMap control available in ASP.NET 3.5 provides a site navigation view in a hirerchichal format. The data source for the SiteMap control is an XML file that has name and URL of the site navigation.
The XmlSiteMapProvider looks for a file named Web.sitemap in the root of the virtual directory. Like all site map providers , its task is to extract the site map data and create the corresponding SiteMap object. This SiteMap object is then made available to other control through the SiteMapDataSource.
Site maps are used in conjunction with the SiteMapDataSource, SiteMapPath, and other controls to render an effective navigation interface for users to navigate a Web site. A SiteMap object is a component of the ASP.NET site navigation infrastructure that provides access to read-only site map information for page and control developers using navigation and SiteMapDataSource controls. Other components of the ASP.NET site navigation infrastructure include the SiteMapProvider and XmlSiteMapProvider classes, the Web.sitemap file, and the various controls that work with the SiteMap class, such as the SiteMapPath control, to render content and menus.
The SiteMap has several functions:----
1.         It provides the root node of the site navigation hierarchy (there can be only one root node).
         It identifies which site map provider is the principal, or default, provider.
         It keeps track of all the provider objects that are used to create the SiteMap.
Now  Create an ASP.NET application using Visual Studio 200:-
Now add a Site Map file to the project by right clicking on the project name, select Add New Item and select Site Map from the templates. 
ct Site Map from the templates.
 
After adding a Site Map file, we need to add Site Map Nodes represeted by siteMapNode XML elements as shown below.
Here is my final thakurtest.sitemap looks like. The siteMapNode has a title and a url attribute that represent the title and URL of the link.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="" title="Software Industry"  description="This is IT Site">

    <siteMapNode title="Header"  description="IT Industry">

      <siteMapNode url="www.modelingcorner.com" title="Project Manager"  description="This is Manager DESC">

        <siteMapNode url="http://urllink1" title="Project Lead"  description="This section is for Project Lead" />

        <siteMapNode url="http://urllink2" title="Team Lead"  description="This section is for Team Lead" />

        <siteMapNode url="http://urllink3" title="Sr.Software Engineer"  description="This section is for Sr.Software Engineer" />

        <siteMapNode url="http://urllink4" title="Software Engineer"  description="This section is for Software Engineer" />

        <siteMapNode url="http://urllink5" title="Software Developer"  description="This section is for Software Developer" />

      </siteMapNode>

      <siteMapNode url="http://urllink6" title="NON PROGRAMMERS"  description="This is NON PROGRAMMERS"></siteMapNode>

      <siteMapNode url="http://urllink7" title="Testing Manager"  description="This is Testing Manager"></siteMapNode>

      <siteMapNode url="http://urllink8" title="DBA Manager"  description="This is DBA Manager section"></siteMapNode>

      <siteMapNode url="http://urllink9" title="Design Manager"  description="This is Design Manager section"></siteMapNode>

      <siteMapNode url="http://urllink10" title="Testing Engineer"  description="This is Testing Engineer section"></siteMapNode>

      <siteMapNode url="http://urllink11" title="Design Specialist"  description="This is Design Specialist section"></siteMapNode>

      <siteMapNode url="http://urllink12" title="DBA"  description="This is DBA section"></siteMapNode>

    </siteMapNode>

    <siteMapNode title="Footer" url="" description="This is Software Industry footer">

      <siteMapNode url="http://urllink13" title="Hosted By Thakur"  description="This is host website name"></siteMapNode>

      <siteMapNode url="http://urllink14" title="Home"  description="This is home page name"></siteMapNode>

      <siteMapNode url="http://urllink15" title="Contact Us"  description="This is contact us page"></siteMapNode>

      <siteMapNode url="http://urllink16" title="About Us"  description="This is about us page"></siteMapNode>

      <siteMapNode url="http://urllink17" title="Help"  description="This is help page"></siteMapNode>

      <siteMapNode url="http://urllink18" title="Search"  description="This is search page"></siteMapNode>

    </siteMapNode>

  </siteMapNode>

  <!-- -->

</siteMap>

 
Now let's add a SiteMapDataSource control to the ASP.NET page. This is my page page looks like.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Site Map</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
  <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"
            BackColor="#F7F6F3" DynamicHorizontalOffset="2" Font-Names="Verdana"
            Font-Size="14px" ForeColor="#7C6F57" StaticSubMenuIndent="14px">
            <StaticSelectedStyle BackColor="#5D7B9D" />
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicHoverStyle BackColor="#7C6F57" ForeColor="White" />
            <DynamicMenuStyle BackColor="#F7F6F3" />
            <DynamicSelectedStyle BackColor="#5D7B9D" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticHoverStyle BackColor="#7C6F57" ForeColor="White" />
        </asp:Menu>
    </form>
</body>
</html>

Now if you run and look at the output, you will see output looks like following where you can click on the root node and it will display next level nodes and so on.





 

Thursday, 4 October 2012

how to export gridview data to PDF using asp.net c#

In code behind add these references:-

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
After that write the following code in code behind:-
public override void VerifyRenderingInServerForm(Control control)
{
// Verifies that the control is rendered
}
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "25%");
gvdetails.HeaderRow.Style.Add("font-size", "11px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "9px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}