Sie sind auf Seite 1von 13

How to redirect a page to another page, use of RequiredFieldValidator and

importance of Server.UrlEncode() in asp.net


Example 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title> </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net Redirect example</h2>
<asp:Label ID="Label1" runat="server" Text="Select an Item for view Image"
Font-Bold="true" ForeColor="SeaGreen">
</asp:Label>
<br />
<asp:ListBox ID="ListBox1" runat="server" ForeColor="AliceBlue" BackColor="Crimson">
<asp:ListItem Value="1">Avatar</asp:ListItem>
<asp:ListItem Value="2">Flower</asp:ListItem>
<asp:ListItem Value="3">Bird</asp:ListItem>
<asp:ListItem Value="4">Elephant</asp:ListItem>
<asp:ListItem Value="5">Fish</asp:ListItem>
<asp:ListItem Value="6">Forest</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="ListBox1" Text="*" >
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show Image"
Font-Bold="true" ForeColor="HotPink"/>
</div>
</form>
</body>
</html>
CodeBehind
protected void Button1_Click(object sender, System.EventArgs e)
{
string url = "Image.aspx?";
url += "ID=" + ListBox1.SelectedValue.ToString() + "&";
url += "Name=" + Server.UrlEncode(ListBox1.SelectedItem.Text);
Response.Redirect(url);
}

Cross-Page PostBack example: how to submit a page to another page


Example 1
Page 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net cross page postback example</title>
</head>

<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net Cross-Page PostBack example</h2>
<asp:Label
ID="Label1"
runat="server"
Text="ProductID"
ForeColor="DodgerBlue"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
ForeColor="AliceBlue"
BackColor="DodgerBlue"
>
</asp:TextBox>
<br />
<asp:Label
ID="Label2"
runat="server"
Text="Product Name"
ForeColor="DodgerBlue"
>
</asp:Label>
<asp:TextBox
ID="TextBox2"
runat="server"
ForeColor="AliceBlue"
BackColor="DodgerBlue"
>
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Submit data"
Font-Bold="true"
ForeColor="DodgerBlue"
PostBackUrl="~/NextPage.aspx"
/>
</div>
</form>
</body>
</html>
NextPage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net Cross-Page PostBack example: how to submit a page to another page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<h2 style="color:Teal">asp.net cross page postback example</h2>


<asp:Label ID="Label1" runat="server" ForeColor="Crimson">
</asp:Label>
<br />
<asp:Label ID="Label2" runat="server" ForeColor="SeaGreen">
</asp:Label>
<br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>
NextPage.aspx.cs

protected void Page_Load(object sender, System.EventArgs e)


{
Label1.Text =String.Empty;
Label2.Text = "Product ID: " + Request.Form["TextBox1"].ToString();
Label2.Text += "<br />Product Name: " + Request.Form["TextBox2"].ToString();
string imageSource = "~/Images/" + Request.Form["TextBox1"].ToString().Trim() +
".jpg";
Image1.ImageUrl = imageSource;
Image1.BorderWidth = 2;
Image1.BorderColor = System.Drawing.Color.DodgerBlue;
}

OR
protected void Page_Load(object sender, System.EventArgs e)
{
TextBox pvProductID = (TextBox)PreviousPage.FindControl("TextBox1");
TextBox pvProductName = (TextBox)PreviousPage.FindControl("TextBox2");
Label1.Text ="You came from: "+ PreviousPage.Title.ToString();
Label2.Text = "Product ID: " + pvProductID.Text.ToString();
Label2.Text += "<br />Product Name: " + pvProductName.Text.ToString();
string imageSource = "~/Images/" + pvProductID.Text + ".jpg";
Image1.ImageUrl = imageSource;
Image1.BorderWidth = 2;
Image1.BorderColor = System.Drawing.Color.DodgerBlue;
}

ViewState example: how to store (save) and retrieve data in ViewState


Example 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net ViewState example: how to store (save) data in ViewState</title>
</head>
<body>

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


<div>
<h2 style="color:Red">asp.net ViewState example: data store</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="Teal" >
</asp:Label>
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Your Name" ForeColor="Green">
</asp:Label>
<asp:TextBox ID="TextBox1" runat="server" BackColor="Green" ForeColor="AliceBlue">
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Save data in ViewState"
OnClick="Button1_Click"
Font-Bold="true"
ForeColor="DarkGreen" />
<asp:Button
ID="Button2"
runat="server"
Text="Read data from ViewState"
OnClick="Button2_Click"
Font-Bold="true"
ForeColor="DarkGreen" />
</div>
</form>
</body>
</html>
Codebehind

protected void Button1_Click(object sender, System.EventArgs e)


