Wednesday, 1 May 2013

Server.Transfer and Response.Redirect in ASP.NET


Server.Transfer and Response.Redirect in ASP.NET



Redirection through coding:
redirecting user from one webpage to another webpage through coding is called “redirection through coding”
This can be done in 2 ways:
1)server.transfer
2)response.redirect

Server.Transfer:
Server.Transfer will perform redirection without informing to browser.

Response.Redirect:
Response.Redirect will perform redirection by informing to browser.

Differences:
1)server.transfer will perform faster redirection compare to response.redirect
2)server.transfer will support redirection only with in website. Response.redirect will support redirection with in website and between websites.

Conclusion:
If it is with in website for faster redirection go with server.transfer else go with response.redirect

Dropdownlist control:
Dropdownlist is a collection of listitems. This allows user to select only one listitem

Autopostback property:
1)autopostback property can be used to provide postback submission functionality to a control
2)this provides 2 options
  True-control can perform postback submission
  False[default]-control can not perform postback submission
3)the control performing postback submission will be execute selectedindex changed event logic [sub program]

Example on dropdownlist control

Goto visual studio
Start->run->devenv
It will display main window of visual studio
File menu->new->website->visual c#->select asp.net empty website
Weblocation->e:\aspnet\dropdownlistsite[drive:\dir\websitename]
Visual studio create a folder with website name, in this folder website related files will be placed

Add webform
Goto view menu and select solution explorer
Right click on website path and select add new item
Select webform
Give name as default.aspx

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

Goto design part

Place dropdownlist control

Dropdownlist1 properties:
Autopostback-true
Items….[click]
               
 Click on add button

 Listitem-text-selectwebsite
                 Value-selectwebsite
Listitem-text-facebook
                Value-http://www.facebook.com
Listitem-text-orkut
                Value-http://www.orkut.com
Listitem-text-gmail
                Value-http://www.gmail.com 

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // get url of website selected by user
        string url = DropDownList1.SelectedValue;
        Response.Write("<h2>" + url + "/h2>");
        Response.Redirect(url);
    }
}

Go to contrl F5

No comments:

Post a Comment