Server Controls (TextBox, Button, CheckBox, Image Map, Label, LinkButton, RadioButton); Post Back

Server controls એ Web Forms ના page ના ઘટકો છે: TextBox, Button, Label, CheckBox અને RadioButton જેવાં tags જેને તમે markup માં મૂકો છો, જેમના properties ગોઠવો છો, અને જેમને તમારા C# code માંથી વાંચી કે બદલી શકો છો, કારણ કે એ server પર રહે છે.

10 min read · 9 cards · 2 checks

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


Theory

Web page માટેની ઓજારપેટી

FestConnect નું registration form બનાવવા તમને વિદ્યાર્થીના નામ માટે text box જોઈએ, 'send me reminders' માટે checkbox જોઈએ, ભોજનની પસંદગી ચૂંટવા radio buttons નું જૂથ જોઈએ, આવકારનો સંદેશો બતાવવા label જોઈએ, અને Register button જોઈએ. ASP.NET આ બધું તૈયાર server controls તરીકે આપે છે.

કાચું HTML હાથે લખવાને બદલે તમે <asp:TextBox> અને <asp:Button> જેવાં tags મૂકો છો, એમના properties ગોઠવો છો, અને તમારા C# code માંથી એમની સાથે વાત કરો છો. આ પાઠ સામાન્ય server controls ની ઓળખાણ કરાવે છે અને એમને એકસાથે બાંધતું એક વર્તન પણ: postback.

Theory

Control ને server control શું બનાવે છે

Server control એટલે asp: ઉપસર્ગ સાથે લખાયેલું અને runat="server" થી ચિહ્નિત કરેલું tag. એ લક્ષણ જ ચાવી છે: એ ASP.NET ને કહે છે કે control server પર રહે છે, એટલે તમારું code-behind એના id થી એના સુધી પહોંચી શકે.

એને લીધે દરેક server control પાસે ત્રણ વસ્તુ હોય છે: તમે ગોઠવી કે વાંચી શકો એવા properties (જેમ કે TextBox નું Text), એ ઊભાં કરી શકે એવાં events (જેમ કે Button નું Click), અને એ browser માટે HTML તરીકે render થાય છે. એટલે markup માં તમે મૂકેલું <asp:TextBox> મુલાકાતી માટે સામાન્ય HTML નું input box બની જાય છે, પણ server પર એ આખું object છે જેને તમે program કરી શકો છો.

At a glance

Controlશેના માટે છેPostback કરે છે?
TextBoxવપરાશકારને લખાણ ટાઇપ કરવા દે (નામ, email)મૂળભૂત રીતે નહીં
ButtonForm submit કરતું, click કરી શકાય એવું buttonહા
Labelવપરાશકારને ફક્ત વાંચવાનું લખાણ બતાવેનહીં
CheckBoxએક જ ચાલુ કે બંધ પસંદગીમૂળભૂત રીતે નહીં
RadioButtonજૂથમાંથી એક વિકલ્પ ચૂંટવોમૂળભૂત રીતે નહીં
LinkButtonLink જેવું દેખાય, button જેવું વર્તેહા
ImageMapClick કરી શકાય એવા વિસ્તારો વાળી છબીહા, hotspot પર click થાય ત્યારે

Practical

FestConnect ના registration form નો એક ટુકડો (.aspx)

<form runat="server">
    <asp:Label id="lblName" Text="Your name:" runat="server" />
    <asp:TextBox id="txtName" runat="server" />

    <asp:CheckBox id="chkReminders" Text="Send me reminders" runat="server" />

    <asp:RadioButton id="rbVeg"    GroupName="meal" Text="Veg"     runat="server" />
    <asp:RadioButton id="rbNonVeg" GroupName="meal" Text="Non-veg" runat="server" />

    <asp:Button id="btnRegister" Text="Register" OnClick="btnRegister_Click" runat="server" />
    <asp:Label id="lblStatus" runat="server" />
</form>

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

Practical

એ controls ને code-behind માં વાંચવાં

protected void btnRegister_Click(object sender, EventArgs e)
{
    string name = txtName.Text;                 // read the TextBox
    bool wantsReminders = chkReminders.Checked; // read the CheckBox
    string meal = rbVeg.Checked ? "Veg" : "Non-veg";

    lblStatus.Text = name + " registered (" + meal + ").";  // write the Label
}

Formula

કયાં controls postback કરે છે

Postback એ જ form ને server પર મોકલે છે જેથી તમારો code ચાલી શકે. Button અને LinkButton click થાય ત્યારે postback કરે છે, અને ImageMap hotspot પર click થાય ત્યારે postback કરે છે. Click handler આ રીતે જ ચાલુ થાય છે.

