I'm trying to set the where clause on a LinqDataSource object bound to a GridView programmatically on a button click, but when the GridView rebinds data (for instance, when the user sorts) the Where clause resets back to the empty string. Is there a way to prevent this, or is there a better way to filter my results?
我试图在单击按钮时,以编程方式将LinqDataSource对象上的where子句绑定到GridView,但是当GridView重新绑定数据(例如,当用户排序时)时,where子句重新设置为空字符串。有没有办法避免这种情况,或者有没有更好的方法来过滤我的结果?
1 个解决方案
#1
3
Perhaps you just add a ViewState property into your page/user control and then retrieve it on all post back?
也许您只是将ViewState属性添加到页面/用户控件中,然后在所有的post上检索它?
public string MyLinqSourceWhere { get { return (string)this.ViewState["MyLinqSourceWhere"]; } set { this.ViewState["MyLinqSourceWhere"] = value; } } public void Page_Load(object sender, EventArgs e) { this.myLinqSource.Where = this.MyLinqSourceWhere; } public void Button1_Click(object sender, EventArgs e) { this.MyLinqSourceWhere = " .... "; this.myLinqSource.Where = this.MyLinqSourceWhere; }
If that doesn't work, then perhaps bind on the LinqDataSource.Selecting event the fetch property from the viewstate to your where clause?? It all depends
如果这不起作用,那么可以绑定LinqDataSource。选择事件从viewstate获取属性到where子句?要看情况而定
#1
3
Perhaps you just add a ViewState property into your page/user control and then retrieve it on all post back?
也许您只是将ViewState属性添加到页面/用户控件中,然后在所有的post上检索它?
public string MyLinqSourceWhere { get { return (string)this.ViewState["MyLinqSourceWhere"]; } set { this.ViewState["MyLinqSourceWhere"] = value; } } public void Page_Load(object sender, EventArgs e) { this.myLinqSource.Where = this.MyLinqSourceWhere; } public void Button1_Click(object sender, EventArgs e) { this.MyLinqSourceWhere = " .... "; this.myLinqSource.Where = this.MyLinqSourceWhere; }
If that doesn't work, then perhaps bind on the LinqDataSource.Selecting event the fetch property from the viewstate to your where clause?? It all depends
如果这不起作用,那么可以绑定LinqDataSource。选择事件从viewstate获取属性到where子句?要看情况而定