ASP.NET Tutorial/Development/URL

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create a URL based on the current URL (C#)

File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Cookieless1" %>
<!DOCTYPE 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" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:HyperLink ID="lnkRedirect" 
                                   runat="server" 
                                   NavigateUrl="NextPage.aspx">Link with Relative Path</asp:HyperLink>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="cmdLink" runat="server" Text="Redirect with Relative Path" OnClick="cmdLink_Click"></asp:Button>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="cmdLinkAbsolute" runat="server" Text="Redirect with Absolute Path" OnClick="cmdLinkAbsolute_Click"></asp:Button>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Cookieless1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
  {
    Session["test"] = "Test String";
    }
  protected void cmdLink_Click(object sender, EventArgs e)
  {
    Response.Redirect("NextPage.aspx");
  }
  protected void cmdLinkAbsolute_Click(object sender, EventArgs e)
  {
    string url = "http://" + Request.Url.Authority +
      Request.Url.Segments[0] + Request.Url.Segments[1] +
      "NextPage.aspx";
    Response.Redirect(url);
  }
}

File: NextPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NextPage.aspx.cs" Inherits="Cookieless2" %>
<!DOCTYPE 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" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblInfo" 
                   runat="server" 
                   Font-Bold="True" 
                   Font-Names="Verdana" 
                   Font-Size="Large"></asp:Label>
    </div>
    </form>
</body>
</html>

File: NextPage.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Cookieless2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["test"] != null)
    {
      lblInfo.Text = "Successfully retrieved " + (string)Session["test"];
    }
    else
    {
      lblInfo.Text = "Session information not found.";
    }
    }
}