gridview的年龄过滤器无法正常工作

时间:2016-12-13 15:31:27

标签: c# sql asp.net gridview

我有一个gridview,显示我数据库中的表格中的一些数据,convertys dob到age,我有一个下拉列表作为过滤器,所以我可以选择一个年龄组中的子项。第一个年龄组6-10工作正常,11-13也显示在11岁以下。并且14-16做同样的事情(基本上显示每个人)。我已经包含了我的.cs代码以及我的源代码,以及网页外观的截图,有人可以在我的代码中指出我需要更改的内容,以便正确过滤年龄?感谢。

enter image description here

.CS CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace Coursework
{
public partial class Testy1 : System.Web.UI.Page
{
    //create a datasource
    SqlDataSource source = new SqlDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        //always set some defaults for the sqldatasource
        source.ID = "source1";
        source.ConnectionString = ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionStr‌​ing;
        source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age";

        if (!IsPostBack)
        {
            //bind the grid
            GridView1.DataSource = source;
            GridView1.DataBind();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //the new database query, now with where clause
        source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children WHERE (DATEDIFF(hour, dob, GETDATE()) / 8766 BETWEEN @start AND @end) ORDER BY age";

        //get the end age from the dropdown and cast as int
        int end = Convert.ToInt32(DropDownList1.SelectedValue);

        //get the start int for the filter
        int start = end - 11;

        //if the filter is resetted, make sure the query returns all ages
        if (end == 6)
        {
            start = 6;
            end = 99;
        }

        //replace the parameters in the query
        source.SelectParameters.Add("start", start.ToString());
        source.SelectParameters.Add("end", end.ToString());

        //rebind the grid
        GridView1.DataSource = source;
        GridView1.DataBind();
    }
}

}

消息来源代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Testy1.aspx.cs" Inherits="Coursework.Testy1" %>

         

<asp:ListItem Text="6 - 10" Value="10"></asp:ListItem>
<asp:ListItem Text="11 - 13" Value="13"></asp:ListItem>
<asp:ListItem Text="14 - 16" Value="16"></asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
    <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
            <%# Eval("firstname") %>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="DOB">
        <ItemTemplate>
            <%# Convert.ToDateTime(Eval("dob")).ToString("d MMMM yyyy") %>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Age">
        <ItemTemplate>
            <%# Eval("age") %>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

1 个答案:

答案 0 :(得分:1)

您需要检查代码:

    //get the end age from the dropdown and cast as int
    int end = Convert.ToInt32(DropDownList1.SelectedValue);

    //get the start int for the filter
    int start = end - 11;

    //if the filter is reset, make sure the query returns all ages
    if (end == 6)
    {
        start = 6;
        end = 99;
    }

更具体地说,以下一行:int start = end - 11;

应该清楚你需要改变什么。