Beruflich Dokumente
Kultur Dokumente
<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);
}
<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>
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;
}
<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>
<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>
<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";
<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
</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
<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
<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();
}
}