Compile Code: Code Behind and Inline Coding; The Common Language Runtime

ASP.NET page એ કેવું દેખાય છે અને એ શું કરે છે એ બંનેને અલગ પાડે છે: તમે C# ને એ જ file માં inline લખી શકો, અથવા ઘણું સારું એ કે અલગ code-behind file માં લખો, અને બંને રીતે CLR એને ચાલતાં પહેલાં compile કરે છે.

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

દરેક page બે સવાલોના જવાબ આપે છે

FestConnect ના દરેક page ને બે જુદા સવાલોના જવાબ આપવા પડે છે: એ કેવું દેખાય છે (layout: મથાળાં, text boxes, Register button) અને એ શું કરે છે (logic: registration સાચવવું, વપરાશકારને આવકારવો). ASP.NET તમને આ બે બાબતોને એમની સાચી જગ્યાએ રાખવા દે છે.

Layout .aspx file માં markup રૂપે રહે છે. Logic એ C# code છે, અને એ code ક્યાં મૂકવો એની તમારી પાસે પસંદગી છે: એ જ .aspx file માં ભેળવીને (inline), કે અલગ સાથી file માં (code-behind). આ પાઠ બંનેની સરખામણી કરે છે અને બતાવે છે કે CLR બંનેમાંથી કોઈને પણ ચાલતા page માં કઈ રીતે ફેરવે છે.

Theory

Inline coding

Inline coding માં C# સીધું .aspx file ની અંદર મુકાય છે, સામાન્ય રીતે markup ની બાજુમાં <script runat="server"> વાળા block માં.

Page નું બધું જ એક file માં બેસે છે. નાના નમૂના માટે એ સગવડભર્યું છે, પણ એ layout અને logic ને ભેળવી દે છે, અને page મોટું થાય એમ એને વાંચવું તથા સંભાળવું ઝડપથી અઘરું બની જાય છે. એને એવું સમજો કે તમે તમારી શણગારની ગોઠવણ, તમારું HTML અને તમારો program એક જ લાંબી file માં લખો: પાંચ લીટી માટે ઠીક છે, પાંચસો લીટી માટે માથાનો દુખાવો.

Practical

Inline: .aspx file ની અંદર જ C#

<%@ Page Language="C#" %>
<script runat="server">
    // Logic lives in the same file as the markup
    void Greet(object sender, EventArgs e) {
        lblMessage.Text = "Welcome to FestConnect";
    }
</script>

<html>
<body>
    <form runat="server">
        <asp:Label id="lblMessage" runat="server" />
        <asp:Button Text="Greet" OnClick="Greet" runat="server" />
    </form>
</body>
</html>

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

Code-behind

Code-behind માં C# એવી અલગ file માં મુકાય છે જે page ની સાથી હોય. Default.aspx નામના page માટે code Default.aspx.cs માં રહે છે. .aspx માં ફક્ત markup રહે; .aspx.cs માં ફક્ત logic રહે. .aspx ની ટોચે આવેલો directive એ બંનેને જોડે છે.

આ પ્રમાણભૂત અને ભલામણ કરાયેલો રસ્તો છે. એ layout ને logic થી સ્વચ્છ રીતે અલગ પાડે છે, એટલે designers markup પર અને programmers code પર એકબીજા સાથે અથડાયા વગર કામ કરી શકે છે, અને દરેક file કેન્દ્રિત તથા વાંચી શકાય એવી રહે છે. FestConnect નું દરેક ખરું page code-behind વાપરે છે.

Practical

Code-behind: logic in Default.aspx.cs

using System;

// The .aspx page links here via its CodeFile and Inherits directive
public partial class Default : System.Web.UI.Page
{
    protected void Greet(object sender, EventArgs e)
    {
        lblMessage.Text = "Welcome to FestConnect";
    }
}
// The markup (labels, buttons) stays in Default.aspx, kept separate and clean.

Formula

CLR તો compile કરે જ છે

બંને રીતે તમારો page નો code compile થાય છે, લીટીએ લીટીએ interpret થતો નથી. આ એ જ Common Language Runtime (CLR) છે જે BCA404 માં હતું: તમારો C# પહેલાં MSIL (વચગાળાની ભાષા) માં compile થાય છે, પછી just-in-time રીતે native machine code માં compile થઈને ચાલે છે.

ASP.NET page ને એની પહેલી માગણી વખતે compile કરે છે અને પછી એ compile થયેલી આવૃત્તિ ફરી ફરી વાપરે છે, એટલે પહેલો ફેરો સહેજ ધીમો હોય છે અને બાકીના ઝડપી. સાર એ કે ASP.NET એ compile થતું .NET છે, અને એની સાથે આવતું બધું type-checking તથા ઝડપ પણ મળે છે.

Quiz

