In today's article you will learn the differences between Code Behind and Inline Code in ASP.NET.
Many people remain confused about the differences between Code Behind and Inline Code. Here I am explaining both of them with examples that will help you to understand the differences between the two.
Code Behind refers to the code for an ASP.NET Web page that is written in a separate class file that can have the extension of .aspx.cs or .aspx.vb depending on the language used. Here the code is compiled into a separate class from which the .aspx file derives. You can write the code in a separate .cs or .vb code file for each .aspx page. One major point of Code Behind is that the code for all the Web pages is compiled into a DLL file that allows the web pages to be hosted free from any Inline Server Code.
Inline Code refers to the code that is written inside an ASP.NET Web Page that has an extension of .aspx. It allows the code to be written along with the HTML source code using a <Script> tag. It's major point is that since it's physically in the .aspx file it's deployed with the Web Form page whenever the Web Page is deployed.
Now I will show you these differences by using an example.
First of all create a new Blank Website in the Visual Studio, then add a Web page to it. Here we are first creating a Web page for the Code Behind so remember one thing "the check box should be checked while adding this page". In other words check the "Place the code in a separate file" and then click on the "Add" button.
Now on the .aspx page use a Button, a Link and a Text Box.
Now double-click on the Button, this will use the Code window. Now you will see that this coding section is opened in a separate window whose extension is .aspx.cs. Write the code in this window. I wrote the code such that whatever I wrote in the TextBox will also appear in the Label.
Now debug this page and verify that your program is running.
Until now we were working on the Code Behind but now we will work on the Inline Code, for that add another web page to your Web Site. But this time things are different, this time don't check the check box and if it's checked then Uncheck it and then Click on "Add".
Now on this new .aspx page again use a Button, a Link and a Text Box.
Now double-click on the Button so that you can write the code on click of this Button. But Now No New Window will be opened, Now the coding will appear on the same .aspx page. Here no .aspx.cs page is available.
Write the same code here also and then debug it.
As you can see it's giving the same output as the Code Behind gave but even after that is different from the Code Behind.