Reputation: 59
I have a question for you all, how can I make an object data source load data in to the gridview only when somebody clicks a link button? The main thing I am going to pass some parameters for the select method in the object data source at run time based on the search criteria of the user. Can anyone suggest a solution for this?
Upvotes: 1
Views: 3427
Reputation: 1192
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 answer.
Code sample:
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
--%>
Id:
<asp:TextBox ID="TxtId" runat="server"></asp:TextBox>
Name:
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
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; }
}
Complete source: Q11874496WebApp.7z
Upvotes: 0
Reputation: 541
I'm assuming you've already specified which source for each parameter value in the ObjectDataSource control.
What you need to do is on the search button's click event specify the data source and explicitly bind the grid as follows:
gridView1.DataSource = objectDataSource1;
gridView1.DataBind();
Upvotes: 0