1

我有一个问题要问大家,如何让对象数据源仅在有人单击链接按钮时才将数据加载到网格视图中?主要是我将根据用户的搜索条件在运行时为对象数据源中的 select 方法传递一些参数。任何人都可以为此提出解决方案吗?

4

3 回答 3

1

设置DataSource和调用DataBind()

于 2012-08-09T04:55:31.803 回答
0

将所有参数放在HiddenField 中,其中一个HDF 必须是一个标志,用于决定select 方法是否必须返回查询项。看到这个答案

代码示例:

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

于 2012-08-09T05:13:12.823 回答
0

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

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

gridView1.DataSource = objectDataSource1;
gridView1.DataBind();
于 2012-08-09T06:04:25.533 回答