博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用多个DropDownList分别来绑定多个年月日
阅读量:7096 次
发布时间:2019-06-28

本文共 7313 字,大约阅读时间需要 24 分钟。

首先看看效果:

 

年与月的数据源,较好定义,而日的数据源即需要根据年与月选择之后,方可获取到那年月的所有日数。如年:

Year
 List<
int> i_Year
    {
        
get
        {
            List<
int> y = 
new List<
int>();
            
int Ny = DateTime.Now.Year;
            
for (
int i = 
1953; i <= Ny; i++)
            {
                y.Add(i);
            }
            
return y;
        }
    }

 

月的数据源:

Month
 List<
int> i_Month
    {
        
get
        {
            List<
int> m = 
new List<
int>();
            
for (
int i = 
1; i <= 
12; i++)
            {
                m.Add(i);
            }
            
return m;
        }
    }

 

日的数据源:

Day
public List<
int> i_Day(
int year, 
int month)
    {
        
int days = DateTime.DaysInMonth(year, month);
        List<
int> d = 
new List<
int>();
        
for (
int i = 
1; i <= days; i++)
        {
            d.Add(i);
        }
        
return d;
    }

由于多个地方是DropDownList数据绑定,因此Insus.NET先写一个通用DropDownList数据绑定的方法:

DropDownlistParse
 
private 
void DropDownlistParse(DropDownList ddl, 
object dataSource, 
string dataTextField, 
string dataValueField)
    {
        ddl.DataSource = dataSource;
        ddl.DataTextField = dataTextField;
        ddl.DataValueField = dataValueField;
        ddl.DataBind();
        ddl.Items.Insert(
0
new ListItem(
string.Empty, 
string.Empty));
    }

 

这样的话,在需要绑定的地方,即管传入参数即可。如在Page_Load时,需要对年与月的DropDownList数据绑定:

View Code
  
protected 
void Page_Load(
object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            Data_Binding();
        }
    }
    
