Gridview data loading using object data source control

I have a question for all of you, how can I make data source data source data included in gridview only when someone clicks the link button? The main thing is that I'm going to pass some parameters for the select method in the object's data source at run time based on user search criteria. Can anyone suggest a solution for this?

+1
source share
3 answers
+1
source

Put all the parameters in a HiddenField, one of the HDFs must be the flag that is used to decide whether the select method should return query elements. See the answer .

Code example:

FilterGridView.aspx:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FilterGridView.aspx.cs" Inherits="Q11876988WebApp.FilterGridView" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- Fields for user inputs --%> &nbsp;Id: <asp:TextBox ID="TxtId" runat="server"></asp:TextBox> &nbsp;Name: <asp:TextBox ID="TxtName" runat="server"></asp:TextBox> &nbsp;Phone: <asp:TextBox ID="TxtPhone" runat="server"></asp:TextBox> <%-- Button to perform query --%> <asp:Button ID="BtnQuery" runat="server" OnClick="BtnQuery_Click" Text="Query" /> <%-- Hidden Fields used as parameters --%> <asp:HiddenField ID="HdfId" runat="server" /> <asp:HiddenField ID="HdfName" runat="server" /> <asp:HiddenField ID="HdfPhone" runat="server" /> <%--'false': Avoid query execution before 'BtnQuery' click.--%> <asp:HiddenField ID="HdfDoQuery" runat="server" Value="false" /> <%-- GridView --%> <asp:GridView ID="GrvMyData" runat="server" AutoGenerateColumns="False" DataSourceID="OdsMyData"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" /> </Columns> </asp:GridView> <%-- ObjectDataSource and parameters related to Hidden Fields --%> <asp:ObjectDataSource ID="OdsMyData" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="QueryMyDataPoco" TypeName="Q11876988WebApp.FilterGridViewODS"> <SelectParameters> <asp:ControlParameter ControlID="HdfId" ConvertEmptyStringToNull="False" Name="id" PropertyName="Value" Type="String" /> <asp:ControlParameter ControlID="HdfName" ConvertEmptyStringToNull="False" Name="name" PropertyName="Value" Type="String" /> <asp:ControlParameter ControlID="HdfPhone" ConvertEmptyStringToNull="False" Name="phone" PropertyName="Value" Type="String" /> <asp:ControlParameter ControlID="HdfDoQuery" ConvertEmptyStringToNull="False" Name="doQuery" PropertyName="Value" Type="Boolean" /> </SelectParameters> </asp:ObjectDataSource> </div> </form> </body> </html> 

FilterGridView.aspx.cs:

 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(); } } 

FilterGridViewODS.cs:

 /// <summary> /// Class for a ObjectDataSource /// </summary> [DataObject] public class FilterGridViewODS { private static IList<MyDataPoco> MyDataList = new List<MyDataPoco>(); /// <summary> /// Static constructor. Creates a list of 50 items. /// </summary> static FilterGridViewODS() { for (int i = 0; i < 50; i++) { MyDataList.Add( new MyDataPoco() { Id = i.ToString(), Name = "Name " + i, Phone = i + "" + i + "" + i + "." + i + "" + i + "" + i + "" + i + "" }); } } /// <summary> /// if <paramref name="doQuery"/> is <c>true</c>, performs a /// query over the data content, Otherwise return an empty list. /// </summary> [DataObjectMethod(DataObjectMethodType.Select)] public IEnumerable<MyDataPoco> QueryMyDataPoco(String id, String name, String phone, bool doQuery) { if (doQuery) { IEnumerable<MyDataPoco> filteredEnum = from md in MyDataList where md.Id.Contains(id) && md.Name.Contains(name) && md.Phone.Contains(phone) select md; return filteredEnum; } else { //returning an empty list. return new List<MyDataPoco>(); } } } 

MyDataPoco.cs:

 public class MyDataPoco { public String Id { get; set; } public String Name { get; set; } public String Phone { get; set; } } 

Full source: Q11874496WebApp.7z

0
source

I assume that you have already indicated which source for each parameter value in the ObjectDataSource control.

What you need to do is to indicate the click event of the search button, specify the data source and explicitly bind the grid as follows:

 gridView1.DataSource = objectDataSource1; gridView1.DataBind(); 
0
source

All Articles