Asp. NET control RadioButtonList Detailed _ Practical skills

Source: Internet
Author: User

The RadioButtonList control represents a list control that encapsulates a set of radio button controls.

You can add radio buttons to a Web page using two types of ASP.net controls: Individual "RadioButton" controls or a "RadioButtonList" control. Both types of controls allow users to choose from a group of mutually exclusive predefined options. With these controls, you can define any number of radio buttons with labels and arrange them horizontally or vertically.

namespaces: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)

[ValidationPropertyAttribute ("SelectedItem")]
public class Radiobuttonlist:listcontrol, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
The RadioButtonList control provides Web page developers with a set of radio buttons that can be dynamically generated through data binding. The control contains a collection of Items that correspond to the members in the list. To determine which item is selected, test the SelectedItem property of the list.

You can specify how the list is rendered using the RepeatLayout and RepeatDirection properties. If you set RepeatLayout to RepeatLayout.Table (the default setting), the list is rendered in the table. If set to RepeatLayout.Flow, the list will not be rendered in tabular form. By default, RepeatDirection is set to RepeatDirection.Vertical. When this property is set to RepeatDirection.Horizontal, the list is rendered horizontally.

RadioButtonList Usage:

<div class= "Rblstyle" >
<asp:radiobuttonlist id= "RBLCHANGQHT" runat= "server" repeatdirection= "Horizontal" >
<asp:listitem text= "yes" value= "1" ></asp:ListItem>
<asp:listitem text= "No" value= "0" ></asp:ListItem>
</asp:RadioButtonList></div>

1.RadioButtonList Checksum

  var rb_changqht = document.getElementById ("Rblchangqht");
  var shif = rb_changqht.getelementsbytagname ("INPUT");
  var result = false;
  for (var i = 0; i < shif.length. i++) {
  if (shif[i].checked) {result
   = true;
   break;
  }
  }
  if (!result) {
  alert ("is required for medium and long term contracts!") ");
  return false;
  }

2.RadioButtonList Style Adjustment

. Rblstyle{width:100%;height:auto;}
. Rblstyle Input{border-style:none;}

3.onselectedindexchanged Event

Like a Drop-down control DropDownList control, it also has onselectedindexchanged events, which are triggered when a change is made

Note that the AutoPostBack property in the control must be set to "True" so that the server side knows your options are changed and triggers the corresponding event

4. Add Tips for ListItem

Radiobuttonlist1.items[0]. Attributes.Add ("title", "Hint content");

5. Bound data Source

String sql = "Select * from Province";
DataTable dt = sqlhelper.executedatatable (sql);
This. Radiobuttonlist1.datasource = DT;
This. Radiobuttonlist1.datatextfield = "Provinces";
This. Radiobuttonlist1.datavaluefield = "PId";
This. Radiobuttonlist1.databind ();

6. Change the foreground color of the selected item

<asp:radiobuttonlist id= "Rblislock" runat= "Server" autopostback= "true" onselectedindexchanged= "RblIsLock_" SelectedIndexChanged "repeatdirection=" horizontal "repeatlayout=" Flow ">   
<asp:listitem Selected=" True " Value= "0" > Enable </asp:ListItem>   
<asp:listitem value= "1" > Disable </asp:ListItem> 
</asp: radiobuttonlist> 
<label>* disabled users will not be able to log in </label>

Background:

protected void Rblislock_selectedindexchanged (object sender, EventArgs e) 

{ 
 var RBL = sender as RadioButtonList; 
 Highliehgselecteditem (RBL); 
}
 
private void Highliehgselecteditem (RadioButtonList rbl) 
{ 
 foreach (ListItem li in RBL). Items) 
 {  
 if (li. Selected)  
 {  
 li. Attributes.Add ("Style", "color:red;");}} 



7. Background dynamic increase RadioButtonList

 RadioButtonList RBL = new RadioButtonList ();
   Rbl.id = "RBL" + (i + 1). ToString ();
   Rbl. BorderStyle = BorderStyle.None;
   Rbl. RepeatLayout = RepeatLayout.Flow;
   Rbl. RepeatDirection = repeatdirection.horizontal;
   Rbl. TextAlign = textalign.right;
   Rbl. cellspacing = 6;
   Rbl. Attributes.Add ("onclick", "CHECKRBL (' Ctl00_ctl00_ctl00_contentplaceholder1_cphbody_cphlower_" + Rbl.ID + ")");
   Rbl. DataSource = Dtrating.defaultview;
   Rbl. DataTextField = "Levelid";
   Rbl. DataValueField = "Levelid";
   Rbl. DataBind ();
   Tc. Controls.Add (RBL); TC is a cell TableCell for TableRow
 
   (int k = 0; k < RBL). Items.Count; k++)
   {
   RBL. ITEMS[K]. Attributes.Add ("title", Dtrating.rows[k][1]. ToString ());
   Rbl. ITEMS[K]. Attributes.Add ("Style", "margin-left:10px;");
   

8. Foreground changes the background color of the selected item

  Window.onload = function () {
  var arr = document.getelementsbytagname ("INPUT");
  for (var i = 0; i < arr.length i++) {
  if (arr[i].checked) {
   if (Arr[i].type = = "Radio") {
   Arr[i].style.bac Kgroundcolor = "Red";
   }
   else {
   Arr[i].style.backgroundcolor = "";
   }
  }
  else {
   Arr[i].style.backgroundcolor = "";}}}
 


3 wonderful topics to be attached:

Asp. NET Control Usage Manual

Asp. NET data-bound controls using totals

Asp. NET controls use totals

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.