Thursday, January 8, 2015

Page Life Cycle in Asp.Net

When a request is sent to the  server, the following events will fire
 Below is the Order of the Events.

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

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

protected void Page_InitComplete(object sender, EventArgs e)
 {


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

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

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

}


MethodsDescription
Page_PreInitBefore page Initialization
Page_InitPage Initialization
LoadViewStateView State Loading
LoadPostDataPostback Data Processing
Page_LoadPage Loading
RaisePostDataChangedEventPostBack Change Notification
RaisePostBackEventPostBack Event Handling
Page_PreRenderPage Pre Rendering Phase
SaveViewStateView State Saving
Page_RenderPage Rendering
Page_UnloadPage Unloading





Output:
PreInit 
In this event we can access to master pages and themes. 
You can dynamically set the values of master pages and themes in this event. 
You can also dynamically create controls in this event.

Note:
Set a master page dynamically.
Set the Theme property dynamically.

protected void Page_PreInit(object sender, EventArgs e)


Example:

Page.MasterPageFile = "~/MasterPage.master";
TextBox masterTextBox = (TextBox)Page.Master.FindControl("txtName");
Label mastLabel = (Label)Page.Master.FindControl("lblErrorMessage");

mastLabel .Text = "Hi, this is Uday";
masterTextBox .Text = "Hi, this is TextBox";


File->Add New Item
MasterPage





Write the Following Code in MasterPage Design as Follows:



Now, Add New Item





Here, I have created a new page Default5 and using the master page dynamically from code behind

Design the Page as follows:



.CS
Now, Run the Program and see the output



Now, my doubt is can i set this master page dynamically in other event. 
So, i tried to set this master page in other event after Page_PreInit event, so i am getting an error  as


Now, i am copying the same code and pasting in the next event which is

protected void Page_Init(object sender, EventArgs e)


Now, i am getting an error as follows.




Init 
This event fires after each control has been initialized.
You can use this event to change initialization values for controls. 
we can use this to  read the property of each initialized control .
We can Set Unique Id  and skin properties of the controls.
We will not get the post back values of the controls. 

protected void Page_PreInit(object sender, EventArgs e)

Example:

Suppose you have entered some text in a textbox control and clicking on a button control that makes a postback, now you want to view the value that you have entered. In such case you will be able to see the textbox value which you’ve entered.


.cs page


Output(Runing  Mode)





Now, i Enter some text and click on Button 



Example 2


.cs Page



OUTPUT:



On Button Click Debugging the Code to see the View State Values





Explanation:
Here on Button Click i am saving the value ViewState["a"]
and when i try to access the value in Page_Init, it does not display the VALUE From ViewState


So in Init we will not get the Post Back Values 

InitComplete

Initialization process is completed in this section.
Tracking of View State can be done in this event.
Till now the viewstate values are not yet loaded, We can make changes to ViewState in this event.


protected void Page_InitComplete(object sender, EventArgs e)


PreLoad :
View State data is visible after postback also.
This event will always Fires after the InitComplete Event

From here  viewstate functionality starts  and we can retrieving their values. 


.cs Page

Run the program, and enter any value in text box and click on button click, now, the view state has a value, Now again click on the button and see the output in debugging mode as follows


Clicking on the Button for the second time to view the values in Debugging mode

In PreLoad



In which event controls get loaded
In, Page load event all controls are fully loaded. But Controls can be accessed in Page_Init events but you will see that view state is not fully loaded during this event.


Load


We can validate Page.
You can create dynamic controls in this method.

Controls Events
This event is used to handle control events such as a Button control’s Click event or a TextBox control’s TextChanged event.

If you have a control like button, and you clicked on the Button,so after the Page_Load event your Button_Click event will fire.

Example



Output:
After Button Click


LoadComplete :
The loading process is completed,  page validation takes place. 

 protected void Page_LoadComplete(object sender, EventArgs e)
    {
        lblmessage.Text += "Page_LoadComplete<br/>";

    }

OnPreRender
Here we  can have last changes to the page or its control.
This event takes place before saving ViewState, so any changes made here are saved.
For example: After completion of this event , we cannot change any property of a button or change any viewstate value

Use the event to make last changes to the contents of the page or its controls.

Any change will be stored on the page.

protected void Page_PreRender(object sender, EventArgs e)

PreRenderComplete . 
 this event is the completion of the pre-rendering phase.

SaveStateComplete 
The next processed method is SaveViewState(). This method saves the updated Viewstate to be processed on the next page. The final Viewstate is encoded to the _viewstate hidden field on the page during the page render.

Before this event occurs, ViewState has been saved for the page and for all controls.

Any changes to the page or controls at this point will be not done.

If you change the server control then state will not be available in next Post Backs.


In this Event Method ,You can do the changes in server control also


Unload().

It is the last phase of the page life cycle. . Once this method is completed, the HTML is sent to the browser for client side processing.
we can use this event to cleanup code.

Cleanup can be performed on:
Instances of classes
Closing database connections.

in this phase the page and its controls have been rendered, so you cannot make further changes to the response stream.


protected void Page_UnLoad(object sender, EventArgs e)



.cs page





Output
]
In, the above output i cannot see the value of 

 lblmessage.Text += "Unload <br/>";

because page has been already rendered, so any changes made here will not effect.


Example




.cs page

