Wednesday 30 October 2013

Properties in asp.net and their uses

Properties and their uses 

Properties get and set values. The C# language provides them as a convenient way to simplify syntax. They are implemented as methods .They act as the standard way of conversation with the class. We should always work by implementing properties. Because this also increases encapsulation.

Encapsulate means to hide. Encapsulation is also called data hiding.We can think Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from outside world .Properties are the best method to encapsulate our data . With properties , our user of the class will be interacting with our class by means of our properties only.

These are different from fields(variables) as these will not require any memory to store data but these will point to the variable values .


Given below is an example to demonstrate the use of properties :

using System;
class MyClass
{
    int _myProperty;
    public int VariableValue
    {
       get
       {
         return this._myProperty;
       }
       set
       {
         this._myProperty= value;
       }
    }
}

class MainClass
{
    static void Main()
    {
      MyClass obj = new MyClass();
      obj.VariableValue = 1000; // setting the property value
      Console.WriteLine(obj.VariableValue); 
        // getting the property value 
    }
}
Result  : 1000
Properties can be of other data types also . For example : Boolean, string etc. 
Run the  code and enjoy object oriented programming by using properties .

Happy Coding!

Thursday 24 October 2013

Concept of User Controls In ASP.NET

Usercontrols In ASP.NET

A usercontrol is a reusable part of page. If we want to use a particular section on different pages of website , we use the usercontrols. User-Controls increases the re-usability of the same code. A simplest example of usercontrol is Login-functionality provided by websites or it could be simply a Left Panel appearing on a website.

Simply , we need to write the code in a separate user-control file and then we can easily use that on any page we want by simply registering the user control on that page. A big advantage of the UserControl is that it can be cached, using the OutputCache functionality. We will learn in the upcoming tutorials ,how we can store our data in cache.
We can cache our complete page or even a user-control.

To create a user control , right click on the project and select Add New Item and then choose web user control from the list of possible things to add. You should now have a MyLeftPanel ascx and a MyLeftPanel ascx.cs in your project. The first is where we put our markup, and the second is our CodeBehind file. Now, if MyLeftPanel ascx is not already open and selected, do so now. You will see only one line of code, the UserControl declaration. As mentioned, this control will be displaying information about a user, so let's get started adding some markup to do so.

For example , if we have to display some data in our left panel , we will use this user control as 

<%@ Control Language="C#" AutoEventWireup="true" Codebehind="MyLeftPanel.ascx.cs" Inherits="MyWeb.UserControls.LeftPanel" EnableViewState="false" %>

<div id="userName"><%=userName%></div> //links that we want in our left panel
<div id="userCity"><%=userCity%></div>
    
<script language="javascript" type="text/javascript">
var userName = '<%=userName%>';
 function jsFunction() {
 alert(userName);
 }
</script>      


I have written a javascript function here to elaborate that we can also use javascript and jquery functions in our user control .

On ascx.cs file we can use these as properties 

protected string userName;
protected string userCity;

userName = MyClass.userName;

Now how to use this user control in our project,
Pick a page in project, or simply create a new one for the purpose, and open it. The first thing we have to do, is declare our UserControl. It can be done either in each page where it's used, or globally in the web.config file. 
There is no performance difference , but when declaring UserControls in the web.config file, the controls have to reside in a different directory than the page(s) using it. 

For now, let's just declare it within the page. Add the following line below the standard page declaration:

<%@ Register TagPrefix="My" TagName="MyLeftPanel" Src="~/MyLeftPanel.ascx" %>


Make sure that the src value matches the path to our UserControl file. Now we can use the UserControl in your page, like any other control. For instance, like this:

<My:MyLeftPanel runat="server" ID="MyLeftPanelControl" />

Now this user control will get rendered with page load. Enjoy.

Happy Coding!




Tuesday 22 October 2013

Concept of Server.Transfer and Response.Redirect and their differences


Concept of Server.Transfer and Response.Redirect


Basically both of these are used when we want to make switch between two pages.


1) Server.Transfer : 

This method is used to transfer user from one page to other page without generating a new HTTP request . Thus this will lead to lesser roundtrip time (RoundTrip time is the time for the request sent and response received), because in this case user is moved to the next page at server side rather than making request first at client side and sending this request again to server to get the new page as is done in case of Reponse.Redirect .


                          Working of Server.transer is demonstrated in the diagram above



2) Response.Redirect :

 Response.redirect sends HTTP code 302 down to the users browser along with the url location of the requested page . HTTP code 302 actually means 'The requested resource resides under a different URI' . Thus simply , it can be said that Reponse.Redirect initiates another request to the server , but this is not the case with Server.Transfer because in that case , the original request is simply rewritten and is transferred to some other page on the server.


               Working of Response.Redirect is demonstrated in the diagram above


Differences among the both :


    Response.Redirect should be used when:
   
    we need to redirect the request to some other web server.
    we don't care about causing additional roundtrips to the server on each request.
    we do not need to preserve Query String and Form Variables from the original request.
    we need our users to be able to see the new redirected URL where he is redirected in  his browser.
   
    Server.Transfer should be used when:
   
    we need to transfer current page request to another page on the same server.
    we need to preserve server resources and avoid the unnecessary roundtrips to the server.
    we need to preserve Query String and Form Variables (optionally).
    we don't need to show the real URL where we redirected the request in the users Web Browser.


Happy Coding!