下载

0下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 用多个DropDownList分别来绑定多个年月日

用多个DropDownList分别来绑定多个年月日.doc

用多个DropDownList分别来绑定多个年月日

乐观的我的爱流离失所
2019-05-09 0人阅读 举报 0 0 0 暂无简介

简介:本文档为《用多个DropDownList分别来绑定多个年月日doc》,可适用于综合领域

用多个DropDownList分别来绑定多个年月日首先看看效果:年与月的数据源,较好定义,而日的数据源即需要根据年与月选择之后,方可获取到那年月的所有日数。如年:List<int>iYear{get{List<int>y=newList<int>()intNy=DateTimeNowYearfor(inti=i<=Nyi){yAdd(i)}returny}}月的数据源:List<int>iMonth{get{List<int>m=newList<int>()for(inti=i<=i){mAdd(i)}returnm}}日的数据源:publicList<int>iDay(intyear,intmonth){intdays=DateTimeDaysInMonth(year,month)List<int>d=newList<int>()for(inti=i<=daysi){dAdd(i)}returnd}由于多个地方是DropDownList数据绑定,因此InsusNET先写一个通用DropDownList数据绑定的方法:privatevoidDropDownlistParse(DropDownListddl,objectdataSource,stringdataTextField,stringdataValueField){ddlDataSource=dataSourceddlDataTextField=dataTextFieldddlDataValueField=dataValueFieldddlDataBind()ddlItemsInsert(,newListItem(stringEmpty,stringEmpty))}这样的话,在需要绑定的地方,即管传入参数即可。如在PageLoad时,需要对年与月的DropDownList数据绑定:protectedvoidPageLoad(objectsender,EventArgse){if(!IsPostBack){DataBinding()}}privatevoidDataBinding(){DropDownlistParse(thisDropDownListYear,iYearSelect(y=>new{value=y})ToList(),"value","value")DropDownlistParse(thisDropDownListMonth,iMonthSelect(m=>new{value=m})ToList(),"value","value")thisDropDownListDayItemsInsert(,newListItem(stringEmpty,stringEmpty))}在年或月DropDownlist控件有异动时,作相应对日的DropDownList控件数据绑定:protectedvoidDropDownListYearSelectedIndexChanged(objectsender,EventArgse){DayDataBinding()}protectedvoidDropDownListMonthSelectedIndexChanged(objectsender,EventArgse){DayDataBinding()}privatevoidDayDataBinding(){if(thisDropDownListYearSelectedIndex==||stringIsOrEmpty(thisDropDownListYearSelectedItemValue))returnif(thisDropDownListMonthSelectedIndex==||stringIsOrEmpty(thisDropDownListMonthSelectedItemValue))returninty=ConvertToInt(thisDropDownListYearSelectedItemValue)intm=ConvertToInt(thisDropDownListMonthSelectedItemValue)DropDownlistParse(thisDropDownListDay,iDay(y,m)Select(d=>new{value=d})ToList(),"value","value")}我们还要为了获取年月日三个DropDownList控件的值,因此写还得写三个属性:privateintyearprivateintmonthprivateintdaypublicintYear{get{if(ViewState"Year"!=){returnConvertToInt(ViewState"Year")}if(thisDropDownListYearSelectedIndex==||stringIsOrEmpty(thisDropDownListYearSelectedItemValue))returnelsereturnConvertToInt(thisDropDownListYearSelectedItemValue)}set{ViewState"Year"=value}}publicintMonth{get{if(ViewState"Month"!=){returnConvertToInt(ViewState"Month")}if(thisDropDownListMonthSelectedIndex==||stringIsOrEmpty(thisDropDownListMonthSelectedItemValue))returnelsereturnConvertToInt(thisDropDownListMonthSelectedItemValue)}set{ViewState"Month"=value}}publicintDay{get{if(ViewState"Day"!=){returnConvertToInt(ViewState"Day")}if(thisDropDownListDaySelectedIndex==||stringIsOrEmpty(thisDropDownListDaySelectedItemValue))returnelsereturnConvertToInt(thisDropDownListDaySelectedItemValue)}set{ViewState"Day"=value}}最后是ASCX的html:<ControlLanguage="C#"AutoEventWireup="true"CodeFile="InsusDateascxcs"Inherits="InsusDate"><asp:DropDownListID="DropDownListYear"runat="server"AutoPostBack="true"OnSelectedIndexChanged="DropDownListYearSelectedIndexChanged"Width="px"><asp:DropDownList>年<asp:DropDownListID="DropDownListMonth"runat="server"AutoPostBack="true"OnSelectedIndexChanged="DropDownListMonthSelectedIndexChanged"Width="PX"><asp:DropDownList>月<asp:DropDownListID="DropDownListDay"runat="server"Width="PX"><asp:DropDownList>日完整的ASCXcs:usingSystemusingSystemCollectionsGenericusingSystemLinqusingSystemWebusingSystemWebUIusingSystemWebUIWebControlspublicpartialclassInsusDate:SystemWebUIUserControl{privateintyearprivateintmonthprivateintdaypublicintYear{get{if(ViewState"Year"!=){returnConvertToInt(ViewState"Year")}if(thisDropDownListYearSelectedIndex==||stringIsOrEmpty(thisDropDownListYearSelectedItemValue))returnelsereturnConvertToInt(thisDropDownListYearSelectedItemValue)}set{ViewState"Year"=value}}publicintMonth{get{if(ViewState"Month"!=){returnConvertToInt(ViewState"Month")}if(thisDropDownListMonthSelectedIndex==||stringIsOrEmpty(thisDropDownListMonthSelectedItemValue))returnelsereturnConvertToInt(thisDropDownListMonthSelectedItemValue)}set{ViewState"Month"=value}}publicintDay{get{if(ViewState"Day"!=){returnConvertToInt(ViewState"Day")}if(thisDropDownListDaySelectedIndex==||stringIsOrEmpty(thisDropDownListDaySelectedItemValue))returnelsereturnConvertToInt(thisDropDownListDaySelectedItemValue)}set{ViewState"Day"=value}}protectedvoidPageLoad(objectsender,EventArgse){if(!IsPostBack){DataBinding()}}privatevoidDataBinding(){DropDownlistParse(thisDropDownListYear,iYearSelect(y=>new{value=y})ToList(),"value","value")DropDownlistParse(thisDropDownListMonth,iMonthSelect(m=>new{value=m})ToList(),"value","value")thisDropDownListDayItemsInsert(,newListItem(stringEmpty,stringEmpty))}protectedvoidDropDownListYearSelectedIndexChanged(objectsender,EventArgse){DayDataBinding()}protectedvoidDropDownListMonthSelectedIndexChanged(objectsender,EventArgse){DayDataBinding()}privatevoidDayDataBinding(){if(thisDropDownListYearSelectedIndex==||stringIsOrEmpty(thisDropDownListYearSelectedItemValue))returnif(thisDropDownListMonthSelectedIndex==||stringIsOrEmpty(thisDropDownListMonthSelectedItemValue))returninty=ConvertToInt(thisDropDownListYearSelectedItemValue)intm=ConvertToInt(thisDropDownListMonthSelectedItemValue)DropDownlistParse(thisDropDownListDay,iDay(y,m)Select(d=>new{value=d})ToList(),"value","value")}privatevoidDropDownlistParse(DropDownListddl,objectdataSource,stringdataTextField,stringdataValueField){ddlDataSource=dataSourceddlDataTextField=dataTextFieldddlDataValueField=dataValueFieldddlDataBind()ddlItemsInsert(,newListItem(stringEmpty,stringEmpty))}List<int>iYear{get{List<int>y=newList<int>()intNy=DateTimeNowYearfor(inti=i<=Nyi){yAdd(i)}returny}}List<int>iMonth{get{List<int>m=newList<int>()for(inti=i<=i){mAdd(i)}returnm}}publicList<int>iDay(intyear,intmonth){intdays=DateTimeDaysInMonth(year,month)List<int>d=newList<int>()for(inti=i<=daysi){dAdd(i)}returnd}}可以把ASCX保存为一个ASCX用户控件,在网页拉进去即可。

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

评分:

/19

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利