Final Theory Paper
Final Theory Paper
WEB THEORY
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; }
}
In Razor View
@model ITEM
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();
}
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:
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">
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;
}
}
}