private 
void Data_Binding()
    {
        DropDownlistParse(
this.DropDownListYear, i_Year.Select(y => 
new { value = y }).ToList(), 
"
value
"
"
value
");
        DropDownlistParse(
this.DropDownListMonth, i_Month.Select(m => 
new { value = m }).ToList(), 
"
value
"
"
value
");
        
this.DropDownListDay.Items.Insert(
0
new ListItem(
string.Empty, 
string.Empty));
    }

 

在年或月DropDownlist控件有异动时,作相应对日的DropDownList控件数据绑定:

View Code
 
protected 
void DropDownListYear_SelectedIndexChanged(
object sender, EventArgs e)
    {
        DayDataBinding();
    }
    
protected 
void DropDownListMonth_SelectedIndexChanged(
object sender, EventArgs e)
    {
        DayDataBinding();
    }
    
private 
void DayDataBinding()
    {
        
if (
this.DropDownListYear.SelectedIndex == -
1 || 
string.IsNullOrEmpty (
this.DropDownListYear.SelectedItem.Value)) 
return;
        
if (
this.DropDownListMonth.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListMonth.SelectedItem.Value)) 
return;
        
        
int y = Convert.ToInt32(
this.DropDownListYear.SelectedItem.Value);
        
int m = Convert.ToInt32(
this.DropDownListMonth.SelectedItem.Value);
        DropDownlistParse(
this.DropDownListDay, i_Day(y, m).Select(d => 
new { value = d }).ToList(), 
"
value
"
"
value
");
    }

 

我们还要为了获取年月日三个DropDownList控件的值,因此写还得写三个属性:

View Code
 
private 
int? _year;
    
private 
int? _month;
    
private 
int? _day;
    
public 
int? Year
    {
        
get
        {
            
if (ViewState[
"
Year
"] != 
null)
            {
                
return Convert.ToInt32(ViewState[
"
Year
"]);
            }
            
if (
this.DropDownListYear.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListYear.SelectedItem.Value))
                
return 
null;
            
else
                
return Convert.ToInt32(
this.DropDownListYear.SelectedItem.Value);
        }
        
set
        {
            ViewState[
"
Year
"] = value;
        }
    }
    
public 
int? Month
    {
        
get
        {
            
if (ViewState[
"
Month
"] != 
null)
            {
                
return Convert.ToInt32(ViewState[
"
Month
"]);
            }
            
if (
this.DropDownListMonth.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListMonth.SelectedItem.Value))
                
return 
null;
            
else
                
return Convert.ToInt32(
this.DropDownListMonth.SelectedItem.Value);
        }
        
set
        {
            ViewState[
"
Month
"] = value;
        }
    }
    
public 
int? Day
    {
        
get
        {
            
if (ViewState[
"
Day
"] != 
null)
            {
                
return Convert.ToInt32(ViewState[
"
Day
"]);
            }
            
if (
this.DropDownListDay.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListDay.SelectedItem.Value))
                
return 
null;
            
else
                
return Convert.ToInt32(
this.DropDownListDay.SelectedItem.Value);
        }
        
set
        {
            ViewState[
"
Day
"] = value;
        }
    }

 

最后是ASCX的html:

View Code
<%
@ Control Language
=
"
C#
"
 AutoEventWireup
=
"
true
"
 CodeFile
=
"
InsusDate.ascx.cs
"
 Inherits
=
"
InsusDate
"
 
%>
<
asp:DropDownList 
ID
="DropDownListYear"
 runat
="server"
 AutoPostBack
="true"
 OnSelectedIndexChanged
="DropDownListYear_SelectedIndexChanged"
 Width
="60px"
></
asp:DropDownList
>
<
asp:DropDownList 
ID
="DropDownListMonth"
 runat
="server"
 AutoPostBack
="true"
 OnSelectedIndexChanged
="DropDownListMonth_SelectedIndexChanged"
 Width
="40PX"
></
asp:DropDownList
>
<
asp:DropDownList 
ID
="DropDownListDay"
 runat
="server"
 Width
="40PX"
></
asp:DropDownList
>

 

完整的ASCX.cs:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public 
partial 
class InsusDate : System.Web.UI.UserControl
{
    
private 
int? _year;
    
private 
int? _month;
    
private 
int? _day;
    
public 
int? Year
    {
        
get
        {
            
if (ViewState[
"
Year
"] != 
null)
            {
                
return Convert.ToInt32(ViewState[
"
Year
"]);
            }
            
if (
this.DropDownListYear.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListYear.SelectedItem.Value))
                
return 
null;
            
else
                
return Convert.ToInt32(
this.DropDownListYear.SelectedItem.Value);
        }
        
set
        {
            ViewState[
"
Year
"] = value;
        }
    }
    
public 
int? Month
    {
        
get
        {
            
if (ViewState[
"
Month
"] != 
null)
            {
                
return Convert.ToInt32(ViewState[
"
Month
"]);
            }
            
if (
this.DropDownListMonth.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListMonth.SelectedItem.Value))
                
return 
null;
            
else
                
return Convert.ToInt32(
this.DropDownListMonth.SelectedItem.Value);
        }
        
set
        {
            ViewState[
"
Month
"] = value;
        }
    }
    
public 
int? Day
    {
        
get
        {
            
if (ViewState[
"
Day
"] != 
null)
            {
                
return Convert.ToInt32(ViewState[
"
Day
"]);
            }
            
if (
this.DropDownListDay.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListDay.SelectedItem.Value))
                
return 
null;
            
else
                
return Convert.ToInt32(
this.DropDownListDay.SelectedItem.Value);
        }
        
set
        {
            ViewState[
"
Day
"] = value;
        }
    }
    
protected 
void Page_Load(
object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            Data_Binding();
        }
    }
    
private 
void Data_Binding()
    {
        DropDownlistParse(
this.DropDownListYear, i_Year.Select(y => 
new { value = y }).ToList(), 
"
value
"
"
value
");
        DropDownlistParse(
this.DropDownListMonth, i_Month.Select(m => 
new { value = m }).ToList(), 
"
value
"
"
value
");
        
this.DropDownListDay.Items.Insert(
0
new ListItem(
string.Empty, 
string.Empty));
    }
    
protected 
void DropDownListYear_SelectedIndexChanged(
object sender, EventArgs e)
    {
        DayDataBinding();
    }
    
protected 
void DropDownListMonth_SelectedIndexChanged(
object sender, EventArgs e)
    {
        DayDataBinding();
    }
    
private 
void DayDataBinding()
    {
        
if (
this.DropDownListYear.SelectedIndex == -
1 || 
string.IsNullOrEmpty (
this.DropDownListYear.SelectedItem.Value)) 
return;
        
if (
this.DropDownListMonth.SelectedIndex == -
1 || 
string.IsNullOrEmpty(
this.DropDownListMonth.SelectedItem.Value)) 
return;
        
        
int y = Convert.ToInt32(
this.DropDownListYear.SelectedItem.Value);
        
int m = Convert.ToInt32(
this.DropDownListMonth.SelectedItem.Value);
        DropDownlistParse(
this.DropDownListDay, i_Day(y, m).Select(d => 
new { value = d }).ToList(), 
"
value
"
"
value
");
    }
    
private 
void DropDownlistParse(DropDownList ddl, 
object dataSource, 
string dataTextField, 
string dataValueField)
    {
        ddl.DataSource = dataSource;
        ddl.DataTextField = dataTextField;
        ddl.DataValueField = dataValueField;
        ddl.DataBind();
        ddl.Items.Insert(
0
new ListItem(
string.Empty, 
string.Empty));
    }
    List<
int> i_Year
    {
        
get
        {
            List<
int> y = 
new List<
int>();
            
int Ny = DateTime.Now.Year;
            
for (
int i = 
1953; i <= Ny; i++)
            {
                y.Add(i);
            }
            
return y;
        }
    }
    List<
int> i_Month
    {
        
get
        {
            List<
int> m = 
new List<
int>();
            
for (
int i = 
1; i <= 
12; i++)
            {
                m.Add(i);
            }
            
return m;
        }
    }
    
public List<
int> i_Day(
int year, 
int month)
    {
        
int days = DateTime.DaysInMonth(year, month);
        List<
int> d = 
new List<
int>();
        
for (
int i = 
1; i <= days; i++)
        {
            d.Add(i);
        }
        
return d;
    }
}

 

可以把ASCX保存为一个ASCX用户控件,在网页拉进去即可。

转载地址:http://rfxql.baihongyu.com/

你可能感兴趣的文章
SQLserver 2008同步复制创建后新增表/函数/存储过程(不重新初始化快照)
查看>>
我们一起清除过的浮动
查看>>
python 实现(简单的一个购物商城小程序)
查看>>
bzoj4025 二分图
查看>>
文档加密、解密jar包
查看>>
Java 8 字符串日期排序
查看>>
了解Python
查看>>
Java遇见HTML——JSP篇之JSP基础语法
查看>>
a common method to rotate the image
查看>>
测试计划
查看>>
深拷贝与浅拷贝
查看>>
textarea禁止拖动 滚动条隐藏
查看>>
Java下利用Jackson进行JSON解析和序列化
查看>>
Js用正则表达式验证字符串
查看>>
大疆农业专家在线空开课直播课件知识
查看>>
怎样快速搜索自己所需的资料?(90%的人不会使用此方法)[转]
查看>>
POJ_2411_Mondriaan's Dream_状态压缩dp
查看>>
694. Number of Distinct Islands - Medium
查看>>
vue打包后出现的.map文件
查看>>
前端应用框架(三)
查看>>