Monday 30 September 2013

Inline and Code behind technique and their differences

Inline and Code behind Technique 

Hello friends , many a times we came across two techniques of coding . These are :
1) Inline Technique.
2) Code-behind Technique.

 Classic ASP uses Inline coding technique as is used in case of php , and now a days in case of Razor(MVC).

First of all , let us discuss each technique individually and how we can implement our logic using these techniques and then I will discuss the differences among the both.

Inline Technique 

As the name indicates , Inline technique places the code on the page itself . That is , it allows the code to be written along with the HTML using the <script></script> tag.

For using inline-coding technique in our ASP.net page , we need to follow the steps written below :

 Step 1:  

Create a blank website and then choose Add New Item and then choose web form and uncheck the checkbox  Place Code in Separate File  as shown below.
I am using c# as the development language .


 Step 2:  

In this example , I will use a text box and a button , and on click of Submit button Current date time would be displayed in the text box.
For inline technique , the code would be written inside the <script></script> tags as shown in the screenshot below.


Code Behind Technique

In this technique , code for an ASP.net web page is written in a separate class file from which our page inherits.

For using code-behind technique , follow these simple steps :

Step 1:

Create a blank website and then choose Add New Item and then choose web form as shown in screenshot above but this time the checkbox  Place Code in Separate File  should be checked.

Step 2:
 
Similarly I have taken a text box and a button again. Now when I press F7 or double click the submit button , code page will get opened having extension .aspx.cs . Write the code there as shown in the screenshot.

Now run the code .

Same output will be there for both the techniques as shown in the screenshot below.


Differences among both techniques:

Code behind technique is better than inline coding technique.

Because , first it separates the code from the design, thus avoiding the confusion .
second , in case of code-behind technique, the code is written in separate file which is placed on the server in the form of compiled dlls , In the upcoming tutorials, I will also be discussing "how to host your website" , Now the dlls are faster to execute as compared to inline coding , in which the code is compiled each time when the page refreshes , because the code is there on the aspx page.

 
Happy Coding!




Thursday 26 September 2013

Concept of Postback in .Net

Today I will introduce you to the concept of PostBack in ASP.NET. PostBack is the name given to the process of submitting an ASP.NET page to the server for processing . PostBack is done if certain credentials of the page are to be checked against a database (such as verification of username and password). This is something that a client machine is not able to accomplish and thus these details have to be ‘posted back’ to the server.

A simple example to illustrate the usage of PostBack is a login page. After the user has typed his username and password, he clicks on the ‘Login’ button. Upon the click, the page is sent to the server to check against the database/XML file to check if the user with supplied details is an authenticated user.Then there arise certain events ( Listbox Index Changed,RadioButton Checked etc..) in an ASP.NET page upon which a PostBack might be needed. 

Suppose a case is there , If we have two dropdown lists .One dropdown list is there to select the country of the user and the other is there for his/her state/province.
Now we need to bind the second dropdown on click of an item from the first dropdown of country. This can be easily done using the concept of postback. We can set the autopostback property of dropdown to true and on click of an item of it , request will be made to the server. The autopost-back property can be selected from the properties list as shown in the screenshot below :


Also postback control is really helpful in case if we need to do some tasks on our page for the very first time it gets loaded. For e.g. If I need to display date on my web page and I do not want it to get refreshed with every postback (say if I click on Submit button and I do not want my Date to be refreshed) then I can use postback.



IsPostBack is a Boolean property of a page which is set (=true) when a page is first loaded. Thus, the first time that the page loads the IsPostBack flag is false and for subsequent PostBacks, it is true. An important point to be noted here is that each time a PostBack occurs, the entire page including the Page_Load is ‘posted back‘ and executed. Thus a very common programming practice is that whenever some part of the code is to be executed just the first time a page loads, it is checked against the IsPostBack flag as is done in the screenshot above. If you have not understood the concept clearly, do not worry. Programming examples in subsequent posts will help making this concept clear.

Happy Coding!

Tuesday 24 September 2013

Hello guys , this is my first tutorial . On this blog , I would be discussing my experiences and code snippets that I have came across till date.

Before starting ASP.net , One must have knowledge of what actually ASP.net is .

ASP stands for Active Server Pages. Before ASP.net , the technology used by Microsoft was ASP for web development.

One must also have knowledge of some other killer languages like Javascript , which is widely used with ASP.net for applying client side validations. Javascript can also be used for handling Server-Side dealings , but mostly it is used for Client-Side purposes.

Let us discuss a very basic program of Javascript. In this program , I will be using javascript for making an alert message on the click  of a div of HTML.


<html>
<head>
<title>Demo alert program</title>
</head>
<body>
    <h1>This is the demo of alert message in Javascript.</h1>
    <div id="myDiv" onclick = "alrtMessage(this.id)">Click me </div>
</body>
</html>


<script type="text/javascript">
    function alrtMessage(id)
    {
         alert("You have clicked "+id);
    }
</script>   


Run the above code , and have a alert message there.

Happy Coding!