August 2012 Master of Computer Application (MCA) - Semester 5 MC0081 - .

(DOT) Net Technolog ies - 4 Credits (Book ID: B0974) Assignment Set - 1 (40 Marks) Answer all Questions Each question carries TEN marks 1. Describe the steps involved in creating classes and objects with the help of a program in C#. 2. Describe the following with respect to creating Web Forms in .Net environment: A. Web Form Life Cycle B.Creating a Web Form Write programs w ith corresponding output screens to demonstrate the above concepts. 3. Describe the following with respect to State Management in ASP.Net: a. Cookies in ASP.NET b. Session State c. Application State 4. Describe the following with respect to Web Services in .Net: a. Writing and Testing a Web Service b. Implementing a We b Service Client

3. Master Pages b.. 4.NET Applicat ions .2 (40 Marks) Answer all Questions Each question carries TEN marks 1.4 Credits (Book ID: B0974) Assignment Set . Deploying ASP. 2. Design a simple Window based form application to perform basic arithm etic operations.August 2012 Master of Computer Application (MCA) . Describe the following with respect to Web site deployment i n ASP.Net: a .Net: a. Finding greatest of n numbers Write separate programs f or each of the above points. Write a program in C# language to perform the following operations: a. (DOT) Net Technolog ies . Creating Application Pools (IIS 6.0) b. Basic arithmetic operations b. Themes & Skins Write code snippets to demonstrate the above co ncepts. Explain the following with respect to ASP.Semester 5 MC0081 .

methods and events. Describe the steps involved in creating classes and objects with the help of a program in C#.1. a fundamental characte ristic of object-oriented programming. Unlike structs. If the class is not declared as static. // Constructor public Person() { name = "unknown". } Creating object Customer object1 = new Customer(). If the class is declared as static. At that time. A class is like a blu eprint. methods and events are inherited // New Manager fi elds. For more information.. not an instance variabl e. see Static Classes and Static Class Members (C# Program ming Guide). It defines the data and behavior of a type. then only one copy exists in memory and client code can only access it through the class itself. properties. classes support inheritance. A class is a construct that enables you to create your own custom types by group ing together variables of other types. client code can use it by creating objects or instances which are as signed to a variable. the CLR marks it as eligible for garbage collecti on. The variable remains in memory until all references to it go out of scope.. methods and events go here.. } } class Tes tPerson { static void Main() { Person person = new Person().. } // Method public void SetName(string newName) { name = newName. properties. properties. Declaring classes public class Customer { //Fields. methods and events go here. . } EXAMPL E public class Person { // Field public string name. Class Inheritance public class Manager : Employee { // Employee fields.

