Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday 31 December 2015

Which page is called first in asp.net c#? Web form or Master page?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imagination hunt to see latest Blogs


Which page is called first in asp.net c#? Web form or Master page? 

Second Scenario: We take the same pages (master and web form), we are working with case one and write the below code in the page load for both pages.

Code that we write on master page

Practical Implementation:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Master Page Loaded<br/>" + "<br/>");
    }
}

And, Code that we write on web form page

Practical Implementation:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Content Page Loaded" + "<br/>");
    }
}

Now, the question arises
Which page load is called first? Is it the Default.aspx.cs or MasterPage.master.cs?
Let’s run and check.



OUTPUT:

Reason explanation: As per the event sequence for pages – Web Form page control load before master page control. That’s why Content Page loads first than Master Page. 


Related Questions:


Q-1 How can I first execute page load of the master page?
Ans. It’s not possible to change the order of the events.
But, there’s a way by which you can place the desired result. Placing the master page code within the Page_Init event instead of Page_Load has the desired effect to firing before the child page Page_Load event.

Q-2 Why page load of web form load first then the master page load event?
Ans. As per the event sequence for pages – web form page control load before master page control. That’s why web form page loads first then master page.

Click imagination hunt to see latest blogs

Keep learning and sharing…
Read More »

Which page is called first in asp.net c#? Web form or Master page?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imagination hunt to see latest Blogs


Which page is called first in asp.net c#? Web form or Master page?

One such scenario is - Suppose you have a master page and a nested web form page. In your master page, you write some piece of code. Like, you add a label control and write its Text property as “Master Page Load” with Red color.
Note: We haven’t written any code in the code-behind for any page (neither in master nor in web form page).

Code that we write on the master page.

Practical Implementation:

<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Master Page Load" ForeColor="Red"></asp:Label>
            <br />
            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

In the same manner, you add a label control and write its Text property as “Content Page Load” with Red color. 

Code that we write on web form page

Practical Implementation:

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Content Page Load" ForeColor="Red"></asp:Label>
    <br />
</asp:Content>

Now, the question arises
Which page label control is called first? Is it the Default.aspx or MasterPage.master?
Let’s run and check.



Output:

We got the output as master page label called first and content page label called after master page.
Reason explanation: We have taken server control. And server control renders. And as per the event sequence of pages – Web Form page control load after Master Page control. That’s why Master Page Control Render first than Content Page.


Related Questions:


Q-1 Which of the following is an ordered collection class?
1) Map
2) Stack
3) Queue
4) BitArray
5) HashTable

(A) 1 only
(B) All the above
(C) 2 and 3 only
(D) 2 and 5 only
(E) 4 and 5 only

Ans. Option(C)

Q-2 What is the base class from which all Web forms inherit?
A) Page Class
B) Session Class
C) Master Page
D) None of the Above

Ans. Option(A)

Click imagination hunt to see latest blogs

Keep learning and sharing…
Read More »

Wednesday 30 December 2015

Which page is called first in asp.net c#? WebForm or Master page?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imagination hunt to see latest Blogs


Which page is called first in asp.net c#? Web form or Master page?

This question is faced by every developer when they start working on Master Page.
And we start searching help from Google. And everyone ends up in the blog posted by Microsoft at https://msdn.microsoft.com/en-us/library/dct97kc3.aspx

This says the following is the sequence in which events occur when a master page is merged with a web form page:
Webform page PreInit event.
Master page controls Init event.
Webform controls Init event.
Master page Init event.
Webform page Init event.
Webform page Load event.
Master page Load event.
Master page controls Load event.
Webform page controls Load event.
Webform page PreRender event.
Master page PreRender event.
Master page controls PreRender event.
Webform page controls PreRender event.
Master page controls Unload event.
Webform page controls Unload event.
Master page Unload event.
Webform page Unload event

This sequence clarifies everything.

A Master page event called before web form events except the page load event and PreRender event.

But one question arises when did we actually see this scenario?
There are two different scenarios when you actually see this. The output also differs according to each scenario:


Related Questions:


Q-1 What will be the output of the program?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        void volume(int height, int length, int width)
        {
            volume = width * height * length;
        }
    }
    class Prameterized_method
    {
        public static void main(String[] args)
        {
            box obj = new box();
            obj.height = 1;
            obj.length = 5;
            obj.width = 5;
            obj.volume(3, 2, 1);
            Console.WriteLine(obj.volume);
            Console.ReadLine();
        }
    }

