可见,我们需要在页面上调用的方法就是GetList(query)方法了,我们看下页面文件SqlFilter.aspx的主要代码:
<asp:ObjectDataSource ID="objdsOrderList" runat="server" SelectMethod="GetList"
TypeName="OrderManager" OnSelecting="objdsOrderList_Selecting">
<SelectParameters>
<asp:Parameter Name="query" Type="string" />
</SelectParameters>
</asp:ObjectDataSource>
ObjectDataSource使用GetList作为SelectCommand, ObjectDataSource的ID将会用于GridView的DataSourceID。
现在我们继续看SqlFilter.aspx的后置代码通常是什么样的(我们设置当DropDownList的Text为“全部”的时候,它的Value为“0”):
public partial class SqlApproach : System.Web.UI.Page
{
public int Year{
get { return Convert.ToInt32(ddlYear.SelectedValue); }
}
public int Month{
get { return Convert.ToInt32(ddlMonth.SelectedValue); }
}
public int Day{
get { return Convert.ToInt32(ddlDay.SelectedValue); }
}
// 获取查询语句
public string QuerySql
{
get
{
int year = Year;
int month = Month;
int day = Day;
List<string> sqlList = new List<string>();
string subSql = string.Empty;
if(year != 0)
sqlList.Add(String.Format("Year(OrderDate) = {0}", year));
if(month != 0)
sqlList.Add(String.Format("Month(OrderDate) = {0}", month));
if (day != 0)
sqlList.Add(String.Format("Day(OrderDate) = {0}", day));
if (sqlList.Count > 0) // 如果选择了任意一个下拉框,那么就拼装Sql语句
{
string list = sqlList.ToArray();
&nb