ASP.NET માં code-behind અને inline coding વચ્ચેનો મુખ્ય ફરક શું છે?

  1. Code-behind browser માં ચાલે છે, જ્યારે inline coding server પર ચાલે છે
  2. Code-behind C# ને .aspx ના markup થી અલગ સાથી file માં મૂકે છે, જ્યારે inline coding C# ને .aspx માં જ ભેળવે છે
  3. Inline coding ને CLR compile કરે છે, જ્યારે code-behind compile થતું જ નથી
  4. ખરેખર કોઈ ફરક નથી; બંને નામ એક જ વસ્તુ માટે છે
Show the answer

Code-behind C# ને .aspx ના markup થી અલગ સાથી file માં મૂકે છે, જ્યારે inline coding C# ને .aspx માં જ ભેળવે છે

ફરક એ છે કે code ક્યાં રહે છે. Code-behind C# ને markup સાથે જોડાયેલી અલગ સાથી file (Default.aspx.cs) માં મૂકે છે, અને એમ layout ને logic થી સ્વચ્છ રીતે અલગ પાડે છે. Inline coding C# ને .aspx file ની અંદર જ મૂકે છે અને બંનેને ભેળવે છે. વિકલ્પ A ખોટો છે: બંને server પર ચાલે છે (ASP.NET નો બધો page code એમ જ ચાલે છે), browser માં નહીં. વિકલ્પ C ખોટો છે: બંનેને CLR compile કરે છે; code ક્યાં બેસે છે એથી એ બદલાતું નથી. વિકલ્પ D ખોટો છે: એ ખરેખર જુદાં છે, અને code-behind ભલામણ કરાયેલો રસ્તો છે કારણ કે એ files ને કેન્દ્રિત અને સંભાળી શકાય એવી રાખે છે.

At a glance

બાબતInline codingCode-behind
C# ક્યાં રહે છે.aspx file ની અંદરઅલગ .aspx.cs file માં
Layout અને logicભેગાં ભળેલાંસ્વચ્છ રીતે અલગ
કયા માટે શ્રેષ્ઠનાના નમૂના અને ઝડપી અખતરાFestConnect જેવી ખરી applications
જાળવણીPage મોટું થાય એમ ગૂંચવાય છેવાંચી શકાય એવું અને ગોઠવાયેલું રહે છે
CLR compile કરે છેહાહા

Think first

Layout ને logic થી અલગ પાડવા વધારાની file શા માટે વેઠવી?

Code-behind એટલે એકને બદલે બે files. એ અલગતાને વધુ સારી રીત શા માટે ગણવામાં આવે છે? વિચારીને પછી tap કરો.

Show the answer

કારણ કે જવાબદારીઓ અલગ પાડવાથી software બનાવવું, વાંચવું અને બદલવું સહેલું બને છે. દેખાવનું layout .aspx માં હોય અને program નું logic .aspx.cs માં હોય ત્યારે દરેક file નું એક જ સ્પષ્ટ કામ હોય છે. Designer registration form નો શણગાર બદલી શકે છે અને data સાચવતા code ને અડવું કે જોખમમાં મૂકવું પડતું નથી; programmer સાચવવાનું logic સુધારી શકે છે અને markup માં ડૂબવું પડતું નથી. ભૂલો શોધવી પણ સહેલી બને છે કારણ કે કઈ file જોવી એ ખબર હોય છે. FestConnect વધીને ડઝનેક pages નું થાય ત્યારે આ શિસ્ત એને સંભાળી શકાય એવું રાખે છે, જ્યારે inline code દરેક મોટા page ને એવી ગૂંચવાયેલી ભેળસેળમાં ફેરવે છે જેને સલામત રીતે બદલવું અઘરું પડે. આ એ જ સિદ્ધાંત છે જે તમે પહેલાં પણ મળ્યા છો: જુદી જવાબદારીઓને જુદી જગ્યાએ રાખો. એક file, એક કામ.

Summary

Key takeaways

  • ASP.NET page markup (.aspx, layout) અને code (C#, logic) એમ બે ભાગમાં વહેંચાય છે.
  • Inline coding C# ને .aspx file ની અંદર script runat=server વાળા block માં મૂકે છે; એ ફક્ત નાના નમૂના માટે ઠીક છે.
  • Code-behind C# ને page ના directive થી જોડાયેલી અલગ સાથી file (Default.aspx.cs) માં મૂકે છે; આ ભલામણ કરાયેલો રસ્તો છે.
  • Code-behind layout ને logic થી અલગ પાડે છે અને દરેક file ને કેન્દ્રિત તથા સંભાળી શકાય એવી રાખે છે.
  • બંને રીતે CLR page ને compile કરે છે: C# માંથી MSIL, પછી just-in-time રીતે native code (BCA404 વાળું એ જ CLR).
  • ASP.NET page ને એની પહેલી માગણી વખતે compile કરે છે અને પછી એ compile થયેલી આવૃત્તિ ફરી વાપરે છે.
  • યાદ રાખવાની કડી: code-behind એટલે logic પોતાની file માં, markup ની બાજુમાં, એની અંદર નહીં.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to ASP.NET

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Compile Code: Code Behind and Inline Coding; The Common Language Runtime · .NET Technology (Major-13) · Gri-Learn