Friday, 15 February 2013

18. 9. 2. Use the SqlHierarchicalDataSource control when working with the TreeView control

 
. Use the SqlHierarchicalDataSource control when working with the TreeView control
<%@ Page Language="C#" %>
<%@ Register TagPrefix="custom" Namespace="MyNamespace" %>
html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
    Show TreeView

<body>
    <form id="form1" runat="server">
    


    
        id="TreeView1"
        DataSourceID="srcDiscuss"
        OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"
        ImageSet="News"
        Runat="server">
        
            
                TextField="Subject"
                ValueField="MessageId" />
        
    

    
        id="srcDiscuss"
        ConnectionString='<%$ ConnectionStrings:Discuss %>'
        DataKeyName="MessageId"
        DataParentKeyName="ParentId"
        SelectCommand="SELECT MessageId,ParentId,Subject FROM Discuss"
        Runat="server" />

    


    You selected message number:
    
        id="lblSelected"
        Runat="server" />

    
    </form>
</body>
</html>

Generating Ms Word document in ASP.NET and C#



 

Lets start with creating a simple .aspx page where we will give a text box to enter user's Name and a button to click on. After clicking on the button Ms Word document will be generated. 

.aspx page

The code will be 


   

        Write your name:

       

       

       

   

   


When button will be clicked, GenerateMsWordDoc event will fire. 

Now lets write the code behind code. What we are going to do here is we are generating a dynamic html content based on the textbox value. Also we are writing one table with 2 cell and at last we are adding some text. 

.cs file code



protected void GenerateMsWordDoc(object sender, EventArgs e)

    {

        string strBody = "" +

            "" +

                "
Your name is: " + txtName.Text + "
" +

                "
1st Cell body data2nd cell body data
" +

                "Ms Word document generated successfully." +

            "
" +

            "
";

        string fileName = "MsWordSample.doc";

        // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs

        Response.AppendHeader("Content-Type", "application/msword");

        Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);

        Response.Write(strBody);

    }

Saturday, 2 February 2013

Routing IN ASP.NET(3.5)

URL Routing in ASP.Net 3.5(IIS7)

Routing?
Service Pack 1 for the Microsoft .NET Framework 3.5 introduced a routing engine to the ASP.NET runtime. The routing engine can decouple the URL in an incoming HTTP request from the physical Web Form that responds to the request, allowing you to build friendly URLs for your Web applications.
Suppose you have an ASP.NET Web Form named CSharp.aspx, and this form is inside a folder named ‘Tutorial’. The classic approach to viewing a tutorial with this Web Form is to build a URL pointing to the physical location of the form and encode some data into the query string to tell the Web Form which author to display. The end of such a URL might look like the following: /Tutorial/CSharp.aspx?AuthorID=5, where the number 5 represents a primary key value in a database table full of authors.


Configuring ASP.NET for Routing
To configure an ASP.NET Web site or Web application for routing, you first need to add a reference to the System.Web.Routing assembly. The SP1 installation for the .NET Framework 3.5 will install this assembly into the global assembly cache, and you can find the assembly inside the standard "Add Reference" dialog box.
To run a Web site with routing in IIS 7.0, you need two entries in web.config. The first entry is the URL routing module configuration, which is found in the section of . You also need an entry to handle requests for UrlRouting.axd in the section of .
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">                            
                                                             
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

      

</modules>

<handlers>                           
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

      


</handlers>
</system.webServer>
Once you've configured the URL routing module into the pipeline, it will wire itself to the PostResolveRequestCache and the PostMapRequestHandler events.
Configuring Routes
Now to configure route, the first thing is to register the route at application startup. To register the routes at application startup write the following code in your Global.asax file.
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
RouteTable.Routes.Add("Tutorial",new("Tutorial/{subject}/{AuthorID}", new RouteHandler(string.Format("~/CSharp.aspx"))));
}
Here {subject}, {AuthorID} is name of Query Srting, through which we will get access to the value passed through query string.
Now, we need a RouteHandler .
public class RouteHandler : IRouteHandler
{
    string _virtualPath;
    public RouteHandler(string virtualPath)
    {
         _virtualPath = virtualPath;
    }
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {       
        foreach (var value in requestContext.RouteData.Values)
        {
            requestContext.HttpContext.Items[value.Key] = value.Value;
        }
        return (Page)BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page));
    }
}
Now, Routing is configured but what about the query string. How we can get access to values passed through query string. To get the data passed through query string we use context.Items[“ID”] instead of Request.QueryString[“ID”].
HttpContext context = HttpContext.Current;
String id = context.Items["AuthorID"].ToString();