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

एक ASP.NET page यह separate करता है कि यह कैसा दिखता है से यह क्या करता है: आप C# को same file में inline लिख सकते हैं या, कहीं बेहतर, एक separate code-behind file में, और किसी भी तरीके से CLR इसे चलने से पहले compile करता है।

10 min read · 10 cards · 2 checks

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


Theory

दो Questions जो हर Page Answer करता है

हर FestConnect page को दो अलग questions answer करने होते हैं: यह कैसा दिखता है (layout: headings, text boxes, एक Register button) और यह क्या करता है (logic: registration save करना, user को greet करना)। ASP.NET आपको इन दोनों concerns को सही जगहों पर रखने देता है।

Layout एक .aspx file में रहता है, markup में written। Logic C# code है, और आपके पास choice है कि वह code कहाँ रखें: same .aspx file में mixed (inline), या एक separate partner file में (code-behind)। यह lesson दोनों compare करता है और दिखाता है CLR कैसे किसी भी एक को एक running page में बदल देता है।

Theory

Inline Coding

Inline coding C# को directly .aspx file के अंदर रखता है, typically markup के साथ एक <script runat="server"> block में।

Page के लिए सब कुछ एक file में बैठता है। यह एक tiny demo के लिए convenient है, पर यह layout और logic को साथ mix करता है, जो page बढ़ने के साथ जल्दी पढ़ना और maintain करना मुश्किल हो जाता है। इसे ऐसे सोचिए जैसे आप अपनी styling, अपना HTML, और अपना पूरा program एक long file में लिखें: पाँच lines के लिए fine, पाँच सौ के लिए painful।

Practical

Inline: C# inside the .aspx file

<%@ 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# को एक separate file में रखता है जो page का partner है। Default.aspx नाम वाले एक page के लिए, code Default.aspx.cs में रहता है। .aspx सिर्फ़ markup रखता है; .aspx.cs सिर्फ़ logic रखता है। .aspx के top पर एक directive इन्हें link करती है।

यह standard, recommended approach है। यह cleanly layout को logic से separate करता है, तो designers markup पर काम कर सकते हैं और programmers code पर बिना collide हुए, और हर file focused और readable रहती है। हर real 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 compiled है, line by line interpreted नहीं। यह BCA404 वाला same Common Language Runtime (CLR) है: आपका C# MSIL (एक intermediate language) में compile होता है, फिर just-in-time native machine code में compile होकर चलता है।

ASP.NET एक page को इसकी पहली request पर compile करता है और बाद में उस compiled version को reuse करता है, तो पहला hit थोड़ा slower है और बाकी fast हैं। Takeaway: ASP.NET compiled .NET है, इससे मिलने वाले सारे type-checking और speed के साथ।

Quiz

ASP.NET में code-behind और inline coding के बीच main difference क्या है?

  1. Code-behind browser में चलता है, जबकि inline coding server पर चलता है
  2. Code-behind C# को .aspx markup से एक separate partner file में रखता है, जबकि inline coding C# को .aspx के अंदर ही mix करता है
  3. Inline coding CLR से compiled है, जबकि code-behind बिल्कुल compiled नहीं है
  4. कोई real difference नहीं है; दोनों नामों का मतलब same है
Show the answer

Code-behind C# को .aspx markup से एक separate partner file में रखता है, जबकि inline coding C# को .aspx के अंदर ही mix करता है

Difference यह है कि code कहाँ रहता है। Code-behind C# को markup से linked एक separate partner file (Default.aspx.cs) में रखता है, layout को logic से cleanly separate करते हुए। Inline coding C# को .aspx file के अंदर ही रखता है, दोनों को mixing करते हुए। Option A wrong है: दोनों server पर चलते हैं (सारा ASP.NET page code करता है), browser नहीं। Option C wrong है: दोनों CLR से compiled हैं; code कहाँ बैठता है यह नहीं बदलता। Option D wrong है: ये genuinely अलग हैं, और code-behind recommended approach है क्योंकि यह files को focused और maintainable रखता है।

At a glance

AspectInline CodingCode-Behind
C# कहाँ रहता है.aspx file के अंदरएक separate .aspx.cs file में
Layout और Logicसाथ mixedCleanly separated
इसके लिए BestTiny demos और quick testsFestConnect जैसे real applications
MaintainabilityPage बढ़ने के साथ messy हो जाता हैReadable और organised रहता है
CLR से Compiledहाँहाँ

Think first

Layout को Logic से Separate करना Extra File के लायक क्यों है?

Code-behind मतलब एक की बजाय दो files। वह separation better practice क्यों माना जाता है? फिर tap कीजिए।

Show the answer

क्योंकि SEPARATION OF CONCERNS software को build, read, और change करना आसान बनाता है। जब visual layout .aspx में है और program logic .aspx.cs में है, हर file का एक clear job है। एक designer registration form को restyle कर सकता है data save करने वाले code को touch या risk किए बिना; एक programmer save logic fix कर सकता है markup में wading किए बिना। Bugs ढूँढना आसान है क्योंकि आप जानते हैं कौन सी file में देखना है। जैसे FestConnect dozens pages तक बढ़ता है, यह discipline इसे maintainable रखता है, जबकि inline code हर बड़े page को एक tangled mix में बदल देता है जो safely edit करना मुश्किल है। यह वही principle है जिससे आप पहले मिल चुके हैं: distinct responsibilities को distinct places में रखिए। एक file, एक job।

Summary

Key takeaways

  • एक ASP.NET page markup (.aspx, layout) और code (C#, logic) में split होता है।
  • Inline coding C# को .aspx file के अंदर रखता है, एक script runat=server block इस्तेमाल करते हुए; सिर्फ़ tiny demos के लिए fine।
  • Code-behind C# को एक separate partner file (Default.aspx.cs) में रखता है जो एक page directive से linked है; यह recommended approach है।
  • Code-behind layout को logic से separate करता है, हर file को focused और maintainable रखते हुए।
  • किसी भी तरीके से, CLR page compile करता है: C# से MSIL, फिर just-in-time native code (BCA404 वाला same CLR)।
  • ASP.NET इसकी पहली request पर एक page compile करता है और बाद में compiled version reuse करता है।
  • Memory hook: code-behind = markup के बगल में अपनी own file में logic, इसके अंदर नहीं।

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