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 क्या है?
- Code-behind browser में चलता है, जबकि inline coding server पर चलता है
- Code-behind C# को .aspx markup से एक separate partner file में रखता है, जबकि inline coding C# को .aspx के अंदर ही mix करता है
- Inline coding CLR से compiled है, जबकि code-behind बिल्कुल compiled नहीं है
- कोई 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
| Aspect | Inline Coding | Code-Behind |
|---|---|---|
| C# कहाँ रहता है | .aspx file के अंदर | एक separate .aspx.cs file में |
| Layout और Logic | साथ mixed | Cleanly separated |
| इसके लिए Best | Tiny demos और quick tests | FestConnect जैसे real applications |
| Maintainability | Page बढ़ने के साथ 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, इसके अंदर नहीं।