我正在使用Visual Studio在C#中创建一个asp.net Web应用程序。我有一个注册页面,孩子们可以注册,然后将他们的详细信息发送到我的数据库中的“子”表,点击“查看注册的孩子”按钮后,会打开一个新页面,显示名字,DOB和用户名( pk)每个孩子都注册了。事实上,DOB存储并读作'04 / 02/2006'。我现在需要找到一种方法来从DOB获得年龄,并能够按年龄组(6-10,11-13,14-16)查看孩子。
我想知道最简单的方法是什么?每个年龄组的单选按钮,选中后,仅显示gridview中该年龄组的子项。或者也许当页面加载并且所有子项都显示在gridview上时,它们已经被分类到年龄组中了?
有人可以告诉我最容易实现这一目标的方法,记住我是C#& asp.net!我附上了gridview目前看起来的截图。提前谢谢。
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";
SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);
source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM table 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 table 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 - 5;
//if the filter is resetted, make sure the query returns all ages
if (end == 0)
{
start = 0;
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();
}
}
}
答案 0 :(得分:2)
您可以从此代码段开始。您需要做的第一件事就是编辑从数据库中获取数据的查询。添加以下内容:DATEDIFF(hour, dob, GETDATE()) / 8766 as AGE
,这将为您提供数据库中人员的年龄。然后,您可以使用它按年龄进行过滤。
然后将DropDownList添加到包含OnSelectedIndexChanged
事件且AutoPostback
设置为true
的网页。
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Filter age" Value="0"></asp:ListItem>
<asp:ListItem Text="0 - 5" Value="5"></asp:ListItem>
<asp:ListItem Text="6 - 10" Value="10"></asp:ListItem>
<asp:ListItem Text="10 - 15" Value="15"></asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("firstname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# Convert.ToDateTime(Eval("dob")).ToString("d MMMM yyyy") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("age") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然后在代码背后
using System;
......
namespace Project1
{
public partial class WebForm1: 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 = "connectionString";
source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM table 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 table 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 - 5;
//if the filter is resetted, make sure the query returns all ages
if (end == 0)
{
start = 0;
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();
}
}
}