0% found this document useful (0 votes)
74 views

Final Theory Paper

The document provides code snippets to demonstrate various tasks using ASP.NET MVC and Web Forms including defining routes, validating form fields, passing data between pages, and using multicast delegates. For ASP.NET MVC, it shows how to define a route with a numeric ID constraint. For Web Forms, it shows how to: 1) Maintain view state for controls on multiple pages, 2) Validate form fields are not empty and match between textboxes, 3) Pass textbox values between pages as query strings and display in controls, 4) Perform the same task using code behind. It also demonstrates multicast delegates to call multiple methods.

Uploaded by

Arslan Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Final Theory Paper

The document provides code snippets to demonstrate various tasks using ASP.NET MVC and Web Forms including defining routes, validating form fields, passing data between pages, and using multicast delegates. For ASP.NET MVC, it shows how to define a route with a numeric ID constraint. For Web Forms, it shows how to: 1) Maintain view state for controls on multiple pages, 2) Validate form fields are not empty and match between textboxes, 3) Pass textbox values between pages as query strings and display in controls, 4) Perform the same task using code behind. It also demonstrates multicast delegates to call multiple methods.

Uploaded by

Arslan Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

FINAL PAPER

WEB THEORY

ARSLAN ABDUL RAUF 18-SE-95 THEORY


Q#1. Do the following tasks using ASP.net MVC? Give code where necessary?

A. For the URL http://localhost/student?Id=123 ,define a route in RouteConfig.cs, keeping in


view the Id value must be numeric?

Answer:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Student",
url: "student/{id}",
defaults: new { controller = "Student", action = "Index"},
constraints: new { id = @"\d+" }

);
}
}

PART_B
Answer:
For debug i will run the application and the response that i'll get from browser as a result will guide
me what actually the error is. Error may be because of the action method does not exists or may be
because of the reason that action method defined is of post request type or the value specified in the
action is not the same as in the given route. The other possible error was 404 which would have been
thrown by the browser if the value in the controller was incorrect.

C) Answer:
Action Methods and Normal Methods:
The methods in controller class are called actions methods and they must be:
i) Public Methods, no protected or private method.
ii) Not overloaded
iii) Static method.
Normal methods in C#
Explanation:
All the public methods of the controller class are called Action methods.
• A controller action cannot be a static method while a normal C# methods can
Be a static.
• A method used as a controller action cannot be overloaded while normal C#
Methods can be overloaded.
• A controller action must be a public method of a controller class. While
Normal methods of C# can be public or private
D) Answer:
Action method is used in controller class for processing of incoming requests. It typically has 1-1
mapping for user interactions. Wherever, Action Selector is an attribute which is applied to action
method. Routing engine uses this action selector to correctly select an action method for a particular
request.

PART_E
Answer:
Let's take an example of an item(drinks) in a grocery store
public class ITEM
{
public int Id { get; set; }
public string Name { get; set; }
public Type Itemtype { get; set; }
}

public enum Type


{
Alcoholic,
Non-Alcoholic
}

In Razor View
@model ITEM

@Html.DropDownListFor(d => d.ITEMType,


new SelectList(Enum.GetValues(typeof(Type))),
"Type of item")
Another EXPLANATION
Dropdownlistfor() here assigns a property to an a Itemtype.And in the next it shows a dropdown list to
select one type.
An action is a method on a controller that gets called when you enter a
Particular URL in your browser address bar while Action selector is the
Attribute that can be applied to the action methods itself.
For example ActionName is an attribute to the action methods which is used
to call the action method with the name specified in the ActionName attribute.
[ActionName("Find")]
public ActionResult GetById(int id)
{
....
}
Here action method name changes from GetById to Find.

F) Answer:
ViewData, ViewBag, TempData:
ViewData, ViewBag and TempData are used to pass data from controller to view. ViewData and
ViewBag have not much difference but TempData has more responsibilities.
ViewData is part of ViewDataDictionary class and can be accessible through strings. It requires
typecasting.

