ASP LAB

 

Active Server Page

 

protected void Button1_Click(object sender, EventArgs e)

 {

     Response.Write("Hello World");

  }

 

1.BulletedList and HyperLink

 

 

protected void Button2_Click(object sender, EventArgs e)

{

    TextBox3.Text = (Convert.ToInt32(TextBox1.Text)+ Convert.ToInt32(TextBox2.Text)).ToString();

}

 

protected void Button3_Click(object sender, EventArgs e)

{

    BulletedList1.Items.Add("SQL");

    BulletedList1.Items.Add("JAVA");

    BulletedList1.Items.Add(".NET");

    BulletedList1.Items.Add("ASP");

}

NavigateUrl -  ~/WebForm2.aspx

2. ImageButton and RadioButtonList

 

 

ImageUrl - ~/t8slvp3f.png

 

protected void Button1_Click(object sender, EventArgs e)

 {

     RadioButtonList1.Items.Add("SQL");

     RadioButtonList1.Items.Add("JAVA");

     RadioButtonList1.Items.Add(".NET");

     RadioButtonList1.Items.Add("ASP");

 }

 

 protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

 {

     Response.Write("You selected" + RadioButtonList1.SelectedItem.Text);

 }

 

 protected void Button2_Click(object sender, EventArgs e)

 {

     Response.Redirect("WebForm1.aspx");

 }

 

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

 {

     Response.Redirect("WebForm1.aspx");

 }

3. HiddenField

protected void Button1_Click(object sender, EventArgs e)

 {

     HiddenField1.Value = "Hello World";

     Label1.Text = HiddenField1.Value;

 }

4. Literal

protected void Button1_Click(object sender, EventArgs e)

 {

     Literal1.Text = "<B>Hello</B>";

 }

5. DropDownList and MultiView

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

 {

     MultiView1.ActiveViewIndex = DropDownList1.SelectedIndex;

 }

6. Wizard

 

 

 

8. SiteMap and Tree view

 

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

  <siteMapNode url="First.aspx" title="First"  description="First page">

    <siteMapNode url="Second.aspx" title="Second"  description="Second page" />

    <siteMapNode url="Third.aspx" title="Third"  description="Third page" />

  </siteMapNode>

</siteMap>

 

 

 

Validation Controls

 

1.      RequiredFieldValidator

 

 

 

 

2. RangeValidator

3. RegularExpressionValidator

4. CompareValidator

 

 

5. CustomValidator

<head runat="server">

    <title></title>

    <script type="text/javascript">

        function validateLength(oSrc, args) {

            args.IsValid = (args.Value.length == 10);

        }

</script>

 

6. ValidationSummary

Output

 

10. Master Page

protected void Button1_Click(object sender, EventArgs e)

 {

     Response.Redirect("home.aspx");

 }

 

 protected void Button2_Click(object sender, EventArgs e)

 {

     Response.Redirect("aboutus.aspx");

 }

 

 protected void Button3_Click(object sender, EventArgs e)

 {

     Response.Redirect("galary.aspx");

 }

 

 protected void Button4_Click(object sender, EventArgs e)

 {

     Response.Redirect("services.aspx");

 }

 

 protected void Button5_Click(object sender, EventArgs e)

 {

     Response.Redirect("contact1.aspx");

 }

 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <p>

    This is service page</p>

</asp:Content>

 Statemanagement


 

11.  Session


Session is a very important technique to maintain state. Normally session is used to store information and identity. The server stores information using Sessionid.

protected void Button1_Click(object sender, EventArgs e)

 {

     Session["user"] = TextBox1.Text;

     Response.Redirect("inbox.aspx");

     Session.Abandon();

 }

 

For time out,in web.config

<system.web>

  <sessionState timeout="20"></sessionState>

 

Query String


Query string stores the value in URL.

Response.Redirect("ShowStringValue.aspx?Username=" + txtUsername.Text);

 

Data Controls

1. Grid View

 

 

 

 

2. Form View

3. Details View

 

 

ADO .NET [Active Data Object]

1.   Select

protected void Button1_Click(object sender, EventArgs e)

 {

     SqlConnection con = new SqlConnection(@"Data Source=TONY;Initial Catalog=dbEmployee;Integrated Security=True");

     SqlCommand cmd = new SqlCommand("Select * from Department", con);

     con.Open();

     SqlDataAdapter adp = new SqlDataAdapter(cmd);

     DataSet ds = new DataSet();

     adp.Fill(ds);

     GridView1.DataSource = ds;

     GridView1.DataBind();

     con.Close();

 

 }

2.   Insert

