C#DOTNETCONSOLEAPPLICATION

ASP LAB

 

Active Server Page

ASP.net

àActive server page network enabling technology

ASP.NET is an open-source, server-side web application framework designed for web development to produce dynamic web pages. Developed by Microsoft, it allows developers to build robust, secure, and scalable web applications and services.

 

Here are some key points about ASP.NET:

 

Languages Supported: ASP.NET supports multiple programming languages, including C#, VB.NET, and F#.

Components:

Web Forms: Enables rapid development with event-driven, drag-and-drop web forms.

MVC (Model-View-Controller): Provides a separation of concerns, making it easier to manage and test web applications.

Web API: Facilitates the creation of HTTP-based services that can be consumed by various clients, including browsers and mobile devices.

Razor Pages: A newer, page-based coding model that makes building web UI easier and more productive.

ASP.NET Core: The cross-platform, high-performance, and modular redesign of ASP.NET. It runs on Windows, macOS, and Linux and is ideal for modern web applications.

Libraries and Frameworks: ASP.NET includes a variety of libraries and frameworks, such as Entity Framework for data access, SignalR for real-time web functionality, and Blazor for building interactive web UIs with C#.

Security: ASP.NET provides built-in security features, including authentication, authorization, and data protection mechanisms.

Deployment: Applications built with ASP.NET can be deployed on various servers and platforms, including IIS, Azure, and Docker.

Sure, here’s a list of key features of ASP.NET:

 

Server-Side Scripting

Rich Toolbox and Designer in Visual Studio

State Management

View State

Session State

Application State

Cache

Security

Authentication and Authorization

Data Protection

Caching

Master Pages and Themes

Rich User Controls and Custom Controls

Event-Driven Programming

Data Access

ADO.NET

Entity Framework

Web Services and APIs

Web API

WCF (Windows Communication Foundation)

Asynchronous Programming

Deployment

Web Deployment

Continuous Integration and Continuous Deployment (CI/CD)

Cross-Platform Development (ASP.NET Core)

Globalization and Localization

Logging and Diagnostics

 

A Master Page in ASP.NET is a template for creating a consistent layout across multiple web pages in a web application. It allows you to define the common structure and interface elements (like headers, footers, and navigation menus) in one place, ensuring a uniform look and feel throughout the site.

 

Source

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

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

        <div>Welcome

        </div>

        <p>

            <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" />

        </p>

    </form>

</body>

</html>

=========

 

namespace WebApplication1

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            Response.Write("Welcome to ASP.net");

        }

    }

 

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

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

        }

    }

}

Source

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style type="text/css">

        .auto-style1 {

            width: 100%;

        }

        .auto-style2 {

            height: 26px;

        }

    </style>

</head>

<body>

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

        <table class="auto-style1">

            <tr>

                <td>Enter first Number</td>

                <td>

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

                </td>

            </tr>

            <tr>

                <td>&nbsp;</td>

                <td>&nbsp;</td>

            </tr>

            <tr>

                <td>Enter second Number</td>

                <td>

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

                </td>

            </tr>

            <tr>

                <td class="auto-style2">Result</td>

                <td class="auto-style2">

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

                </td>

            </tr>

            <tr>

                <td>&nbsp;</td>

                <td>

                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Output" />

                </td>

            </tr>

        </table>

        <div>

        </div>

    </form>

</body>

</html>

Page Linking

namespace WebApplication1

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            Response.Redirect("https://www.netflix.com/in/");

 

        }

Image Button

Imagebuttonàproperties image Url

SolutionExplorer àrightclickààadd existing itemàaddimage

namespace WebApplication1

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

        {

            Response.Redirect("WebForm2.aspx");

        }

    }

}

Hidden Field

 

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            HiddenField1.Value = "Hello World";

 

            Label1.Text = HiddenField1.Value;

        }

    }

}

Literal—we can format using html code

 

namespace WebApplication1

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

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

        }

    }

}

Multiview

From toolbox

Dropdownlist

Multiview

view

 

