gridview使用对象数据源控件加载数据

时间:2012-08-09 04:51:02

标签: c#

我有一个问题,如果有人点击链接按钮,我怎么能让对象数据源将数据加载到gridview?我将在运行时根据用户的搜索条件在对象数据源中传递select方法的一些参数。有人可以为此提出解决方案吗?

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

将所有参数放在HiddenField中,其中一个HDF必须是一个标志,用于决定select方法是否必须返回查询项。请参阅此answer

代码示例:

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

完整来源:Q11874496WebApp.7z

答案 2 :(得分:0)

我假设您已经为ObjectDataSource控件中的每个参数值指定了哪个源。

您需要做的是在搜索按钮的click事件中指定数据源并显式绑定网格,如下所示:

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