મૂળભૂત રીતે TextBox, CheckBox કે RadioButton પોતાની મેળે postback કરતું નથી; એ તો ફક્ત પોતાની કિંમત સાચવી રાખે છે, જે પછીના postback સાથે મોકલાય છે. એમાંનું કોઈ બદલાય કે તરત postback કરે એવું જોઈતું હોય તો એની AutoPostBack property ને true કરો. અંગૂઠાનો નિયમ: buttons કામ કરે છે, inputs રાહ જુએ છે.

Quiz

FestConnect ના form પર વિદ્યાર્થી TextBox માં નામ લખે છે, CheckBox પર નિશાની કરે છે અને Register Button પર click કરે છે. તમારો server નો code ચલાવતું postback કયું control કરે છે?

  1. TextBox, નામ ટાઇપ થતાં જ
  2. Button, એના પર click થાય ત્યારે
  3. CheckBox, એના પર નિશાની થાય ત્યારે
  4. એકેય નહીં; page ઘડિયાળ પ્રમાણે ફરી load થાય છે
Show the answer

Button, એના પર click થાય ત્યારે

Button postback કરે છે: એના પર click કરવાથી form server પર જાય છે, જે તમારો btnRegister_Click handler ચલાવે છે. મૂળભૂત રીતે TextBox (વિકલ્પ A) અને CheckBox (વિકલ્પ C) જાતે postback કરતાં નથી; એ તો ફક્ત પોતાની કિંમતો (લખાયેલું નામ, નિશાની થયેલી સ્થિતિ) સાચવી રાખે છે જે પછીના postback સાથે મોકલાય, સિવાય કે તમે એમના પર AutoPostBack ને true કરો. વિકલ્પ D ઉપજાવેલો છે: ASP.NET pages ઘડિયાળ પ્રમાણે ફરી load થતાં નથી. એટલે buttons કામ કરે છે (postback), inputs રાહ જુએ છે. એટલે જ button નો Click handler છેવટે ચાલે ત્યારે એની અંદર વિદ્યાર્થીએ લખેલું નામ અને નિશાની થયેલું ખાનું બંને હાજર હોય છે.

Think first

સાદા HTML input ને બદલે asp:TextBox શા માટે વાપરવું?

Browser પાસે <input> ખાનું તો છે જ. એને <asp:TextBox runat="server"> તરીકે વીંટવાથી ખરેખર શું મળે છે? વિચારીને પછી tap કરો.

Show the answer

એ મૂંગા HTML ઘટકને program કરી શકાય એવા server ના object માં ફેરવે છે. સાદું <input> તો ફક્ત browser બતાવે છે તે markup છે; તમારો server નો code એને નામથી સહેલાઈથી વાંચી કે બદલી શકતો નથી. <asp:TextBox runat="server"> મુલાકાતી માટે તો સામાન્ય input તરીકે જ render થાય છે, પણ server પર એ id વાળું object છે, એટલે તમારું code-behind txtName.Text વાંચી શકે, એને ગોઠવી શકે, તપાસી શકે, બંધ કરી શકે કે C# માંથી શણગારી શકે. વળી એ postbacks દરમિયાન પોતાની કિંમત આપોઆપ સાચવી રાખે છે (એને view state કહે છે), એટલે click પછી પણ ખાનામાં વપરાશકારે લખેલું દેખાય છે. અને એ event model, validation controls તથા આગળ મળનારા data binding સાથે પણ જોડાય છે. ટૂંકમાં: browser માટે એ જ HTML, પણ server પર આખું, કાબૂમાં રહેતું object, અને Web Forms નાં controls નો આખો હેતુ એ જ છે. Server પર થોડા વધારાના ભાર બદલામાં સગવડ અને કાબૂ.

Summary

Key takeaways

  • Server controls એ asp: ઉપસર્ગ અને runat=server સાથે લખાયેલા તૈયાર ઘટકો છે, જેથી server એમને program કરી શકે.
  • દરેક પાસે properties હોય છે (TextBox.Text), એ events ઊભાં કરી શકે છે (Button.Click), અને એ browser માટે HTML તરીકે render થાય છે.
  • સામાન્ય controls: TextBox (input), Button (submit), Label (બતાવવા), CheckBox (ચાલુ કે બંધ), RadioButton (જૂથમાંથી એક), LinkButton (button જેવું વર્તતું link), ImageMap (click કરી શકાય એવા છબીના વિસ્તારો).
  • Button, LinkButton અને ImageMap postback કરે છે; TextBox, CheckBox અને RadioButton મૂળભૂત રીતે કરતાં નથી (એ બદલવા AutoPostBack ગોઠવો).
  • Code-behind માં તમે controls ને id થી વાંચો અને લખો છો: txtName.Text, chkReminders.Checked, lblStatus.Text.
  • Server control postbacks દરમિયાન પોતાની કિંમત સાચવે છે (view state) અને events, validation તથા data binding સાથે જોડાય છે.
  • યાદ રાખવાની કડી: buttons કામ કરે છે, inputs રાહ જુએ છે; runat=server એને program કરી શકાય એવું બનાવે છે.

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