13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6: Standard Web Controls 145Using either the Properties window or typing in code, you need to add two key properties,GroupName (Behavior category) and Text (Appearance category). The GroupNameproperty causes the selection of any single radio button to automatically deselect all of theothers in the group. After adding both elements, you will see something like the following:As you can see, the properties are similar to the HTML radio button.For the C# portion the radio button’s value in the Text property is accessed verymuch like one for the CheckBox web control in the previous section. The followingC# code snippet illustrates how a value is passed from a checked radio button toa variable:radVar += RadioButton1.Checked ? RadioButton1.Text : null;The += <strong>com</strong>pound operator is a way of not getting a null where you don’t want it. If yourlast radio button in a group is null and assigned to a variable, it wipes out the value ofa previous assignment. However, if you use +=, the null is just added to whatever is storedand does not really affect the non-null value stored.RadioButtonList Web ControlWorking with the RadioButtonList is almost identical to working with the CheckBoxListdiscussed in the last section. Each single RadioButtonList can have several items addedto it, and when addressing the selected value, the reference is to the ListItem propertywhere the data are stored. Adding items is the same as working with CheckBoxList items(shown earlier in Figure 6-9) in the previous section.When it <strong>com</strong>es to working with the RadioButtonList with C#, you’ll find it veryeasy because the user selects only a single ListItem. So instead of looping throughthe object, you can just assign the value of the selected item as shown in the followingsnippet:radVar = RadioButtonList1.SelectedItem.Text;The key here is the SelectedItem property. Only one list item can have thatproperty.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!