protected void Page_Load(object sender, EventArgs e)

 {

     TextBox4.Text = DateTime.Now.ToString();

 }

 

 protected void Button1_Click(object sender, EventArgs e)

 {

           

     SqlConnection con = new SqlConnection(@"Data Source=TONY;Initial Catalog=dbEmployee;Integrated Security=True");

     SqlCommand cmd = new SqlCommand("insert into Position values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text +"')", con);

     con.Open();

     cmd.ExecuteNonQuery();

     con.Close();

     Response.Write("<script>alert('Inserted Sucessfully')</script>");      

 }

3.   Update

 

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace WebApplication38

{

    public partial class ADOInsert : System.Web.UI.Page

    {

        SqlConnection con = new SqlConnection(@"Data Source=TONY;Initial Catalog=dbEmployee;Integrated Security=True");

        protected void Page_Load(object sender, EventArgs e)

        {

            TextBox4.Text = DateTime.Now.ToString();

            if (!IsPostBack)

            {

            SqlCommand cmd = new SqlCommand("Select distinct(DeptID) from Position", con);

            con.Open();

            SqlDataAdapter adp = new SqlDataAdapter(cmd); //for getting full details

            DataTable ds = new DataTable();

            adp.Fill(ds);

            DropDownList1.DataSource = ds;

            DropDownList1.DataTextField = "DeptID";

            DropDownList1.DataValueField = "DeptID";

            DropDownList1.DataBind();

            con.Close();

        }

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

           

            SqlCommand cmd = new SqlCommand("insert into Position values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text +"')", con);

            con.Open();

            cmd.ExecuteNonQuery();

            con.Close();

            Response.Write("<script>alert('Inserted Sucessfully')</script>");

           

        }

 

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

        {

            SqlCommand cmd = new SqlCommand("Select * from Position where DeptID='" + DropDownList1.SelectedItem.Text + "'", con);

            con.Open();

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            adp.Fill(ds);

            GridView1.DataSource = ds;

            GridView1.DataBind();

 

            SqlDataReader rd = cmd.ExecuteReader(); // getting single value

            while (rd.Read())

            {

TextBox1.Text = rd.GetString(1);

               // TextBox2.Text = rd.GetString(2);

                TextBox3.Text = rd.GetString(3);

                TextBox4.Text = rd.GetDateTime(4).ToString();

            }

            con.Close();

 

        }

 

        protected void Button3_Click(object sender, EventArgs e)

        {

            SqlCommand cmd = new SqlCommand("update Position set PositionName= '" + TextBox1.Text + "', AddDate='" + TextBox4.Text + "' where DeptID='" + DropDownList1.SelectedItem.Text + "' ", con);

            con.Open();

            cmd.ExecuteNonQuery();

            Response.Write("<script>alert('Updated Sucessfully')</script>");

            con.Close();

 

        }

 

        protected void Button4_Click(object sender, EventArgs e)

        {

            SqlCommand cmd = new SqlCommand("delete from Position where PositionID='" + DropDownList1.SelectedItem.Text + "' ", con);

            con.Open();

            cmd.ExecuteNonQuery();

            Response.Write("<script>alert('Deleted Sucessfully')</script>");

            con.Close();

 

        }

    }

}

 

 

 

Ajax

 

1.      Right click References from Solution Explorer,Add references then add ajaxtoolkit.dll file.

2.      Add this in web.config.

 

       <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />

3.      Add name space in web form.

 

                <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

 

 

   Eg.1. CalendarExtender    

 

       <div>

            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

            <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>

<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"

    TargetControlID="txtDate">

</ajaxToolkit:CalendarExtender>

        </div>

 

Eg.2.   BalloonPopup

 

        <div>

 

            <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

            <asp:Label ID="lblTarget" runat="server" Text="Hover me for a tooltip"></asp:Label>

            <ajaxToolkit:BalloonPopupExtender ID="BalloonPopupExtender1" runat="server"

    TargetControlID="lblTarget"

    BalloonPopupControlID="pnlBalloon"

    BalloonStyle="Cloud"

    BalloonSize="Small"

    DisplayOnMouseOver="true"

    DisplayOnFocus="false">

</ajaxToolkit:BalloonPopupExtender>

            <asp:Panel ID="pnlBalloon" runat="server" style="display: none;">

    This is a tooltip message.

</asp:Panel>

        </div>

 

 

Eg.3. AutoCompleteExtender    

 

     <div>

            <b style = "color:Red;">Enter Customer Name</b>

  <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>

 

 

  <asp:AutoCompleteExtender ID="txtContactsSearch_AutoCompleteExtender" MinimumPrefixLength="1"

 ServiceMethod="SearchCustomers" CompletionInterval="100" EnableCaching="false"

  CompletionSetCount="10" TargetControlID="txtContactsSearch" runat="server" FirstRowSelected="false">

  </asp:AutoCompleteExtender>

  <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

  </asp:ScriptManager>

        </div>

 

private static List<Customer > GetStudent()

  {

  List<Customer > Customers = new List<Customer >()

   {

    new Customer  {Name = "Roman"},

    new Customer  {Name = "Iqbal"},

    new Customer  { Name = "Amin"},

    new Customer  {  Name = "Asad"},

    new Customer  {  Name = "Abul"},

    new Customer  {  Name = "Abaden"},

    new Customer  { Name = "M Ali"},

    new Customer  {  Name = "Ashikur Rhaman"},

    new Customer  {  Name = "Abdul"},

    new Customer  {  Name = "Asif"},

    new Customer  {  Name = "Aminur"},

    new Customer  {  Name = "Arifur Rahman"},

    new Customer  {  Name = "Asgor"},

    new Customer  {  Name = "Abul Momen"}

 

  };

  return Customers;

  }

  public class Customer

  {

  

    public string Name { get; set; }

    

  }

  [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]

  public static List<string> SearchCustomers(string prefixText, int count)

  {

  List<Customer > CustomeList = GetStudent();

  var query = from m in CustomeList

 

  select m.Name.ToString();

  try

  {

  return (from customer in query

  where customer.ToLower().StartsWith(prefixText.ToLower())

  select customer).Take (count).ToList<string>();

 

  }

  catch (Exception ex )

  {

 

  throw new Exception("Problem Loading in finding customer" + ex.Message);

  }

 

  }

No comments:

Post a Comment

Techzmatrix