Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Put all parameters in HiddenField's, one of the HDF must be a flag that is used to decide if the select method must return the query items. See this <a href="https://dl.dropbox.com/u/49510149/stackoverflow/dotnet/Q11874496WebApp.7z" rel="nofollow">answer</a>.</p> <p>Code sample:</p> <p>FilterGridView.aspx:</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FilterGridView.aspx.cs" Inherits="Q11876988WebApp.FilterGridView" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;%-- Fields for user inputs --%&gt; &amp;nbsp;Id: &lt;asp:TextBox ID="TxtId" runat="server"&gt;&lt;/asp:TextBox&gt; &amp;nbsp;Name: &lt;asp:TextBox ID="TxtName" runat="server"&gt;&lt;/asp:TextBox&gt; &amp;nbsp;Phone: &lt;asp:TextBox ID="TxtPhone" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;%-- Button to perform query --%&gt; &lt;asp:Button ID="BtnQuery" runat="server" OnClick="BtnQuery_Click" Text="Query" /&gt; &lt;%-- Hidden Fields used as parameters --%&gt; &lt;asp:HiddenField ID="HdfId" runat="server" /&gt; &lt;asp:HiddenField ID="HdfName" runat="server" /&gt; &lt;asp:HiddenField ID="HdfPhone" runat="server" /&gt; &lt;%--'false': Avoid query execution before 'BtnQuery' click.--%&gt; &lt;asp:HiddenField ID="HdfDoQuery" runat="server" Value="false" /&gt; &lt;%-- GridView --%&gt; &lt;asp:GridView ID="GrvMyData" runat="server" AutoGenerateColumns="False" DataSourceID="OdsMyData"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /&gt; &lt;asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /&gt; &lt;asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;%-- ObjectDataSource and parameters related to Hidden Fields --%&gt; &lt;asp:ObjectDataSource ID="OdsMyData" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="QueryMyDataPoco" TypeName="Q11876988WebApp.FilterGridViewODS"&gt; &lt;SelectParameters&gt; &lt;asp:ControlParameter ControlID="HdfId" ConvertEmptyStringToNull="False" Name="id" PropertyName="Value" Type="String" /&gt; &lt;asp:ControlParameter ControlID="HdfName" ConvertEmptyStringToNull="False" Name="name" PropertyName="Value" Type="String" /&gt; &lt;asp:ControlParameter ControlID="HdfPhone" ConvertEmptyStringToNull="False" Name="phone" PropertyName="Value" Type="String" /&gt; &lt;asp:ControlParameter ControlID="HdfDoQuery" ConvertEmptyStringToNull="False" Name="doQuery" PropertyName="Value" Type="Boolean" /&gt; &lt;/SelectParameters&gt; &lt;/asp:ObjectDataSource&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>FilterGridView.aspx.cs:</p> <pre><code>public partial class FilterGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BtnQuery_Click(object sender, EventArgs e) { //change this values causes the 'OdsMyData' 'DataBind'. this.HdfId.Value = this.TxtId.Text; this.HdfName.Value = this.TxtName.Text; this.HdfPhone.Value = this.TxtPhone.Text; this.HdfDoQuery.Value = true.ToString(); } } </code></pre> <p>FilterGridViewODS.cs:</p> <pre><code>/// &lt;summary&gt; /// Class for a ObjectDataSource /// &lt;/summary&gt; [DataObject] public class FilterGridViewODS { private static IList&lt;MyDataPoco&gt; MyDataList = new List&lt;MyDataPoco&gt;(); /// &lt;summary&gt; /// Static constructor. Creates a list of 50 items. /// &lt;/summary&gt; static FilterGridViewODS() { for (int i = 0; i &lt; 50; i++) { MyDataList.Add( new MyDataPoco() { Id = i.ToString(), Name = "Name " + i, Phone = i + "" + i + "" + i + "." + i + "" + i + "" + i + "" + i + "" }); } } /// &lt;summary&gt; /// if &lt;paramref name="doQuery"/&gt; is &lt;c&gt;true&lt;/c&gt;, performs a /// query over the data content, Otherwise return an empty list. /// &lt;/summary&gt; [DataObjectMethod(DataObjectMethodType.Select)] public IEnumerable&lt;MyDataPoco&gt; QueryMyDataPoco(String id, String name, String phone, bool doQuery) { if (doQuery) { IEnumerable&lt;MyDataPoco&gt; filteredEnum = from md in MyDataList where md.Id.Contains(id) &amp;&amp; md.Name.Contains(name) &amp;&amp; md.Phone.Contains(phone) select md; return filteredEnum; } else { //returning an empty list. return new List&lt;MyDataPoco&gt;(); } } } </code></pre> <p>MyDataPoco.cs:</p> <pre><code>public class MyDataPoco { public String Id { get; set; } public String Name { get; set; } public String Phone { get; set; } } </code></pre> <p>Complete source: <a href="https://dl.dropbox.com/u/49510149/stackoverflow/dotnet/Q11874496WebApp.7z" rel="nofollow">Q11874496WebApp.7z</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload