asp.net+access实现DropDownList与RadDatePicker同步筛选
这里没有使用SqlServer是因为老师要求使用access。
前台代码
<table style="margin:auto"> <tr><td class="auto-style3"><asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Newscategory" AutoPostBack="True"> </asp:DropDownList></td><td> <telerik:RadDatePicker ID="RadDatePicker1" runat="server" AutoPostBack="True" Culture="zh-CN" HiddenInputTitleAttibute="Visually hidden input created for functionality purposes." WrapperTableSummary="Table holding date picker control for selection of dates."> <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x"></Calendar> <DateInput DisplayDateFormat="yyyy-MM-dd" DateFormat="yyyy-MM-dd" LabelWidth="40%" AutoPostBack="True"></DateInput> <DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton> </telerik:RadDatePicker> </td></tr> <tr><td class="auto-style4">新闻类别</td><td class="auto-style2">新闻标题</td><td class="auto-style2">发布时间</td></tr> <asp:ListView ID="ListView1" runat="server"> <EmptyDataTemplate> <div style="margin-left: 600px;">搜索不到有关内容</div> </EmptyDataTemplate> <ItemTemplate><tr><td class="css"><%#Eval("Newscategory") %></td><td class="css"><a href="News2.aspx?id=<%#Eval("ID") %>"><%#Eval("Newstitle") %></td><td class="css"><%#Eval("Releasetime") %></td></tr></ItemTemplate> </asp:ListView> </table>
这里使用了telerik第三方控件,所以需要写配置文件
<system.webServer> <validation validateIntegratedModeConfiguration="false"/> <handlers> <remove name="ChartImageHandler"/> <add name="Telerik_Web_UI_WebResource_axd" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource"/> <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </handlers> <directoryBrowse enabled="true"/> <defaultDocument> <files> <add value="Login.aspx"/> </files> </defaultDocument> </system.webServer>
还需要在html中加一段
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
这样才可以使用telerik控件。
效果图如上,使用listview绑定数据。
后台代码
public static readonly string connStr1 = "Provider = Microsoft.Jet.OLEDB.4.0 ;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/News.mdb");//链接数据库 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { data();//显示下拉框数据 } if (DropDownList1.SelectedItem.Text == "--请选择--")//判断下拉框的默认选项是否为“请选择” { News1(); if (RadDatePicker1.DateInput.Text == "")//如果是则显示所有数据 { News1(); } else//如果否则使用RadDatePicker控件进行对日期的筛选 { OleDbConnection connection = new OleDbConnection(connStr1); string sql = "select * from News where Releasetime = '" + RadDatePicker1.DateInput.DisplayText + "'"; OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); myadapter.Fill(ds); this.ListView1.DataSource = ds; this.ListView1.DataBind(); } } else//如果不为“请选择”则执行news方法. { news(); } } private void News1()//显示所有数据 { OleDbConnection connection = new OleDbConnection(connStr1); string sql = "select * from News"; OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); myadapter.Fill(ds); this.ListView1.DataSource = ds; this.ListView1.DataBind(); } private void news()//筛选数据 { if (RadDatePicker1.DateInput.Text == "")//如果RadDatePicker控件没有被使用则执行DropDownList进行分类筛选 { OleDbConnection connection = new OleDbConnection(connStr1); string sql = "select * from News where Newscategory = '" + DropDownList1.SelectedValue + "'"; OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); myadapter.Fill(ds); this.ListView1.DataSource = ds; this.ListView1.DataBind(); } else//否则同时执行DropDownList和RadDatePicker进行筛选 { OleDbConnection connection = new OleDbConnection(connStr1); string sql = "select * from News where Newscategory = '" + DropDownList1.SelectedValue + "' and Releasetime = '" + RadDatePicker1.DateInput.DisplayText + "'"; OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); myadapter.Fill(ds); this.ListView1.DataSource = ds; this.ListView1.DataBind(); } } private void data()//给DropDownList绑定数据 { OleDbConnection connection = new OleDbConnection(connStr1); string sql = "select * from News"; OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); myadapter.Fill(ds); this.DropDownList1.DataSource = ds; this.DropDownList1.DataBind(); DropDownList1.Items.Insert(0, new ListItem("--请选择--", "")); DropDownList1.Items.FindByText("--请选择--").Selected = true; }
演示效果:
没有任何筛选情况下显示所有数据
使用DropDownList进行筛选
使用DropDownList加RadDatePicker进行筛选
使用RadDatePicker进行筛选
以上属于比较简单的实现方法,有什么不足希望大家提出。