Friday, 29 June 2012

Url Routing in Asp.net 3.5


                                            Url Routing in Asp.net 3.5

Note:
means url looks like this if i press pravat sabat then
url would be like this http://localhost:1450/url_rout/User/hhhhh
means not showing pagename.

html code like this:-> take hello.aspx page and RegisterUser.aspx page and do the following code there

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegisterUser.aspx.cs" Inherits="RegisterUser" %>

<!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>Add New User</title>
    <style type="text/css">
        .style1 {
            width: 80%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <div align="left" class="style1">
            <asp:GridView ID="GridViewUserList" runat="server" AutoGenerateColumns="False"
            DataSourceID="LinqDataSource1" Width="400px">
                <Columns>
                    <asp:TemplateField HeaderText="Registered Users">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("UniqueName", "~/User/{0}") %>'
                                Text='<%# Eval("Name") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Unique Name">
                        <ItemTemplate>
                            <asp:Label ID="HyperLink1" runat="server" Text='<%# Eval("UniqueName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext"
                Select="new (UniqueName, Id, Name)" TableName="Users">
            </asp:LinqDataSource>
        </div>
    </div>
    <div>&nbsp;</div>
    <div align="center">
        <div align="left" style="font-weight:bold;">Add New User</div>
    </div>
    <div align="center">
   
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Full Name</td>
                <td>
                    <asp:TextBox ID="TextBoxFullName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Summary</td>
                <td>
                    <asp:TextBox ID="TextBoxSummary" runat="server" Height="50px"
                        TextMode="MultiLine" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Unique Name</td>
                <td>
                    <asp:TextBox ID="TextBoxUniqueName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="LabelMessage" runat="server" Font-Bold="True" Font-Size="Small"
                        ForeColor="Red"></asp:Label>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click1">LinkButton</asp:LinkButton>
                </td>
                <td>
                    <asp:Button ID="ButtonRegister" runat="server" onclick="ButtonRegister_Click"
                        Text="Register" />
                </td>
                <td>
                    &nbsp;</td>s
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>


.cs code-> i am using linq datasource for connectivity



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
/// <summary>
/// Implements IDisplay interface which
/// </summary>
public partial class RegisterUser : System.Web.UI.Page, IDisplay
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        string uniqueName = TextBoxUniqueName.Text.Trim();
        DataClassesDataContext dc = new DataClassesDataContext();
        //getting users using unique name given by user.
        var users = from u in dc.Users
                    where u.UniqueName == uniqueName
                    select u;
        if (users.Count<User>() == 0)
        {
            //Registering new user
            User user = new User();
            user.UniqueName = uniqueName;
            user.Name = TextBoxFullName.Text;
            user.Summary = TextBoxSummary.Text;
            dc.Users.InsertOnSubmit(user);
            dc.SubmitChanges();
            LabelMessage.Text = "Congratulations! The Name '" + uniqueName + "' is registered successfully.";
            TextBoxFullName.Text = "";
            TextBoxUniqueName.Text = "";
            TextBoxSummary.Text = "";
            LinqDataSource1.DataBind();
            GridViewUserList.DataBind();
        }
        else
        {
            //unique name not available
            LabelMessage.Text = "Sorry! The Name '" + uniqueName + "' is not available.";
        }
    }
  
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("~/A");
    }
  
}

now open asax file and do the code like this:

<%@ Application Language="C#" %>

<script runat="server">
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes();
    }
    private static void RegisterRoutes()
    {
        System.Web.Routing.RouteTable.Routes.Add(
                "Add_New_User", new System.Web.Routing.Route("Add_New_User",
                                    new RouteHandler("~/RegisterUser.aspx")));
       
        System.Web.Routing.RouteTable.Routes.Add(
                "user", new System.Web.Routing.Route("User/{name}",
                new UserRouteHandler("~/DisplayUser.aspx")));
       
        System.Web.Routing.RouteTable.Routes.Add(
               "A", new System.Web.Routing.Route("A",
               new RouteHandler("~/hello.aspx")));
    }
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
      
</script>

take ,DisplayUser.aspx
and html code like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayUser.aspx.cs" Inherits="DisplayUser" %>

<!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>Display User</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="left">
        <asp:HyperLink ID="HyperLink1" NavigateUrl="~/Add_New_User" runat="server">Back To List</asp:HyperLink>
    </div>
    <div align="center">
        <table style="width:70%;">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Full Name</td>
                <td>
                    <asp:TextBox ID="TextBoxFullName" runat="server" Width="200px" ReadOnly="true"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Summary</td>
                <td>
                    <asp:TextBox ID="TextBoxSummary" runat="server" Height="50px"
                        TextMode="MultiLine" Width="200px" ReadOnly="true"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Unique Name</td>
                <td>
                    <asp:TextBox ID="TextBoxUniqueName" runat="server" Width="200px" ReadOnly="true"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="LabelMessage" runat="server" Font-Bold="True" Font-Size="Small"
                        ForeColor="Red"></asp:Label>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DisplayUser : System.Web.UI.Page, IUserDisplay  
                                                                                         <-// this is important interface to display user
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (UniqueName != null && !UniqueName.Equals(""))
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var users = from u in dc.Users
                       where u.UniqueName == UniqueName
                       select u;
            if (users.Count<User>() > 0)
            {
                User user = users.First<User>();
                TextBoxFullName.Text = user.Name;
                TextBoxSummary.Text = user.Summary;
                TextBoxUniqueName.Text = user.UniqueName;
            }
        }
    }
    public string UniqueName { get; set; }

}

 if i click link button on below link button then
.cs code of hello pages would be like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class hello : System.Web.UI.Page,IDisplay// this is needed to display routing
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
       
    }
}





RegisterUser.aspx page design ->


Registered UsersUnique Name
Anurag Bajpai anurag
Vijay SP vijay
Pravat Sabat hhhhh
Rajendra NAutiyal Nauti
mukesh mukesh 123
 
Add New User
s
     
Full Name  
Summary  
Unique Name  
   
LinkButton                         

No comments:

Post a Comment