ASP.NET Denetimleri: Kontrol EtBox, Radyo Düğmesi, ListeBox
⚡ Akıllı Özet
ASP.NET Kontrolleri, Etiket, Metin Kutusu ve Liste öğelerini sürükleyip bırakarak etkileşimli Web Formları oluşturmanıza olanak tanır.BoxSayfaya RadioButton, Checkbox ve Button kontrolleri eklemek, özelliklerini ayarlamak ve kullanıcı girişini işlemek için bir düğme tıklama olay işleyicisi bağlamak.
Web Formlarına ASP.Net Denetimleri Ekleme
ASP.Net, formlara metin kutuları ve etiketler gibi kontroller ekleme özelliğine sahiptir.
Let's look at the other controls available for Web Forms and see some of their common properties.
Örneğimizde aşağıdaki işlevselliğe sahip bir form oluşturacağız.
- Kullanıcının adını girme yeteneği.
- Kullanıcının ikamet ettiği şehri seçme seçeneği.
- Kullanıcının cinsiyet için bir seçenek girebilmesi.
- Kullanıcının öğrenmek istediği dersi seçme seçeneği. İkisi için de seçimler olacak C# ve ASP.Net.
Let's look at each control in detail. Let's add them to build the form with the above-mentioned functionality.
) 1 Adım İlk adım Demo web formu için Form Tasarımcısını açmaktır. Bunu yaptıktan sonra araç kutusundan denetimleri sürükleyip bırakabileceksiniz. internet formu.
Tasarımcı web formunu açmak için:
- Çözüm Gezgini'nde Demo.aspx dosyasına sağ tıklayın ve
- Tasarımcıyı Görüntüle menü seçeneğini seçin.
Yukarıdaki adımı gerçekleştirdiğinizde Form Tasarımcınızı aşağıda gösterildiği gibi görebileceksiniz.
Now let's start adding our controls one by one.
Etiket Kontrolü
The label control is used to display a text or a message to the user on the form. The label control is normally used along with other controls. A common example is where a label is added along with the textbox control. The label gives an indication to the user on what is expected to fill up in the textbox. Let's see how we can implement this with an example shown below. We will use a label called 'name'. This will be used in conjunction with the textbox controls, which will be added in the later section.
) 1 Adım The first step is to drag the 'label' control onto the Web Form from the toolbox as shown below.
) 2 Adım Etiket eklendikten sonra aşağıdaki adımları izleyin.
- Etiket denetimine sağ tıklayarak özellikler penceresine gidin.
- Özellikler menü seçeneğini seçin.
) 3 Adım Özellikler penceresinden, Metin özelliğinin değerini Ad olarak değiştirin.
Benzer şekilde, kontrolün ID özelliği değerini de lblName olarak değiştirin. Kontrollere anlamlı bir ID belirtilerek kodlama aşamasında bunlara erişim kolaylaşır. Bu aşağıda gösterilmiştir.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Web Formunda Ad etiketinin göründüğünü göreceksiniz.
Metin kutusu
A text box is used for allowing a user to enter some text on the Web form application. Let's see how we can implement this with an example shown below. We will add one textbox to the form in which the user can enter his name.
) 1 Adım İlk adım, aşağıdaki resimde gösterildiği gibi araç kutusundan metin kutusu denetimini Web Formuna sürüklemektir.
Aşağıda, Textbox denetimi forma eklendikten sonra form tasarımcısında nasıl görüneceği gösterilmiştir.
) 2 Adım Textbox eklendikten sonra ID özelliğini değiştirmeniz gerekiyor.
- Textbox denetimine sağ tıklayarak özellikler penceresine gidin ve
- Özellikler'i seçin, ardından
- Metin kutusunun ID özelliğini txtName olarak değiştirin.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Liste kutusu
A Listbox is used to showcase a list of items on the Web form. Let's see how we can implement this with an example shown below. We will add a list box to the form to store some city locations.
) 1 Adım İlk adım, aşağıdaki resimde gösterildiği gibi, araç kutusundan liste kutusu denetimini Web Formuna sürüklemektir.
) 2 Adım Once you drag the listbox to the form, a separate side menu will appear. In this menu, choose the 'Edit Items' menu.
) 3 Adım Şimdi liste öğelerini liste kutusuna ekleyebileceğiniz bir iletişim kutusu karşınıza çıkacak.
- Bir liste öğesi eklemek için Ekle düğmesine tıklayın.
- Liste öğesinin metin değeri için bir ad verin – bizim örneğimizde Mumbai. Mangalore ve Hyderabad için liste öğeleri eklemek üzere 1. ve 2. adımları tekrarlayın.
- Tamam düğmesine tıklayın.
) 4 Adım Özellikler penceresine gidin ve denetimin ID özelliğinin değerini lstLocation olarak değiştirin.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Çıktıdan da açıkça görülebileceği gibi, Listbox forma eklendi.
RadioButton
A Radio button is used to showcase a list of items out of which the user can choose one. Let's see how we can implement this with an example shown below. We will add a radio button for a male/female option.
) 1 Adım The first step is to drag the 'radiobutton' control onto the Web Form from the toolbox (see image below). Make sure to add 2 radio buttons, one for the option of 'Male' and the other for 'Female'.
) 2 Adım Once the Radiobutton has been added, change the 'text' property.
- Go to the properties window by clicking on the 'Radiobutton control'.
- Change the text property of the Radio button to 'Male'.
- Repeat the same step to change it to 'Female'.
- Ayrıca ilgili kontrollerin kimlik özelliklerini rdMale ve rdFemale olarak değiştirin.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Çıktıdan da açıkça görülebildiği gibi, radyo düğmesi forma eklendi.
Onay Kutusu
A checkbox is used to provide a list of options in which the user can choose multiple choices. Let's see how we can implement this with an example shown below. We will add 2 checkboxes to our Web Forms. These checkboxes will provide an option to the user on whether they want to learn C# veya ASP.Net.
) 1 Adım İlk adım, aşağıdaki resimde gösterildiği gibi, onay kutusu denetimini araç kutusundan Web Formuna sürüklemektir.
) 2 Adım Onay kutuları eklendikten sonra, onay kutusu kimliği özelliğini değiştirin.
- Onay Kutusu denetimine tıklayarak özellikler penceresine gidin.
- Change the ID properties of the respective controls to 'chkC' and 'chkASP'.
Also, change the text property of the Checkbox control to 'C#'. Do the same for the other Checkbox control and change it to 'ASP.Net'.
- Change the ID property of the checkbox to 'chkASP'.
- Onay kutusunun ID özelliğini chkC olarak değiştirin.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Çıktıdan da açıkça görülebileceği gibi, onay kutuları forma eklenmiştir.
düğme
A button is used to allow the user to click on a button which would then start the processing of the form. Let's see how we can implement this with our current example as shown below. We will add a simple button called the 'Submit' button. This will be used to submit all the information on the form.
) 1 Adım İlk adım, aşağıdaki resimde gösterildiği gibi, düğme denetimini araç kutusundan Web Formuna sürüklemektir.
) 2 Adım Once the button has been added, go to the properties window by clicking on the button control. Change the text property of the button control to Submit. Also, change the ID property of the button to 'btnSubmit'.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Çıktıdan butonun forma eklendiğini açıkça görebilirsiniz.
ASP.Net'te Olay İşleyicisi
Bir web formuyla çalışırken kontrollere etkinlik ekleyebilirsiniz. Bir olay, bir eylem gerçekleştirildiğinde meydana gelen bir şeydir. Muhtemelen en yaygın eylem, formdaki bir düğmenin tıklatılmasıdır.
In web forms, you can add code to the corresponding aspx.cs file. This code can be used to perform certain actions when a button is pressed on the form. This is generally the most common event in Web Forms. Let's see how we can achieve this.
We are going to make this simple. Just add an event to the button control to display the name which was entered by the user. Let's follow the below steps to achieve this.
) 1 Adım Öncelikle Web Formu'ndaki Button'a çift tıklamanız gerekiyor. Bu, Visual Studio'daki button için olay kodunu getirecektir.
Visual Studio, web formları tasarımcısında düğmeye çift tıkladığınızda btnSubmit_Click olayını otomatik olarak ekler.
) 2 Adım Let's now add code to the submit event to display the name textbox value and the location chosen by the user.
protected void btnSubmit_Click(object sender, EventArgs e) { Response.Write(txtName.Text + "<br/>"); Response.Write(lstLocation.SelectedItem.Text + "<br/>"); lblName.Visible = false; txtName.Visible = false; lstLocation.Visible = false; chkC.Visible = false; chkASP.Visible = false; rdFemale.Visible = false; btnSubmit.Visible = false; }
Code Açıklama:-
- The above line of code does the simplest thing. It takes the value of the Name textbox control and sends it to the client via the Response object. So if you want to enter the string "Guru99" in the name text box, the value of txtName.Text would be 'Guru99
- The next line of code takes the selected value of the listbox via the property 'lstLocation.SelectedItem.Text'. It then writes this value via the Response.Write method back to the client.
- Finally, we make all the controls on the form invisible. If we don't do this, all the controls plus our response values will be displayed together. Normally, when a person enters all the information on the form such as the Name, location, Gender, etc., the next page shown to the user should only have the information which was not entered. The user does not want to see the Name, Gender, and location controls again. But ASP.Net does not know this, and hence, by default, it will again show all the controls when the user clicks the Submit button. Hence, we need to write code to ensure all the controls are hidden so that the user just sees the desired output.
Yukarıdaki değişiklikleri yaptıktan sonra aşağıdaki çıktıyı göreceksiniz.
Çıktı:-
Çıktı ekranında aşağıdaki adımları uygulayın:
- Bir isim verin Guruİsim kutusuna 99 yazın.
- Bangalore'un bulunduğu liste kutusundan bir konum seçin.
- Gönder düğmesine tıklayın.
Once you do this, you will see 'Guru99' and the location 'Bangalore' displayed on the page.
































