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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Perhaps you just add a ViewState property into your page/user control and then retrieve it on all post back?
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