protected void Page_PreInit(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "PreInit";
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "Init";
    }

    protected void Page_InitComplete(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "InitComplete";
    }

    protected override void OnPreLoad(EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "PreLoad";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "Load";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "btnSubmit_Click";
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "LoadComplete";
    }

    protected override void OnPreRender(EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "PreRender";
    }

    protected override void OnSaveStateComplete(EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "SaveStateComplete";
    }

    protected void Page_UnLoad(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "UnLoad";
    }



output:


After Button Click


Now, i want to disable the ViewState and check the output.

Example



.cs page

protected void Page_PreInit(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "PreInit";
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "Init";
    }

    protected void Page_InitComplete(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "InitComplete";
    }

    protected override void OnPreLoad(EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "PreLoad";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "Load";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "btnSubmit_Click";
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "LoadComplete";
    }

    protected override void OnPreRender(EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "PreRender";
    }

    protected override void OnSaveStateComplete(EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "SaveStateComplete";
    }

    protected void Page_UnLoad(object sender, EventArgs e)
    {
        lblmessage.Text = lblmessage.Text + "<br/>" + "UnLoad";
    }


Output
After Button Click 

Example



.cs Page

protected void Page_PreInit(object sender, EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreInit";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected void Page_Init(object sender, EventArgs e)
    {
       
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "Init";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected void Page_InitComplete(object sender, EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "InitComplete";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected override void OnPreLoad(EventArgs e)
    {
       // Here ,we will always have post back data, in ViewState. 
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreLoad";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "Load";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "btnSubmit_Click";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "LoadComplete";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected override void OnPreRender(EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreRender";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected override void OnSaveStateComplete(EventArgs e)
    {
        //Here view state values will not be available during post back.
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "SaveStateComplete";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }

    protected void Page_UnLoad(object sender, EventArgs e)
    {
        ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "UnLoad";
        lblmessage.Text = Convert.ToString(ViewState["value"]);
    }


Output:




After Button CLick


Wednesday, January 7, 2015

Difference Between Constant,ReadOnly,Readonly Static KeyWord

Const(constant) : Here value is constant but at compile time only , it is  must to assign a value to it. By default a const is static and we cannot change the value of a const variable throughout the entire program.

Note:Constants should be assigned a value at the time of the variable declaration and hence are known at compile time.


Hence, constants are immutable values 
We cannot change their values.

Constants can 
public, 
private, 
protected, 
internal, 
protected internal 

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            const string co = "abc";
            Console.WriteLine(co);
            Console.ReadKey();
        }
    }

}

Output:





Example


In, the above Example, when i am trying to assign the value to X, it it say Errors

Read Only 
Readonly: Whose value we can change during runtime or we can assign it at run time but only through the non-static constructor.

Note: We can assign value to it only throught Non Static Constructor only.



Explanation:

In the above Example:
I am assigning the value by using the constructor only (non static constructor)  for the (public  readonly int X1;)


Here readonly int X1 is not static , so we can assign the value to it. But, When i am trying to assign the value to the variable        (public static readonly int X = 10;) I am getting the following error.





Note:
1 When i try to initialize the value in the static constructor than It gives me an error. 
2 When i try to initialize in the method, It gives me an error.

Example 2:

Output: 10

Example:



output:


Remember
Constants:
It can be assigned values only at the time of declaration

Have to be accessed using "Classname.VariableName"

Compile Time

Run Time
It can be assigned values either at runtime or at the time of instance initialization via constructor

Read only variables have to be accessed using the "InstanceName.VariableName"

Run time.


Static ReadOnly: It value can be assigned at runtime or assigned at compile time and we can change at runtime. 

Note: But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime.




Output:



Difference Between Response.Redirect, Response.RedirectParmanent

It is the new feature Introducted in .NET 4.0.

Response.RedirectParmanent() is a function introduced in .NET 4.0.


503 - Server is temporary unavailable.

Response.Redirect - Returns 302 
RedirectParmanent - Returns 301.

If you use the Response.Redirect() to redirect from Page 1 to Page 2, 

search engines will keep Page 1 in their index since the Response.Redirect() indicates a temporary redirect. 

In case the original page (Page 1) no longer exists, and Page 2 

should replace Page 1, you need to indicate it with a Response Code 301, thus the Response.RedirectPermanent() method(It redirects permanently to another location. Search engines (such as Google and Bing) will change their indexes to point to the new page directly. ).



Server.Transfer: Server.Transfer will transfer request from Page1.aspx to Page2.aspx and return HTTP response status code 200 (Page found). 

Note:
Response.Redirect() : Search Engine will take this redirection as Temporary(Status 301) and always keep Page1 in its cache.

Response.RedirectParmanent() : Search Engine will take this a permanent redirection(Status 302) and will remove Page1 from its database and include Page2 for better performance on search.

Server.Transfer:
when you are thinking of keeping the page for ever, and to let search engine unaware of this redirection.

Conclusion


Example


You have a page, and which is included to search engine for longer days, if we use Response.Redirect() it will not change this effect to the search engine(taking this a temporary change), while if you use Response.RedirectParmanent() it will take it as permanent.

Tuesday, January 6, 2015

How to Install Web Service in Live Server

Steps: 1 Open the command prompt(Run as Administrator  and go to the path

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319




Note: Chose the.NET framework you are using(v4.0.30319).


Skip step 1 if using the visual studio command prompt

Step: 2 Run the installutil command 

C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil "ServiceName"



And Press Enter


Step: 3 Navigate to Services using (Run -> services.msc), search for your service and start it.



In, the following list of all the services, select your service Name, and click on Start Button








Again to uninstall the service, using the command mentioned in step 2 with /u switch.

C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil /u "ServiceName"


To delete 
 sc delete ServiceName






Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...