Example:
public ActionResult Index()
{
List<string> City = new List<string>();
City.Add("Islamabad");
City.Add("Karachi");
City.Add("Lahore");

ViewData["City"] = City;
return View();
}

ViewBag is a dynamic property and don’t require any typecasting.

Example:
public ActionResult Index()
{
List<string> City = new List<string>();
City.Add("Islamabad");
City.Add("Karachi");
City.Add("Lahore");

ViewBag.City= City;
return View();
}

TempData is stored data like a Live session which holds data between controllers or between actions
using session variables. It’s part of TempDataDictionary.

Example:

public ActionResult Index()


{
List<string> City = new List<string>();
City.Add("Islamabad");
City.Add("Karachi");
City.Add("Lahore");
TempData["City"] = City;
return View();
}
G. Demonstrate multicast delegates. Create a namespace, which contains two classes. In one
class define three methods. In second class call these three methods using multicast
delegates.
ANSWER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace finalpaper
{

delegate void MDelegate();


class DM
{
static public void Display()
{
Console.WriteLine("ARSLAN");
}
static public void print()
{
Console.WriteLine("ABDUL");
}
static public void rit()
{
Console.WriteLine("RAUF");
}
}
class MTest
{
public static void Main()
{
MDelegate m1 = new MDelegate(DM.Display);
MDelegate m2 = new MDelegate(DM.print);
MDelegate m3 = new MDelegate(DM.rit);

m1();
m2();
m3();
}
}
}

Output:
Question # 2
Do the following tasks using ASP.net Web Forms? Give code where
necessary. CLO3
A. Create two web pages that contain two server-side text boxes, two drop down lists and
one button. 10
B. Each control must maintain its view state information. 03

C. User should type text in text boxes of first web page. Perform two types of validation on
text boxes. First validation checks both text boxes must not be empty and second
validations checks that text in both text boxes must be the same. 10

 Required Validator
<asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtbox1"
ErrorMessage="Required"></asp:RequiredFieldValidator>

 Compare Validator
<asp:CompareValidator runat="server" ControlToValidate="txtbox2"
ControlToCompare="txtbox1" ErrorMessage="Not Matched"></asp:CompareValidator>
D. Using inline model, upon clicking submit button, text of text boxes should be added to
dropdownlists of the same page. At the same time user should be redirected to second
page, with each control value of the first page to be passed as query string. Now Each
value must be displayed in the same controls of second web page. 10
Page 1
<script runat="server">

protected void btn_Click(object sender, EventArgs e)


{

ddl1.Items.FindByValue("item1").Text=txtbox1.Text;
ddl2.Items.FindByValue("item1").Text=txtbox2.Text;
Response.Redirect("WebForm2.aspx?
firstname="+txtbox1.Text+"&lastname="+txtbox2.Text);

}
</script>
Page 2

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string firstname = Request.QueryString["firstname"];
string lastname = Request.QueryString["lastname"];
txtbox1.Text = firstname;
txtbox2.Text=lastname;
ddl1.Items.FindByValue("item1").Text = txtbox1.Text;
ddl2.Items.FindByValue("item1").Text = txtbox2.Text;
}
</script>
E. Do part D. using code behind model. 07
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFinal2
{
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btn_Click(object sender, EventArgs e)
{

ddl1.Items.FindByValue("item1").Text = txtbox1.Text;
ddl2.Items.FindByValue("item1").Text = txtbox2.Text;
Response.Redirect("WebForm2.aspx?firstname=" + txtbox1.Text + "&lastname=" +
txtbox2.Text);

}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFinal2
{
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string firstname = Request.QueryString["firstname"];
string lastname = Request.QueryString["lastname"];
txtbox1.Text = firstname;
txtbox2.Text = lastname;
ddl1.Items.FindByValue("item1").Text = txtbox1.Text;
ddl2.Items.FindByValue("item1").Text = txtbox2.Text;
}
}
}

You might also like