Web Forms Interview Preparation Guide
Download PDF

Microsoft Web Forms frequently Asked Questions in various Web Forms job Interviews by interviewer. The set of Microsoft Web Forms interview questions here ensures that you offer a perfect answer to the interview questions posed to you. Get preparation of Microsoft Web Forms job interview

11 Microsoft Web Forms Questions and Answers:

1 :: How to use QueryString in web forms?

private void Button1_Click (object sender, System.EventArgs e)
{
string url;
url="anotherwebform.aspx?name=" +
TextBox1.Text + "&email=" + TextBox2.Text;
Response.Redirect(url);
}

Destination web form

private void Page_Load (object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["email"];
}

2 :: How to use Sessions in web forms?

private void Button1_Click(object sender, System.EventArgs e)
{
//Drag TextBox1 and TextBox2 onto a web form
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;
Server.Transfer("anotherwebform.aspx");
}

Destination web form

private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text=Session["name"].ToString();
Label2.Text=Session["email"].ToString();
Session.Remove("name");
Session.Remove("email");
}

3 :: How to use Context?

//Page1.aspx stores value in context before transferring
Context.Items("UserName") = txtName.Text;
Server.Transfer("Page2.aspx");

//Page2.aspx retrieves the value from Page1’s context
string sName;
sName = Context.Items("UserName").ToString;
Response.Write("Your name is " + sName);

4 :: How to use PreviousPage on web forms?

FirstForm.aspx

<asp:Button id="buttonPassValue" runat="server" Text="Button" PostBackUrl="~/SecondForm.aspx">
</asp:Button>

SecondForm.aspx.cs

TextBox1.Text = Request.Form["TextBox1"].ToString();

Or SecondForm.aspx.cs

TextBox textBoxTemp = (TextBox) PreviousPage.FindControl("TextBox1");
TextBox1.Text = textBoxTemp.Text;

As you’ve noticed, this is a simple and clean implementation of passing values between pages.

5 :: How to use Server.Transfer in dot net?

page1.aspx,

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button_Click(object sender, EventArgs e)
{
Server.Transfer("page2.aspx", true);

}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>



page2.aspx,

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Form["TextBox1"]);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

6 :: How to upload a file on web forms?

if (FileUpLoad1.HasFile)
{
FileUpLoad1.SaveAs(Server.MapPath("upload")+ "\\" + FileUpLoad1.FileName);
}

7 :: How to upload an image files only in .net web forms?

See the code,

Fileupload.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Fileupload.aspx.cs" Inherits="FileuploadDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload Image Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>

Fileupload.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class FileuploadDemo : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string fileFullname = this.FileUpload1.PostedFile.FileName;
string dataName = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
if (FileUpload1.PostedFile.ContentType.ToUpper().IndexOf("IMAGE") > -1)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;

if (Width > 1000 || Height > 1000 || FileUpload1.PostedFile.ContentLength > 1024 * 1024 * 200)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('The image size is too large!');</script>");
}
else
{
if (type == "jpg" || type == "gif" || type == "bmp" || type == "JPG" || type == "GIF")
{
string ImagePath = "images/";
string sPath = Server.MapPath(ImagePath) + dataName + fileName;
string imgPath = ImagePath + dataName + fileName;
this.FileUpload1.PostedFile.SaveAs(sPath);
this.ClientScript.RegisterStartupScript(this.GetType(),
"Startup", "<script language='javascript'>alert('Success!');</script>");
this.Image1.ImageUrl = imgPath;
this.btnSubmit.Enabled = false;
this.btnSubmit.Text = "Success!";
this.btnSubmit.Enabled = true;

}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('File type is not right!');</script>");
}
}
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('The uploaded file is not a image file!');</script>");
}
}
}

8 :: How to select multiple non-sequential dates at Code-Behind in Microsoft web forms?

Invoke the member function ‘Add’ of the control's SelectedDates collection. You can add dates in any sequence, because the collection will automatically arrange them in order for you.

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Calendar1.SelectedDates.Clear();
Calendar1.SelectedDates.Add(new DateTime(2008, 8, 1));
Calendar1.SelectedDates.Add(new DateTime(2008, 8, 7));
Calendar1.SelectedDates.Add(new DateTime(2008, 8, 15));
}

9 :: How to disable some dates in Calendar control in web forms?

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
string date="02/01/2008";
DateTime dt = DateTime.Parse(date);
if (e.Day.Date == dt)
e.Day.IsSelectable = false;
}

10 :: How to access a control inside a UserControl in .net?

Assume there is a user control called UC and there is only a TextBox control inside it. Now drag this user control into a web page, you can use the following code to visit the TextBox control.

((TextBox)UC1.FindControl("TextBox1")).Text = "demo";