月选取器

分享于 

10分钟阅读

Web开发

  繁體

介绍

在本文的最后,你将能够创建自己的月选取器。

为什么MonthYear选取器

在创建报告时,我们通常是从和到日期过滤器,因为我们使用我们的。 但是最近我得到了一个要求,我想把""给"至月"过滤器来做一个报告。 这可以通过使用DateTimePicker实现,对它的JavaScript进行一些修改。 但在某些情况下,我失败并决定创建自己的usercontrol。

使用的技术

.NET 3.5框架,AJAX。

月选择器控件


按 below 步骤进行收费以创建月选取器。

步骤 1.创建用户控件

1.右键单击项目并单击add添加新项目""
2.选择Web用户控件并将它的命名为" monthyearpicker.ascx",然后单击添加。
3。现在将 below 代码放入用户控件的designer


 <asp:UpdatePanel ID="UpdatePanel1" runat="server">


 <ContentTemplate>


 <asp:TextBox ID="txtValue" runat="server" Enabled="false" BackColor="White">


 </asp:TextBox>


 <asp:Button ID="btnSelect" runat="server" Text="..." OnClick="btnSelect_Click" />


 <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">


 <ProgressTemplate>


 loading...


 </ProgressTemplate>


 </asp:UpdateProgress>


 <asp:Panel ID="pnlDate" runat="server" Visible="false" CssClass="DatePanel">


 <asp:DropDownList ID="ddlMonth" runat="server">


 </asp:DropDownList>


 <asp:DropDownList ID="ddlYear" runat="server">


 </asp:DropDownList>


 <asp:Button ID="btnSet" runat="server" Text="Set" OnClick="btnSet_Click" />


 </asp:Panel>


 </ContentTemplate>


</asp:UpdatePanel> 



4.将 below 代码放置到用户控件的behind 代码中


private string _TextBoxCss;


public string TextBoxCss


{


 get { return _TextBoxCss; }


 set { _TextBoxCss = value; }


}



private string _SelectButtonCss;


public string SelectButtonCss


{


 get { return _SelectButtonCss; }


 set { _SelectButtonCss = value; }


}



private string _SetButtonCss;


public string SetButtonCss


{


 get { return _SetButtonCss; }


 set { _SetButtonCss = value; }


}



private string _PanelCss;


public string PanelCss


{


 get { return _PanelCss; }


 set { _PanelCss = value; }


}



private int _MinYear;


public int MinYear


{


 get { return _MinYear; }


 set { _MinYear = value; }


}



private int _MaxYear;


public int MaxYear


{


 get { return _MaxYear; }


 set { _MaxYear = value; }


}



private int _MinMonth;


public int MinMonth


{


 get { return _MinMonth; }


 set { _MinMonth = value; }


}



private int _MaxMonth;


public int MaxMonth


{


 get { return _MaxMonth; }


 set { _MaxMonth = value; }


}



private string _Value;


public string Value


{


 get


 {


 _Value = GetSelectMonthYear();


 return _Value;


 }


 set { _Value = value; }


}



protected void Page_Init(object sender, EventArgs e)


{


 if (!IsPostBack)


 {


 txtValue.CssClass = _TextBoxCss;


 btnSelect.CssClass = _SelectButtonCss;


 btnSet.CssClass = _SetButtonCss;


 pnlDate.CssClass = _PanelCss;



 ddlYear.Items.Clear();


 _MinYear = _MinYear == 0? DateTime.MinValue.Year : _MinYear;


 _MaxYear = _MaxYear == 0? DateTime.MaxValue.Year : _MaxYear;


 for (int i = _MinYear; i <= _MaxYear; i++)


 ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));



 ddlMonth.Items.Clear();


 _MinMonth = _MinMonth == 0? DateTime.MinValue.Month : _MinMonth;


 _MaxMonth = _MaxMonth == 0? DateTime.MaxValue.Month : _MaxMonth;


 for (int i = _MinMonth; i <= _MaxMonth; i++)


 ddlMonth.Items.Add(new ListItem(GetMonth(i), i.ToString()));



 }


}



private string GetSelectMonthYear()