namespace WebApplication1

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

        {

            MultiView1.ActiveViewIndex = DropDownList1.SelectedIndex;

        }

    }

}

Wizard

Wizardàadditemsàadd Home about etc

We can edit each page individually

Then run

 

Side Map and Tree View

Webapplicationàright click àaddànew itemàwindow opens àsearch site map

Web.sitemap-----extension

Create multiple web forms ,these can be linked using sitemap

On each open tree view and give name and description

Code for site map

<?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>

 

 

Hyper Link—linking web pages

In properties

Text – change name

Navigate url – select the page that we need to navigate

Link Button

Poperties

Text – change name

Post back url-select the page to navigate to

Validation Controls

1.      RequiredField Validator

Properties

Control to validate

Error message

Fore Color

2.      Range Validator

Properties

Control to validate

Error message

Fore color

Minimum value

Maximum value

3.      RegularExpression Validator

Properties

Fore color

Error message

Control to validate

Validation expression—internet e-mail address

4.      Compare Validator

Properties

ControltoCompare

Control to validate

Fore Color

Error message

 

5.      Custom Validator—we can customize as per needed

Properties

Control to validate

Fore Color

Error mesaage

Client validation function

html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script type="text/javascript">

 

        function validateLength(oSrc, args) {

 

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

 

        }

 

    </script>

Need to copy paste in source of the code

 

6.Validation Summary

 

to hide Password  click on Textbox àproperties àText mode àchange toàpassword from static

Master Page(Parent Page)

A master page is a template in ASP.NET that defines the common structure, layout, and behavior for a set of related web pages. It contains placeholders for content, which can be overridden by individual content pages, allowing them to provide unique content while inheriting the overall look and functionality defined by the master page. The master page is an ASP.NET file with the extension .master. It can include static text, HTML elements, and server controls.

Right click Web application2 àAddànew item

Web Form Master à extention ----.Master

Content page – child Page

·         Copy an image inside the project

·         Refresh solution explorer

·         Right click on the needed image and select include in the project

·         Inside div add Image from toolbox

·         In design click on Image icon select properties

·         In Properties image URl àclick àselect image àclick ok

·         We can adjust the size of the image in the source

 

Insert a table(1 row 5 coloumn) inside each coloumn place a button

Name the buttons as per requirements

 

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication2.Site1" %>

 

<!DOCTYPE html>

 

<html>

<head runat="server">

    <title></title>

    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>

    <style type="text/css">

        .auto-style1 {

            width: 100%;

        }

    </style>

</head>

<body>

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

        <div>

            <asp:Image ID="Image1" runat="server" ImageUrl="~/coverphoto.jpg" Width="331px" />

            <asp:Image ID="Image2" runat="server" ImageUrl="~/image2.jpg"  Width="364px" Height="171px"/>

             <table class="auto-style1">

     <tr>

         <td>

             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Home" />

         </td>

         <td>

             <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="About US" />

         </td>

         <td>

             <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Departments" />

         </td>

         <td>

             <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Admission" />

         </td>

         <td>

             <asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="Placements" />

         </td>

     </tr>

 </table>

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

            </asp:ContentPlaceHolder>

          

        </div>

    </form>

</body>

</html>

AdRotator- to rotate advertisements

Xml—extensible markup language

Html—pre-defined tags

Xml—user defined tags

 

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

<Advertisements>

            <Ad>

                        <ImageUrl>aboutus.png</ImageUrl>

            </Ad>

            <Ad>

                        <ImageUrl>image2.jpg</ImageUrl>

            </Ad>

            <Ad>

                        <ImageUrl>coverphoto.jpg</ImageUrl>

            </Ad>

            <Ad>

                        <ImageUrl>homeimage.jpeg</ImageUrl>

            </Ad>

</Advertisements>

 

Right click add new item xml file

Create tags

Give different images

New Webform à Ad-Rotator àpropertiesàAdvertisementfileàadd the xml file

 

 

AJAX-Asynchronous javaScript and XML

 

Script Manager – to manage AJAX controls

UpdatePanel – all tools inside update panel becomes AJAX tools.Inside update panel place timer and ad-Rotator