{
ViewState["Name"] = TextBox1.Text.ToString();
Label1.Text = "Your name saved in ViewState.";
}
protected void Button2_Click(object sender, System.EventArgs e)
{
string name = (string)ViewState["Name"];
Label1.Text = "Hi " + name + "! we found your name in ViewState.";
}
Example 2

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net ViewState example: how to read data from ViewState</title>
</head>
<body>

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


<div>
<h2 style="color:Navy">asp.net ViewState example: data read</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="SeaGreen">
</asp:Label>
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Favorite Color" ForeColor="HotPink">
</asp:Label>
<asp:TextBox ID="TextBox1" runat="server" BackColor="HotPink" ForeColor="Snow">
</asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="City" ForeColor="HotPink">
</asp:Label>
<asp:TextBox ID="TextBox2" runat="server" BackColor="HotPink" ForeColor="Snow">
</asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Save data in ViewState"
OnClick="Button1_Click" Font-Bold="true" ForeColor="HotPink" />
<asp:Button ID="Button2" runat="server" Text="Read data from ViewState"
OnClick="Button2_Click" Font-Bold="true" ForeColor="HotPink" />
</div>
</form>
</body>
</html>
CodeBehind

protected void Button1_Click(object sender, System.EventArgs e)


{
ViewState["FavoriteColor"] = TextBox1.Text.ToString();
ViewState["City"] = TextBox2.Text.ToString();
Label1.Text = "Your data saved in ViewState.";
}
protected void Button2_Click(object sender, System.EventArgs e)
{
string color = (string)ViewState["FavoriteColor"];
string city = (string)ViewState["City"];
Label1.Text = "Hi your favorite color is: " + color + "<br />and you came from: " + city;
}
N. B. :Even if you change data in textbox and then click on Button2 data in viewstate will not
be changed it will retain its value during postback.

Session Add example: how to add an item in the session


Example 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net session Add example: how to add an item in the session</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">asp.net session example: Session Add</h2>
<asp:Label
ID="Label1"

runat="server"
Font-Size="Large"
ForeColor="IndianRed"
>
</asp:Label>
</div>
</form>
</body>
</html>
CodeBehind
protected void Page_Load(object sender, System.EventArgs e)
{
Session["CityID"] = "10";
Session["CityName"] = "Paris";
Label1.Text = "Session read...<br />";
Label1.Text += "Total session items: " + Session.Count;
Label1.Text += "<br />City ID : " + Session["CityID"];
Label1.Text += "<br />City Name : " + Session["CityName"];
Label1.Text += "<br><br />Now add an item in session.";
Session.Add("EmployeeName","Jenny");
Label1.Text += "<br />Now Total session items: " + Session.Count;
Label1.Text += "<br />City ID : " + Session["CityID"];
Label1.Text += "<br />City Name : " + Session["CityName"];
Label1.Text += "<br />Employee Name : " + Session["EmployeeName"];
}
Example 2
session IsNewSession example: how to get whether the session was created only for the current
request or not

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net session IsNewSession example: how to get whether the session was created onl
y for the current request or not</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Blue">asp.net session example: Session IsNewSession?</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="OrangeRed">
</asp:Label>
</div>
</form>
</body>
</html>
CodeBehind
protected void Page_Load(object sender, System.EventArgs e)
{
Session["ZipCode"] = "12345";

Label1.Text = "Session read<br />";


Label1.Text += "Session IsNewSession?: " + Session.IsNewSession;
Label1.Text += "<br />Zip Code :" + Session["ZipCode"];
}
OR to check whether session exist for current users request or not
protected void Page_Load(object sender, System.EventArgs e)
{
if (Session["ZipCode "] == null)
return;
else
{
Label1.Text = "Session read<br />";
Label1.Text += "Session IsNewSession?: " + Session.IsNewSession;
Label1.Text += "<br />Zip Code :" + Session["ZipCode"];
}
}

Application state example: how to use application state in asp.net


Example 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net application state example: how to use application state in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net application state example</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Crimson">
</asp:Label>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Show Button Click Status"
OnClick="Button1_Click" Font-Bold="true" ForeColor="SeaGreen" />
</div>
</form>
</body>
</html>
CodeBehind
protected void Button1_Click(object sender, System.EventArgs e)
{
int counter = 0;
if(Application["ButtonClickCounter"] !=null)
{
counter = (int)Application["ButtonClickCounter"];
}
counter++;
Application["ButtonClickCounter"] = counter;
Label1.Text = "Button Clicked: " + counter.ToString() + " times";
}
N.B.: Use Global.asax to initialize Application Variables.

application state example: how to lock and unlock application state in asp.net
Example 2

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net application state example: how to lock and unlock application state in asp.net</titl
e>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net application state example: Lock and UnLock</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="DodgerBlue"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="Show Button Click Status"
OnClick="Button1_Click"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>
CodeBehind

