ASP.NET中RadioButtonList绑定后台数据后触发点击事件
本文实例为大家分享了RadioButtonList绑定后台数据,触发点击事件的方法
首先前台页面放置一个RadioButtonList控件
<asp:RadioButtonListrunat="server"ID="RadioButtonList1"BorderStyle="None"RepeatColumns="3"CssClass="" RepeatLayout="Flow"AutoPostBack="true"OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"> </asp:RadioButtonList>
.cs文件后台绑定数据
namespaceBTApp { publicpartialclassTechnology:System.Web.UI.Page { stringId; protectedvoidPage_Load(objectsender,EventArgse) { if(!IsPostBack) { AspNetPager1.PageSize=10; if(Request.QueryString["Id"]!=null) { Id=Request.QueryString["Id"]; } else {Id="";} GetDataBind(Id); DropDownListDataBind(); } } //RadioButtonList绑定后台数据 privatevoidDropDownListDataBind() { ExpertInfoBLLbll=newExpertInfoBLL(); DataTabledt=bll.GetDepInfo(); foreach(DataRowdrindt.Rows) { RadioButtonList1.Items.Add(dr["Name"].ToString());//循环读出数据库的数据 } this.RadioButtonList1.DataSource=dt; this.RadioButtonList1.DataTextField="Name"; this.RadioButtonList1.DataValueField="Id"; this.RadioButtonList1.RepeatDirection=RepeatDirection.Horizontal; this.RadioButtonList1.DataBind(); } privatevoidGetDataBind(stringId) { //这里写解码和数据库返回结果 TechnologyBLLbll=newTechnologyBLL(); stringstrWhere="1=1"; if(Id!=""&&Id!=null) { strWhere+=string.Format("anda.Depinfo_Id='{0}'",Id); } AspNetPager1.RecordCount=bll.GetCountList(strWhere); //绑定数据 DataTabledt=bll.GetList((AspNetPager1.CurrentPageIndex-1)*AspNetPager1.PageSize,AspNetPager1.PageSize,strWhere,"CreateTime"); this.Repeater1.DataSource=dt; this.Repeater1.DataBind(); } protectedvoidAspNetPager1_PageChanged(objectsender,EventArgse) { GetDataBind(Id); } //根据选择单选按钮的不同id,触发事件 protectedvoidRadioButtonList1_SelectedIndexChanged(objectsender,EventArgse) { stringId; Id=RadioButtonList1.SelectedValue; GetDataBind(Id); } } }
TechnologyBLL层的方法
namespaceBTAppBLL { publicclassTechnologyBLL { TechnologyDALdal=newTechnologyDAL(); publicDataTableGetList(intstartPage,intpageSize,stringwhere,stringorderby) { DataTabledTable=dal.GetList(startPage,pageSize,where,orderby); returndTable; } publicintGetCountList(stringwhere) { intrecord=dal.GetCountList(where); returnrecord; } publicDataTableGetListShow(stringTechnologyId) { DataTabledTable=dal.GetModel(TechnologyId); returndTable; } publicDataTableGetPicture(stringTechnologyId) { DataTabledTable=dal.GetPicture(TechnologyId); returndTable; } } }
TechnologyDAL层的方法
namespaceBTAppDAL { publicclassTechnologyDAL { publicDataTableGetList(intstartPage,intpageSize,stringwhere,stringorderby) { stringstrSql=string.Format("SELECTa.TechnologyId,a.TechnologyName,a.Summarize,a.Effect,a.MainPoint,a.AppropriateArea,a.Attention,a.CreateTime,a.CreatUser,a.UpdateTime,b.NameFROMTechnologyASa\n"+ "leftjoinSys_DepInfoASbONa.Depinfo_Id=b.Id\n"+ "wherea.IsActive='1'and{0}",where); stringproc="proc_CommonPagerWithStatement"; SqlConnectioncon=SqlDbHelper.Connection; SqlParameter[]sp={newSqlParameter("@intStartIndex",startPage), newSqlParameter("@intPageSize",pageSize), newSqlParameter("@varStatement",strSql), newSqlParameter("@varSortExpression",orderby+"DESC")}; DataTabledt=SqlDbHelper.GetDataSet(proc,sp,con); returndt; } publicintGetCountList(stringwhere) { intcountRecord=0; stringstrSql=string.Format("selectCOUNT(TechnologyId)ascountRecordfrom(SELECTa.TechnologyId,a.TechnologyName,a.Summarize,a.Effect,a.MainPoint,a.AppropriateArea,a.Attention,a.CreateTime,a.CreatUser,a.UpdateTime,b.NameFROMTechnologyASa\n"+ "leftjoinSys_DepInfoASbONa.Depinfo_Id=b.Id\n"+ "wherea.IsActive='1'and{0})asc",where); SqlConnectioncon=SqlDbHelper.Connection; try { if(con.State==System.Data.ConnectionState.Closed) con.Open(); DataTabledt=SqlDbHelper.GetDataTable(strSql); if(dt.Rows.Count>0) countRecord=int.Parse(dt.Rows[0]["countRecord"].ToString()); } catch(Exception) { throw; } finally { if(con.State==ConnectionState.Open) { con.Close(); } } returncountRecord; } publicDataTableGetModel(stringTechnologyId) { stringstrSql=string.Format("SELECTa.TechnologyId,a.TechnologyName,a.Summarize,a.Effect,a.MainPoint,a.AppropriateArea,a.Attention,a.CreateTime,a.CreatUser,a.UpdateTime,b.NameFROMTechnologyASa\n"+ "leftjoinSys_DepInfoASbONa.Depinfo_Id=b.Id\n"+ "wherea.IsActive='1'anda.TechnologyId='{0}'",TechnologyId); DataTabledataTable=SqlDbHelper.GetDataTable(strSql); returndataTable; } publicDataTableGetPicture(stringTechnologyId) { stringstrSql=string.Format("SELECTTOP5a.Files_Id,a.Files_Name,a.Files_PathFROMdbo.Com_FilesASa\n"+ "LEFTJOINdbo.TechnologyASbONa.ForeignKey_Id=b.TechnologyId\n"+ "WHEREb.IsActive=1anda.ForeignKey_Id='{0}'",TechnologyId); DataTabledataTable=SqlDbHelper.GetDataTable(strSql); returndataTable; } } }
ExpertInfoBLL层的方法
publicDataTableGetDepInfo() { DataTabledTable=dal.GetDepInfo(); returndTable; }
ExpertInfoDAL层的方法
publicDataTableGetDepInfo() { try { StringBuilderstr=newStringBuilder(@"SELECTId,NameFROMdbo.Sys_DepInfoWHEREIs_Active='1'ANDDepinfoType='1'"); DataTabledata=SqlDbHelper.GetDataTable(str.ToString()); if(data.Rows.Count>0) { returndata; } else { returnnull; } } catch(Exception) { returnnull; } }
在页面加载的时候调用DropDownListDataBind()方法
触发RadioButtonList的点击事件
protectedvoidRadioButtonList1_SelectedIndexChanged(objectsender,EventArgse) { stringId; Id=RadioButtonList1.SelectedValue; GetDataBind(Id); }
既可以实现点击某个单选按钮,并触发事件。
以上就是本文的全部内容,希望对大家的学习有所帮助。