以下是使用了WEB的数据源控件:
在aspx页面是这样的:
一般情况下,.nettiers是生成这样的代码:(请注意是"CustomParameter")
< Parameters >
< data:CustomParameter Name = " WhereClause " .. />
</ Parameters >
</ data:EmployeesDataSource >
然而要实现后台修改数据源的"whereClause",则要把<data:CustomParameter Value="Country='USA'"...> 改为
<data:SqlParameter DefaultValue="Country='USA'"...> , 在这种情况下Value不可用,只会有DefaultValue,但DefaultValue没有选出相应记录的功能.
< DeepLoadProperties Method = " IncludeChildren " Recursive = " False " >
< Types >
< data:EmployeesProperty Name = " Employees " />
<%--< data:EmployeesProperty Name = " OrdersCollection " />--%>
<%--< data:EmployeesProperty Name = " EmployeesCollection " />--%>
<%--< data:EmployeesProperty Name = " EmployeeTerritoriesCollection " />--%>
<%--< data:EmployeesProperty Name = " TerritoryIdTerritoriesCollection_From_EmployeeTerritories " />--%>
</ Types >
</ DeepLoadProperties >
< Parameters >
< data:SqlParameter Name = " WhereClause " ConvertEmptyStringToNull = " false " UseParameterizedFilters = " false " />
< data:SqlParameter Name = " OrderByClause " ConvertEmptyStringToNull = " false " />
< asp:ControlParameter Name = " PageIndex " ControlID = " GridView1 " PropertyName = " PageIndex " Type = " Int32 " ></ asp:ControlParameter >
< asp:ControlParameter Name = " PageSize " ControlID = " GridView1 " PropertyName = " PageSize " Type = " Int32 " ></ asp:ControlParameter >
< data:SqlParameter Name = " RecordCount " Type = " Int32 " />
</ Parameters >
</ data:EmployeesDataSource >
后台代码:
parameter.Column = EmployeesColumn.HireDate;
parameter.BuilderExpression = StringBuilderExpression.AppendEquals;
parameter.DefaultValue = this .TextBox1.Text;
(EmployeesDataSource.Parameters[ 0 ] as SqlParameter).Filters.Add(parameter);
以下是通过时间范围修改whereclause:
{
EmployeesExpressionBuilder parameter = new EmployeesExpressionBuilder();
parameter.Column = EmployeesColumn.Country;
parameter.BuilderExpression = StringBuilderExpression.AppendEquals;
parameter.DefaultValue = " USA " ;
(EmployeesDataSource.Parameters[ 0 ] as SqlParameter).Filters.Add(parameter);
}
protected void Button1_Click( object sender, EventArgs e)
{
EmployeesExpressionBuilder parameter1 = new EmployeesExpressionBuilder();
parameter1.Column = EmployeesColumn.HireDate;
parameter1.BuilderExpression = StringBuilderExpression.AppendGreaterThanOrEqual;
parameter1.DefaultValue = this .TextBox1.Text;
EmployeesExpressionBuilder parameter2 = new EmployeesExpressionBuilder();
parameter2.Column = EmployeesColumn.HireDate;
parameter2.BuilderExpression = StringBuilderExpression.AppendLessThanOrEqual;
parameter2.DefaultValue = this .TextBox2.Text;
(EmployeesDataSource.Parameters[ 0 ] as SqlParameter).Filters.Add(parameter1);
(EmployeesDataSource.Parameters[ 0 ] as SqlParameter).Filters.Add(parameter2);
// ************************************************************************************************************
// 查询方法五:
// SqlFilterBuilder<EmployeesColumn> sqlfilter = new SqlFilterBuilder<EmployeesColumn>();
// sqlfilter.AppendRange(EmployeesColumn.HireDate, this.TextBox1.Text, this.TextBox2.Text);
// int cnt = 0;
// TList<Employees> List = DataRepository.EmployeesProvider.GetPaged(sqlfilter.ToString(), null, 0, 20, out cnt);
// this.GridView1.DataSource = List;
// this.GridView1.DataBind();
// “GridView1”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。
}
}
又或者直接在ASPX页面是修改WhereClause:
<data:EmployeesDataSource id="EmployeesDataSource" runat="server" EnableDeepLoad="True" EnableSorting="True" EnablePaging="True" SelectMethod="GetPaged">
<DeepLoadProperties Method="IncludeChildren" Recursive="False">
<Types>
<data:EmployeesProperty Name="Employees" />
<%--<data:EmployeesProperty Name="OrdersCollection" />--%>
<%--<data:EmployeesProperty Name="EmployeesCollection" />--%>
<%--<data:EmployeesProperty Name="EmployeeTerritoriesCollection" />--%>
<%--<data:EmployeesProperty Name="TerritoryIdTerritoriesCollection_From_EmployeeTerritories" />--%>
< pes>
</DeepLoadProperties>
<Parameters>
<data:CustomParameter Name="WhereClause" Value="Country='USA'" ConvertEmptyStringToNull="false" />
<data:CustomParameter Name="OrderByClause" Value="EmployeeId DESC" ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="PageIndex" ControlID="GridView1" PropertyName="PageIndex" Type="Int32" ></asp:ControlParameter>
<asp:ControlParameter Name="PageSize" ControlID="GridView1" PropertyName="PageSize" Type="Int32" ></asp:ControlParameter>
<data:CustomParameter Name="RecordCount" Value="0" Type="Int32" />
</Parameters>
</data:EmployeesDataSource>
以上问题总结如下:
1.<data:CustomParameter...> 与
<data:SqlParameter...> SqlParameter,CustomParameter主要区别是?
2.用了<data:SqlParameter...> 就不能用 Value="Country='USA'",那么我要页面加载的时候是不是要在 Page_Load事件中写同类似的事件?
答:可以在Page_Load事件中写入
parameter.Column = EmployeesColumn.Country;
parameter.BuilderExpression = StringBuilderExpression.AppendEquals;
parameter.DefaultValue = " USA " ;
(EmployeesDataSource.Parameters[ 0 ] as SqlParameter).Filters.Add(parameter);
3.(EmployeesDataSource.Parameters[0] as SqlParameter).Filters.Add(parameter);这句它就会自动地绑定.
4.记得加上UseParameterizedFilters="false" !!!!
5.http://www.cnblogs.com/iCaca/archive/2007/07/10/812632.html 前台可参考这里,还要感谢icaca的帮忙!