Stage 2 . The server cont rol would then update its state based on this postback data. }} 2. the vi ew state data that had been saved from the previous page visit is loaded and rec ursively populated into the control hierarchy of the Page. A server control can indicate that it is inter ested in examining the posted back data by implementing the IPostBackDataHandler interface. Stage 3 . . Console. If i t finds the control.SetName("John Smith").Console. the Page class enumerates the posted back form fields. Describe the following with respect to creating Web Forms in . and searches for the corresponding server control.Initialization After the control hierarchy has been built.WriteLine(person. This stage is marked by having the Page and controls fire the ir Init events. the Page. As we'll discuss later in this article.WriteLine( person. it checks to see if the control implements the IPostBackDat aHandler interface. It is during this sta ge that the view state is validated. the view state can become invalid due to a number of reasons.").ReadKey(). along with all of the controls in its control hierarchy. During this stage.WriteLine("Press any key to exit. such as view state ta mpering. Console. person.Creating a Web Form Ans 2 of A Stage 1 .name). it hands off the appropriate postback data to th e server control by calling the control's LoadPostData() method.Load View State The load view state stage only happens when the page has been posted back.Net environment : A. enter the ini tialization stage.Load Postback Data The load postback data stage also only happens wh en the page has been posted back. and injecting dynamic controls into the middle of the control hierarchy . Console. If it does. At this point in the page life cycle. Web Form Life Cycle B. and the Web control properties that are specified in the decla rative syntax have been In this stage in the page life cycle. the control hierarchy has been constructed.

One nice thing about ASP .NET developers are familiar. as we've all created an event handler for a page's Load event (Page_Load). if you have a TextBox Web control on a page and the user enters some value into the TextBox and posts back the page. To concretize things. let's look at a simple example. along with the postback data (from s tage 3.NET Web page. Load View State). This happens because the Text Box Web control implements the IPostBackDataHandler interface. imagine that we have an ASP. which then updates its T ext property.NET is that the Web controls in a Web Form remember their values across postbac k.Raise Postback Event Certain server controls raise events with respect to changes that occurred between postbacks. the view state has b een loaded (from stage 2. the DropDownList Web c ontrol has . passing the form field values back in the HTTP POST heade rs. "Hello. When the Load event fires. the TextBox's Text property is automatically updated to the user's entered value. the following HTML will be rendered for the TextBox: <input type="text" id="txt Name" name="txtName" />. That is.Load This is the stage with which all ASP. When the user enters a value into this TextBox (such as . Stage 5 . and the Page clas s hands off the appropriate value to the TextBox class. Load Postback Data). World!") and submits the form. Stage 4 . These include the hidden form field values (such as __VIEWSTATE). For example.To help clarify things. along with the value from the txtName TextBox. when the Load eve nt fires we know that the page has been restored to its state from the previous page visit. When the page is first visited. If the page has been posted back. the browser will make a request to the same ASP.NET Web page wit h a TextBox whose ID property is set to txtName.

. the Page class constru cts the page's view state. In stage 7. when the page's Web Fo rm is rendered.Save View State In the save view state stage. The page accomplishes this by recursively calling the SaveViewState() method of the controls in its control hierarchy. There are two flavors of post back events. keep in mind that every single time an ASP. In the render stage the HTML that is emitted to the client requesting the page i s generated. The Page class accomplishes this by recursively invoking the Render Control() method of each of the controls in its hierarchy. These are events that are raised by the server control for whatever reason the control sees fit. such as the PreRender and Unload stages. and the Calendar contro l raises the VisibleMonthChanged event when the user moves to another month. For example. or the TextBox's TextChanged event. which represents the state that must persist across p ostbacks. An example is the DropDownListsSelectedIndexCh anged event. the Butto n Web control raises the Click event when it is clicked. The other flavor of postback events is the raised event. (Note that I did omit a couple of stages. These seven stages ar e the most important stages with respect to understanding view state. Stage 6 .) As you continue through the article. This event fires when some piece of d ata is changed between postbacks. Another exampl e: if the Web Form was posted back due to a Button Web control being clicked. which fires if the DropDownList's SelectedIndex ha s changed from the SelectedIndex value in the previous page load. Con trols that fire raised events must implement the IPostBackEventHandler interface .a SelectedIndexChanged event. th e Button's Click event is fired during this stage. saved state is t hen serialized into a base-64 encoded string. the view state is persisted in the page as a hidden form field.NET We b page is requested. Server controls that provide ch anged events must implement the IPostBackDataHandler interface. The first is a changed event. This combined. it proceeds through these series of stages.

each time you create a new pa ge or component for your Web site. 5. right-click the tab that displays th e file click name and then click Close Close. 4. . The following illustration shows an example of the Add New Item dialog box. type FirstWebPage. Clear the Place code in separate file check box.aspx page. In the template list.aspx page as the home page for your Web site. However. To do this. C:\ \BasicWebSite). and then click Add New Item Item. Close the Default. However. Visu al Studio adds an ASP. you will create and work with a new page. When you created the Web site project. 3. you specified a default language based o n the project template that you selected. r ight-click the Web site (for example. select Web Form. You can use the Default.NET Web Site project template. In Solution Explorer. In the Name box.Ans 2 of B Creating a New ASP.aspx.NET Web Forms Page When you create a new Web site using the ASP. you can select the programming language for t hat page or component.NET page (Web Forms page) named Default. for this walk through. You can use different programming languages in the same W eb site. 2. The Add New Item dialog box is displayed. To add a page to the Web site 1. right .

as shown in the following illustration. At this point. 2. Type Welcome to Visual Web Developer and press ENTER twi ce. . Adding HTML to the Page In this part of the walkthrough. Switch to Source view. you will add some static text to the page. At the bottom of the document window. Click inside the rectangle that is outlin ed by a dashed line. 3. To add text to the page 1. The code for ASP. click the Design tab to switch to Desig n view. Click Add. you are creating a single-file page with the code and HTML in the same page.NET pages can be located either in the page o r in a separate class file. This rectangle re presents a div element on the page. 4.In this walkthrough. Visual Studio creates the new page and opens it. 6. Design view displays the page that you are working on in a WYSIWYG-like way. You can see the HTML that you created by typing in Des ign view. so the pag e is blank except for a dashed line that outlines a rectangle. you do not have any text or controls on the page. The following illustration shows the text you typed in Design view.


Now.Describe the following with respect to State Management in ASP. // Define the cookie for the next visit Response. Cookies i n ASP. A cookie is a small amount of data that is store d either in a text file on the client file system (if the cookie is persistent) or in memory in the client browser session (if the cookie is temporary). set the Path property.Cookies["lastVi sit"] != null) // Encode the cookie in case the cookie contains clientside scrip t Label1.Value = DateTime.Cookies["lastVisit"]. as the following examp le demonstrates: . Reading and Writing Cookies: A Web application creates a cookie by sending it to the client as a header in an HTTP response.Text = Server. You can’t directly delet e cookies because they are stored on the client’s computer. ove rwrite the cookie and set an expiration date in the past. To limit the scope of a cookie to a folder. else Labe l1. If you do not define the Expires property.NET b.Net: a.Now. To delete a cookie. Session State c. Example: // Check if cookie exists. Create a cookie -> add a value to the Response.Cookies["lastVisit" ].Response.HtmlEncode(Request. browsers won’t send a cookie to a Web site with a different ho stname. the browser stores it in memory and t he cookie is lost if the user closes his or her browser. Controlling the Cookie Scope: By default.Expires = DateTime.Text = "No value defined".AddDays(1).net : Web applications can store small pieces of data in the clien t’s Web browser by using cookies.Cookies HttpCookieCollection. and display it if it does if (Request.Value). Application State Cookiesin asp. Read a cookie -> read values in Request. You can control a cookie’s scope to either limit the scope to a specific f older on the Web server or expand the scope to any server in a domain. The mos t common use of cookies is to identify a single user as he or she visits multipl e Web pages.ToString().Cookies. The Web browser then s ubmits the same cookie to the server with every new request.C ookies["lastVisit"].

A typical query string might look like th e following real-world example: http://support.” and a value for that parameter. Response. The query strin g (which starts with a question mark [?]) contains a single parameter named “kbid.Cookies[“lastVisit”]. you typically can’t store more than 20 cookies per site.” Query strings can also have multiple param eters. such as the following real-world . A query string is information that is appended to the end of a page URL.Value = "Tony". “315233.Cookies["info"]["visit"].ToString(). you can st ore multiple values in a cookie. such as search terms or page numbers.aspx page. Response. Storing Multiple Values in a Cookie: Though it depends on the br owser.Cook ies["info"]["firstName"].Now.Cookies["info"]["border"].AddDays(1).aspx?kbid=3 15233 In this example. Domain = “Contoso”. Response.Value = DateTime. Running the code in this example sends a cookie with the following value to the Web browser: (visit=4/5/2006 2:35:18 PM) (firstName=Tony) (border=blue) Query St rings: Query strings are commonly used to store variables that identify specific pages. We can expand the scope to a part icular domain using the following statement: Example: Response. Through this the scope is limited to the “/Application1” folder that is the browser submits the cookie to any page with in this folder and not to pages in other fol ders even if the folder is in the same server. as the following code demonstrates: Example: Response. the URL identifies the Default.Cookies["info"].Expires = = "/Application1".Now.Valu e = "blue". To work around the 20-cookie limit. and each cookie ca n be a maximum of 4 KB in["lastVisit"].Example: Response.

Once you add your application-specific information to ap plication state. a global storage mechanism that is accessible from all pages in the Web application. Some Browsers and client devices impose a 208 3 – character limit on the length of the URL.Text = "User: " + A pplication state is a great place to store information that is not user-specific . Pre fs: " + Server. Server . Example: Label1.Ht mlEncode(Request.QueryString[“q”] | hello world Limitations for Query Strings: 1. 3. all pages can access data from a singl e location in memory.aspx?mkt=en-US&setlang=en-US&q=hello+wo rld Value Name | ASP.QueryString["page"]).QueryString["user"]) + ".Side State Management: Application State: ASP. You must submit the page using an HTTP GET command in order for query string values to be available during page p rocessing.NET provides three events that enable you to initialize App lication variables (free resources when the application shuts down) and respond to Application errors: a. This is the perfect place to initialize Application variables. Application state is stored in the Applicatio n key/value dictionary.URL. .HtmlEncode(Request. 2. Page: " + Server. the server manages it. Therefore.QueryString[“setlang”] | en-US q | By storing it in the application state. and it is never exposed to the client. you shouldn’t add query strings to button targets in forms.NET Object | Value mkt | Request.NET allows you to save va lues using application Web s ite: http://search.HtmlEncode(Request.QueryString["prefs"]) + ". which specifies a language and query when searching the Microsoft. Data stor ed in the Application object is not permanent and is lost any time the applicati on is restarted. Application_Start: Raised when the application starts. rather than keeping separate copies of the data. You must manually add query string values to every hyperlink that the user mi ght click.QueryString[“mkt”] | en-US set lang | Request. ASP.

you must set the . iii. Therefore. In addition. Raise appropriate session management events. StateServer Stores session sta te in a service called the ASP. Session State: ASP.NET session state supports se veral different storage options for session data: a. Therefore. Uniquely identify browser or client-device requests an d map them to individual session instances on the server. the servic e is set up to start manually by default. however. and it offers much better per formance than using the ASP. Session state is similar to application state.NET State Service. ASP. You can use session state to accomplis h the following tasks: i.NET allows you to save values using session state. If different users are using your application. you can use session state to store user-specific information. This ensures that session stat e is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. Store sessio n-specific data on the server for use across multiple browser or client-device r equests during the same session. c. Use this to free appl ication resources and perform logging. Use this to perform error logging.NET State Service is includ ed with any computer set up to run ASP. if a user leaves your appli cation and then returns later after the session timeout period.b. Session state is st ored in the Session key/value dictionary. when configuring the ASP. InProc Stores session state in memory on the Web server. This allows you to tra ck which pages a user saw on your site during a specific visit. ii. This is perfect for storing shopping cart infor mation. but robust applications that use multiple Web servers or must persist session data between application restarts should use State Server or SQLServer.NET Web applications. except that it is scoped to the c urrent browser session. In addition.NE T State Service. This is the default. a storage mechanism that is accessible from all pages requested by a single Web browser se ssion. each user session has a different session state. Application_End: Raised when an application shuts down. session state in formation is lost and a new session is created for the user. b. Application_Error: Raised when an unha ndled error occurs. ASP.NET state service or storing state information in a database server. you can w rite application code leveraging these events. InProc is fine for simple applications.

You should disable session state if you are not using it to improve performance.startup type to Automatic. How ever. Off Disables session state. Custom Enables you to specify a custom storage provider. a SQL Server database offers more robust data integrity and reporting capa bilities. You also n eed to implement the custom storage provider. This ensures that session state is preserved if the Web application is re started and also makes session state available to multiple Web servers in a Web farm.NET State Service outperforms SQLServer. d. On the same hardware. . SQLServer Stores session state in a SQL Server dat abase. the ASP. c. e.

asmx to wwwroot and type http://localhost/calc. Implementing a Web Service Client Ans 4 Of A Testing A Web Service How do you test an ASMX Web service? Simple: just call it in your browser.asmx in your browser’s address bar. The XML retu rned by the Web method appears in a separate browser window .asmx. complete with the descriptions spelled out in the WebMethod attributes. Underneath is a list of W eb methods that the service exposes. I t even generates an HTML form that you can use to call the Add method with your choice of inputs.Net: a. Type 2 and 2 into the ―a and ―b boxes and click Invoke. What happened? ASP.asmx by generating an H TML page that describes the Web service. ASP.7).6.asmx as seen in Internet Explorer Click ―Add near the top of the page. copy Calc. To de monstrate.NET knows the method name and signature because it reads them from the metadata in the DLL it compiled from Calc.4. Calc.NET displays a page that you can use t o test the Add method (Figure 8. Describe the following with respect to Web Services in . You’ll be greeted with the screen shown in Figure 8. and ASP. The name and description in the ASMX fi le’s WebService attribute appear at the top of the page. Writing and T esting a Web Service b.NET responded to the HTTP request for Calc.

Test page for the Add method .

Don’t worry about the details just yet.NET Framework simply b y pointing your browser to it. it’s time to learn about We b service clients – that is.NET generates on the fly from ASMX files enable you to test t he Web services that you write without writing special clients to test them with . It’s ea sy to write Web services. But do notice how much you can learn a bout TerraService simply by viewing the page that ASP. applications that use. Ans 4 Of B Testing A Web Service Implementing Web Service Clients Now that you’ve seen Web services up close and personal. an ultra-cool Web service that provides a programmatic int erface to a massive database of geographic data known as the Microsoft TerraServ er. Web methods. or consume. thanks to some high-level support lent by the generated for it. For kicks. They also let you explore a Web service built with the .asmx That’s the URL of the Mi crosoft TerraService.NET Framework class library (FCL) and a cod e-generator named Wsdl. type the following URL into your brows er’s address bar: http://terraservice. If you have a WSDL contract describing a Web service (or the URL of a DISCO file that . Writing Web service clients is even easier.XML returned by the Add method The forms that ASP. you’ll be using TerraService to build a Web service client later in this chapter.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.