ASP.NET控件之RadioButtonList详解

2019-05-22 08:50:41刘景俊

4.为ListItem添加提示

RadioButtonList1.Items[0].Attributes.Add("title", "提示内容");

5.绑定数据源            

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.改变选中项的前景色

<asp:RadioButtonList ID="rblIsLock" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblIsLock_SelectedIndexChanged" RepeatDirection="Horizontal" RepeatLayout="Flow">   
<asp:ListItem Selected="True" Value="0">启用 </asp:ListItem>   
<asp:ListItem Value="1">禁用 </asp:ListItem> 
</asp:RadioButtonList> 
<label>*禁用的用户将无法登录</label>

后台:   

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.后台动态增加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是TableRow的一个单元格TableCell
 
   for (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.前台改变选中项的背景色 

  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.backgroundColor = "red";
   }
   else {
   arr[i].style.backgroundColor = "";
   }
  }
  else {
   arr[i].style.backgroundColor = "";
  }
  }
 }


为大家附3个精彩的专题:

ASP.NET控件使用手册

ASP.NET数据绑定控件使用汇总

ASP.NET控件使用汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。