protected void Button1_Click(object sender, System.EventArgs e)


{
Application.Lock();
int clickCounter = 0;
if(Application["ClickCounter"] !=null)
{
clickCounter = (int)Application["ClickCounter"];
}
clickCounter++;
Application["ClickCounter"] = clickCounter;
Application.UnLock();
Label1.Text = "Button Clicked: " + clickCounter.ToString() + " times";
}

Cookie example: how to create a cookie


Example 1
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net cookie example: how to create a cookie</title>

</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net Cookie example: Create a cookie</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="SeaGreen">
</asp:Label>
<asp:Label ID="Label2" runat="server" Font-Size="Large" ForeColor="Crimson">
</asp:Label>
</div>
</form>
</body>
</html>
CodeBehind

protected void Page_Load(object sender, System.EventArgs e)


{
HttpCookie userCookie = new HttpCookie("UserInfo");
userCookie["Country"] = "Italy";
userCookie["City"] = "Rome";
userCookie["Name"] = "Jones";
userCookie.Expires = DateTime.Now.AddDays(3);
Response.Cookies.Add(userCookie);
Label1.Text = "Cookie create successful!<br><br/>";
HttpCookie cookie = Request.Cookies["UserInfo"];
if (cookie != null)
{
string country = cookie["Country"];
string city = cookie["City"];
string name = cookie["Name"];
Label2.Text = "Cookie Found and read<br/>";
Label2.Text += "Name: " + name;
Label2.Text += "<br />Country: " + country;
Label2.Text += "<br />City: " + city;
}
}
Example 2
cookie example: how to use cookie

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net cookie example: how to use cookie in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net cookie: how to use</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="SeaGreen">
</asp:Label>
</div>

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

CodeBehind

protected void page_Load(object sender, System.EventArgs e)


{
HttpCookie favoriteColor = Request.Cookies["FavoriteColor"];
if (favoriteColor == null)
{
HttpCookie userCookie = new HttpCookie("FavoriteColor");
userCookie["Name"] = "Jones";
userCookie["Color"] = "Crimson";
userCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(userCookie);
Label1.Text = "Cookie created at: " + DateTime.Now.ToString()+ "<br /><br />";
}
else
{
string name = favoriteColor["Name"];
string color = favoriteColor["Color"];
Label1.Text += "Cookie found and read<br />";
Label1.Text += "Hi " + name + " your favorite Color: " + color;
}
}
Example 3
cookie example: how to read a cookie

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net cookie example: how to read a cookie</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net Cookie example: Read a cookie</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DarkGreen">
</asp:Label>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Read Cookie" OnClick="Button1_Click"
Font-Bold="true" ForeColor="HotPink" />
</div>
</form>
</body>
</html>
CodeBehind
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
HttpCookie infoCookie = new HttpCookie("Info");
infoCookie["Country"] = "USA";

infoCookie["City"] = "NewYork";
infoCookie["Name"] = "Jenny";
infoCookie.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(infoCookie);
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie cookie = Request.Cookies["Info"];
if (cookie != null)
{
string country = cookie["Country"];
string city = cookie["City"];
string name = cookie["Name"];
Label1.Text = "Cookie[Info] Found and read<br/>";
Label1.Text += "Name: " + name;
Label1.Text += "<br />Country: " + country;
Label1.Text += "<br />City: " + city;
}
}
DataReader example
Example 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DataReader example: how to use DataReader in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Codebehind
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection MyConnection;
SqlCommand MyCommand;
SqlDataReader MyReader;
MyConnection = new SqlConnection();
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["dbcon
n"].ConnectionString;
MyCommand = new SqlCommand();
MyCommand.CommandText = "SELECT TOP 10 * From PRODUCTS";

MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyCommand.Connection.Open();
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
GridView1.DataSource = MyReader;
GridView1.DataBind();
MyCommand.Dispose();
MyConnection.Dispose();
}
}
DataAdapter example
Example 1

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DataAdapter example: how to use DataAdapter in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Code Behind file
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection MyConnection;
SqlCommand MyCommand;
SqlDataAdapter MyAdapter;
DataTable MyTable;
MyConnection = new SqlConnection();
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["dbconn"].
ConnectionString;
MyCommand = new SqlCommand();
MyCommand.CommandText = "SELECT TOP 8 * FROM PRODUCTS";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyTable = new DataTable();
MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = MyCommand;
MyAdapter.Fill(MyTable);
GridView1.DataSource = MyTable.DefaultView;

GridView1.DataBind();
MyAdapter.Dispose();
MyCommand.Dispose();
MyConnection.Dispose();
}
}

Das könnte Ihnen auch gefallen