Theory
एक Page जो React करता है
आपके VB.NET desktop apps straight through नहीं चले और रुक नहीं गए। ये wait करते थे, और react करते थे: user ने एक button click किया, और आपका click code चला। यही event-driven programming है, और ASP.NET Web Forms web के across same तरीके से काम करता है।
एक FestConnect page सिर्फ़ एक बार display नहीं होता। यह react करता है: जब page load होता है, कुछ code चलता है; जब एक student Register click करता है, आपका registration code चलता है। आप handlers नाम के छोटे methods लिखते हैं, हर एक एक event से tied, और ASP.NET सही handler को call करता है जब वह event होता है। यह lesson दिखाता है कैसे, और web के add किए एक twist introduce करता है: postback।
Theory
Events और Handlers
दो kinds के events matter करते हैं। Page events page से खुद आते हैं, सबसे important Page_Load, जो हर बार page process होने पर चलता है। Control events उन controls से आते हैं जिनसे user interact करता है, जैसे एक Button का Click।
हर event के लिए जिसकी आपको care है, आप एक handler लिखते हैं: एक ordinary C# method जिसे ASP.NET event fire होने पर चलाता है। आप इन methods को खुद call नहीं करते; framework इन्हें सही moment पर आपके लिए call करता है। आपका job लिखना है क्या होना चाहिए।
Practical
Handlers for a page event and a button click
public partial class Register : System.Web.UI.Page
{
// Page event: runs every time the page is processed
protected void Page_Load(object sender, EventArgs e)
{
lblTitle.Text = "Register for an Event";
}
// Control event: runs when the Register button is clicked
protected void btnRegister_Click(object sender, EventArgs e)
{
lblStatus.Text = "Thanks, " + txtName.Text + "! You are registered.";
}
}Formula
Postback: Page खुद से बात करता है
यहाँ web twist है। जब student Register click करता है, browser पूरा form वापस same page को server पर भेजता है। यह round trip एक postback है।
Server पर, ASP.NET page reload करता है, Page_Load चलाता है, फिर button का Click handler चलाता है, fresh HTML build करता है, और इसे वापस भेजता है। तो एक click browser में handle नहीं होता; यह server तक एक trip है और वापस। हर button click, हर postback, पहले Page_Load फिर से चलाता है।
Theory
First Load बनाम Postback: IsPostBack
चूँकि Page_Load हर postback पर चलता है, आपको एक तरीका चाहिए बहुत पहली visit को बाद के button clicks से अलग बताने का। यही Page.IsPostBack के लिए है।
Page की पहली request पर, IsPostBack false है। इसके बाद हर postback पर (उदाहरण के लिए एक button click), यह true है। आप इसे first-time setup सिर्फ़ एक बार चलाने के लिए इस्तेमाल करते हैं, इसे if (!IsPostBack) में wrapping करते हुए। Typical first-time setup: database से events की एक drop-down list भरना। Guard के बिना, आप हर single click पर उस list को refill करते, user की selection wipe out करते हुए।
Practical
Run setup only on the first load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Runs ONCE, on the first visit only
LoadEventList(); // fill the drop-down from the database
}
// Code here (outside the guard) runs on every load and every postback
}Quiz
एक student पहले Register.aspx खोलता है, फिर एक बार Register button click करता है। Page_Load कितनी बार चलता है, और हर बार IsPostBack क्या है?
- Total एक बार; IsPostBack हमेशा false है
- दो बार: पहले load पर (IsPostBack false), फिर button-click postback पर फिर से (IsPostBack true)
- एक बार, सिर्फ़ जब button click हो; IsPostBack true है
- Page_Load कभी नहीं चलता जब तक आप इसे खुद call न करें
Show the answer
दो बार: पहले load पर (IsPostBack false), फिर button-click postback पर फिर से (IsPostBack true)
Page_Load page के EVERY processing पर चलता है। पहली visit एक processing है (IsPostBack false है), और Register click करना एक postback cause करता है, एक दूसरी processing (IsPostBack true है), तो Page_Load total दो बार चलता है। Option A miss करता है कि एक postback Page_Load फिर से चलाता है। Option C पहला load भूल जाता है, जो Page_Load भी चलाता है। Option D wrong है: framework automatically Page_Load call करता है; आप इसे कभी खुद call नहीं करते। यही exactly वजह है if (!IsPostBack) guard exist करता है: first-time setup एक बार चलाने के लिए, हर click पर फिर से नहीं।
Think first
Setup को if (!IsPostBack) से Guard क्यों करें?
अगर आप अपना event drop-down Page_Load में if (!IsPostBack) check के बिना भरें तो actually क्या ग़लत होता है? फिर tap कीजिए।
Show the answer
यह हर postback पर drop-down को re-fill करता है, जो usually user की choice wipe out करता है और इनकी selection hide कर सकता है। FestConnect के registration page को picture कीजिए: Page_Load events की list से एक drop-down भरता है। Student 'Robotics Workshop' चुनता है और Register click करता है। वह click एक postback है, तो Page_Load फिर से चलता है; अगर यह unconditionally drop-down reload करे, list scratch से rebuild होती है और selected item typically reset हो जाता है, तो जब तक आपका Click handler choice पढ़ता है, यह gone या wrong हो सकता है। if (!IsPostBack) से guard करना मतलब list एक बार, पहली visit पर build होती है, और फिर हर click पर alone छोड़ी जाती है, तो student की selection postback survive करती है। Rule of thumb: one-time setup (lists load करना, initial values) if (!IsPostBack) के अंदर जाता है; काम जो हर click को respond करना चाहिए control के event handler में जाता है।
Watch out
Event-Model Traps
Page_Load Re-Runs भूलना: यह हर postback पर चलता है, सिर्फ़ पहली visit पर नहीं; one-time setup को if (!IsPostBack) से guard कीजिए।
हर Postback पर Controls Rebuilding करना: unconditionally lists reload करना user की selection erase करता है।
Clicks के Browser में चलने की Expect करना: एक Button Click एक postback के बाद SERVER पर handle होता है, page में instantly नहीं।
Handlers खुद Call करना: framework Page_Load और event handlers call करता है; आप बस इन्हें लिखते हैं।
Summary
Key takeaways
- ASP.NET Web Forms event-driven है, VB.NET desktop apps की तरह: code events के response में चलता है, top to bottom एक बार नहीं।
- Page events (Page_Load) page से आते हैं; control events (Button Click) user interaction से आते हैं।
- आप handler methods लिखते हैं; framework इन्हें सही समय पर call करता है।
- एक button click जैसा एक user interaction एक postback cause करता है: page उसी server page को भेजा जाता है, जो फिर से चलता है और फिर से render करता है।
- Page_Load हर processing पर चलता है, हर postback सहित; Page.IsPostBack पहले load पर false है और postbacks पर true।
- One-time setup को if (!IsPostBack) से guard कीजिए ताकि यह एक बार चले और बाद के clicks पर user का input erase न करे।
- Memory hook: click -> postback -> Page_Load -> आपका handler -> नया HTML।