A) 1
B) 0
C) 6
D) 25

Ans.
Option(C)

Q-2 What will be the output of the given code snippet?

    class Program
    {
        static void Main(string[] args)
        {
            int x, y = 1;
            x = 10;
            if (x != 10 && x / Convert.ToInt32(0) == 0)
                Console.WriteLine(y);
            else
                Console.WriteLine(++y);
            Console.ReadLine();
        }
    }

A) 2
B) 1
C) Compile time error
D) Run time error

Ans.
Option(A).
Explanation: Both conditions for if statements are failed and hence statement after else is executed.

Click imagination hunt to see latest blogs

Keep learning and sharing…
Read More »

Tuesday 29 December 2015

How to call .cs file from .aspx.cs file in asp.net c#?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imagination hunt to see latest Blogs


Calling .cs (class file) from .aspx.cs (webform code behind file) in Asp.net C#

We have already prepared our web form and validation class logic in our previous two blogs.
Inference from our previous blogs, all we need is to call our validation class method into our web form.

For that, we have to call the validate.cs file method in 
calling_cs_page_from_aspxcs.aspx.cs web form file. And this calling need to be done on click of button event.

Practical Implementation:

Code Behind code:

using System;    -----> Namespace

public partial class calling_cs_page_from_aspxcs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //using and creating the object of validation.cs class
        Validation isValid = new Validation(); //isValid-> object of Validation class

/* Here, isValid (object of Validation class) has a method checkName which is taking the name. So, we need to pass that name (entered from textbox) onto the method parameter. And also this method (checkName) is returning output in string. This means, we need to store the output in string. So, we take string variable 'msg' to store the result. */
      
        string msg = isValid.checkName(txtName.Text);

        if (msg.Trim().Length != 0)
        {
            lblmsg.Text = msg;
            lblmsg.Visible = true;
        }
        else
        {
            lblmsg.Text = "Name validate successfully.";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            lblmsg.Visible = true;
        }
    }
}


Press Ctrl+Shift+B. As the output, message shows build succeeded. Press Ctrl+F5. It will Run the website locally.
  
You can check all the three cases:

A) Case 1: When we do not enter anything in the name text box. And click on the submit button. We got a label message - which says - "Enter Name". One more thing to note down is the message is in red color, implies it is a validation alert message, means this field is required, we cannot submit a blank name in  the 'Name' field.



B) Case 2: When we input more than 15 characters in the name text box. And click on the submit button. We again got a label message - which says - "Name length cannot be greater than 15 characters" in Red color. Again an alert message.



C) Case 3: When we input the appropriate name with less than 15 characters. It is accepted. Hence, giving a success message in green color.



This demo shows - how we can call a function from class (.cs) file present in some different location into a web form (.aspx.cs) which is also present in some different location.


Related Questions:


Q-1 What will be the correct output for the given code snippet?
  
class maths
 {
     int fact(int n)
     {
         int result;
         if (n == 1)
         return 1;
         result = fact(n - 1) * n;
         return result;
     }
 }
 class Output
 {
     static void main(String args[])
     {
         maths obj = new maths() ;
         Console.WriteLine(obj.fact(4)*obj.fact(2));
     }
 }

A) 60
B) 64
C) 120
D) 48

Ans. Option(D)
Explanation- 4! = 4*3*2*1 & 2! = 2*1 .So, 24*2 = 48.
Output- 48

Q-2 What is output of the code?

static void Main(string[] args)
{
    m();
    Mul();
    Console.ReadLine();
}
static void Mul()
{
    Console.WriteLine("4");
}
static void m()
{
    Console.WriteLine("3");
    Mul();
}

A) 4 3 4
B) 4 4 3
C) 4 3 3
D) 3 4 4

Ans. Option(D)
Explanation- First m() will be executed to print the number ‘3’ and at last mentioned function Mul() will be executed to print the statement 4. Second, Mul() will be executed to print the number ‘4’ to return the output as 3 4 4.
Output- 4 3 4.

Click imagination hunt to see latest Blogs

Keep learning and sharing…
Read More »

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...