Timer àInterval à to control time by which each ad changes(eg-1000)

Ad-Rotator – add xml file

 

ScriptManager control registers the script for the Microsoft AJAX Library with the page.

UpdatePanel Control You can refresh the selected part of the web page by using UpdatePanel control, Ajax updatepanel control contains a two child tags that is ContentTemplate and Triggers. In a ContenTemplate tag we used to place the user controls and the Trigger tag allows you to define certain triggers which will make the panel update its content.

State Management

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.

 

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            Session["user"] = TextBox1.Text;

 

            Response.Redirect("inbox.aspx");

        }

    }

}

Inbox

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Label1.Text = "Welcome "+Session["user"].ToString();

    }

}

 

QueryString--Query String Query string stores the value in URL.

Username is passed through the address bar

localhost:44378/ShowStringValue.aspx?Username=user

 

Design login form à next page as ShowStringValue

On click àlogin Button goes to next page and the Username is passed through address bar

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace WebApplication3

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

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

        }

    }

}

 

Disadvantage

Since the information is passed through the address bar the stringquery is not commonly used as the information is easily available and so can be  hacked

 

 

Data Controls

1.Grid View

2.Form View – we can view the details as forms

New webFormà Form view from toolboxà add the db connectionàfinish àrun

Each page has one one information

3.ADO.net—(Active data Object)

Data grid View Binding

using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace WebApplication3

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9DL12AG\SQLEXPRESS;Initial Catalog=dbcollege;Integrated Security=True;");

 

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

 

            con.Open();

 

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

 

            DataSet ds = new DataSet();

 

            adp.Fill(ds);

 

            GridView1.DataSource = ds;

 

            GridView1.DataBind();

 

        }

    }

}

 

Select update delete and view all

 

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace WebApplication3

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9DL12AG\SQLEXPRESS;Initial Catalog=dbcollege;Integrated Security=True;");

 

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

 

            con.Open();

 

            cmd.ExecuteNonQuery();

 

            con.Close();

 

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

        }

 

        protected void Button2_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9DL12AG\SQLEXPRESS;Initial Catalog=dbcollege;Integrated Security=True;");

            SqlCommand cmd = new SqlCommand("update tblemployee set name='"+TextBox1.Text+"',age='"+TextBox2.Text+"',department='"+DropDownList1.Text+"',designation='"+DropDownList2.Text+"',salary='"+TextBox3.Text+"' where id ='"+DropDownList3.Text+"'" ,con);

            con.Open(); cmd.ExecuteNonQuery();

            con.Close();

        }

 

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9DL12AG\SQLEXPRESS;Initial Catalog=dbcollege;Integrated Security=True;");

            SqlCommand cmd = new SqlCommand("select * from tblemployee where id='" + Convert.ToInt32(DropDownList3.SelectedValue) + "'", con);

            con.Open();

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            adp.Fill(ds);

            GridView1.DataSource = ds;

            GridView1.DataMember = ds.Tables[0].ToString();

 

 

            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())

            {

                TextBox1.Text = rd.GetString(1);

                TextBox2.Text = rd.GetInt32(2).ToString();

                DropDownList1.Text = rd.GetString(3);

                DropDownList2.Text = rd.GetString(4);

                TextBox3.Text = rd.GetInt32(2).ToString();

               

 

            }

            con.Close();

 

        }

 

        protected void Button3_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9DL12AG\SQLEXPRESS;Initial Catalog=dbcollege;Integrated Security=True;");

            SqlCommand cmd = new SqlCommand("delete from tblemployee where id = '"+DropDownList3.Text+"'",con);

 

            con.Open();

            cmd.ExecuteNonQuery();

            con.Close();

        }

 

        protected void Button4_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9DL12AG\SQLEXPRESS;Initial Catalog=dbcollege;Integrated Security=True;");

 

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

 

            con.Open();

 

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

 

            DataSet ds = new DataSet();

 

            adp.Fill(ds);

 

            GridView1.DataSource = ds;

 

            GridView1.DataBind();

        }

    }

}

 

 

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