{


 string _ReturnValue = string.Empty;


 string _Month = string.Empty;



 if (!string.IsNullOrEmpty(txtValue.Text))


 {


 string[] _strValue = txtValue.Text.Split(' ');



 switch (Convert.ToString(_strValue[0]).ToLower())


 {


 case "jan":


 _Month = "01";


 break;


 case "feb":


 _Month = "02";


 break;


 case "mar":


 _Month = "03";


 break;


 case "apr":


 _Month = "04";


 break;


 case "may":


 _Month = "05";


 break;


 case "jun":


 _Month = "06";


 break;


 case "jul":


 _Month = "07";


 break;


 case "aug":


 _Month = "08";


 break;


 case "sep":


 _Month = "09";


 break;


 case "oct":


 _Month = "10";


 break;


 case "nov":


 _Month = "11";


 break;


 case "dec":


 _Month = "12";


 break;


 default:


 break;


 }


 if (!(string.IsNullOrEmpty(_Month) & string.IsNullOrEmpty(Convert.ToString(_strValue[1]))))


 {


 _ReturnValue = _Month + "," + Convert.ToString(_strValue[1]);


 }



 }


 return _ReturnValue;


}



private string GetMonth(int Month)


{


 string _ReturnValue = string.Empty;


 switch (Month)


 {


 case 1:


 _ReturnValue = "Jan";


 break;


 case 2:


 _ReturnValue = "Feb";


 break;


 case 3:


 _ReturnValue = "Mar";


 break;


 case 4:


 _ReturnValue = "Apr";


 break;


 case 5:


 _ReturnValue = "May";


 break;


 case 6:


 _ReturnValue = "Jun";


 break;


 case 7:


 _ReturnValue = "Jul";


 break;


 case 8:


 _ReturnValue = "Aug";


 break;


 case 9:


 _ReturnValue = "Sep";


 break;


 case 10:


 _ReturnValue = "Oct";


 break;


 case 11:


 _ReturnValue = "Nov";


 break;


 case 12:


 _ReturnValue = "Dec";


 break;


 default:


 break;


 }


 return _ReturnValue;


}



protected void btnSelect_Click(object sender, EventArgs e)


{


 pnlDate.Visible =!pnlDate.Visible;


 if (pnlDate.Visible)


 {


 ddlMonth.SelectedValue = DateTime.Now.Month.ToString();


 ddlYear.SelectedValue = DateTime.Now.Year.ToString();


 }


}



protected void btnSet_Click(object sender, EventArgs e)


{


 txtValue.Text = ddlMonth.SelectedItem.Text + "" + ddlYear.SelectedValue;


 pnlDate.Visible = false;


}




步骤 2.创建要放置这里控件的页面

1.右键单击项目并单击add添加新项目""
2.选择Web窗体并将它的命名为" default1.aspx"并单击 Add"。

4.register 使用 below 代码 上面 <html> 创建用户控件


<%@ Register Src="~/UserControl/MonthYearPicker.ascx" TagName="MonthYearPicker" TagPrefix="myp" %>



3。现在将 below 代码放入页面的设计器部分


 <html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">


 <title>Month Year Picker</title>


 <style type="text/css">


. DatePanel


 {


 position: absolute;


 background-color: #FFFFFF;


 border: 1px solid #646464;


 color: #000000;


 z-index: 1;


 font-family: tahoma,verdana,helvetica;


 font-size: 11px;


 padding: 4px;


 text-align: center;


 cursor: default;


 line-height: 20px;


 }


 </style>


</head>


<body>


 <form id="form1" runat="server">


 <asp:ScriptManager ID="ScriptManager1" runat="server">


 </asp:ScriptManager>


 <div>


 <table>


 <tr>


 <td>


 Month Year


 </td>


 <td>


 <myp:MonthYearPicker ID="mypMonthYear" runat="server" PanelCss="DatePanel" MinYear="2011"



                        MaxYear="2020" />


 </td>


 </tr>


 <tr>


 <td colspan="2">


 Show Value


 <asp:Button ID="btnShow" runat="server" Text ="Show" OnClick="btnShow_Click" />


 </td>


 </tr>


 </table>


 </div>


 </form>


</body>


</html> 



4.将 below 代码放置到页面的behind 代码中。


protected void btnShow_Click(object sender, EventArgs e)


 {


 Response.Write(mypMonthYear.Value);


 }




这就是你不需要你自己的月挑选器。


pick  挖掘  
相关文章