了解ObjectDataSource和选择参数

时间:2009-08-06 10:23:38

标签: c# asp.net gridview objectdatasource

我在GridView控件中有一个TemplateField,如:

<asp:TemplateField ItemStyle-Width="150px">
   <ItemTemplate>
      <asp:DropDownList ID="ddlFields" runat="server" DataSourceID="odsOperator" DataTextField="Text" DataValueField="Value" />
      <asp:HiddenField ID="hfFieldType" runat="server" Value='<%# Eval("FieldType")%>' />
   </ItemTemplate>
</asp:TemplateField>

我有一个要从ObjectDataSource填充的下拉列表,但是对于每一行,我想传递一个Select参数,以便填充正确的值

<asp:ObjectDataSource ID="odsOperator" runat="server" TypeName="OperatorFieldsDAO"
   SelectMethod="FindByType">      
   <SelectParameters>
      <asp:ControlParameter ControlID="hfFieldType" Type="String" Name="Type" PropertyName="Value" />
   </SelectParameters>
</asp:ObjectDataSource>

我的OperatorFieldsDAO类是:

public class OperatorFieldsDAO
{
    private List<OperatorField> OperatorFields
    {
        get
        {
            List<OperatorField> operatorFields = HttpContext.Current.Session["OperatorFields"] as List<OperatorField>;
            if (operatorFields == null)
            {
                operatorFields = new List<OperatorField>();
                operatorFields.Add(new OperatorField("string", "contains", "C"));
                operatorFields.Add(new OperatorField("string", "begins with", "BW"));
                operatorFields.Add(new OperatorField("string", "is equal to", "E"));
                operatorFields.Add(new OperatorField("string", "is not equal to", "NE"));
                operatorFields.Add(new OperatorField("string", "is less than", "L"));
                operatorFields.Add(new OperatorField("string", "is greater than", "G"));
                operatorFields.Add(new OperatorField("string", "is less than or equal to", "LE"));
                operatorFields.Add(new OperatorField("string", "is greater than or equal to", "GE"));
                operatorFields.Add(new OperatorField("string", "is from", "F"));
                operatorFields.Add(new OperatorField("string", "is between", "B"));
                operatorFields.Add(new OperatorField("string", "is nothing", "N"));
                operatorFields.Add(new OperatorField("string", "is something", "S"));

                operatorFields.Add(new OperatorField("number", "is the same as", "S"));
                operatorFields.Add(new OperatorField("number", "is not the same as", "S"));
                operatorFields.Add(new OperatorField("number", "is one of", "S"));
                operatorFields.Add(new OperatorField("number", "is not one of", "S"));
                operatorFields.Add(new OperatorField("number", "is nothing", "N"));
                operatorFields.Add(new OperatorField("number", "is something", "S"));
            }
            return operatorFields;
        }
    }
    public OperatorFieldsDAO() { }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<OperatorField> FindAll()
    {
        return this.OperatorFields;
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<OperatorField> FindByType(String type)
    {    
        List<OperatorField> r = new List<OperatorField>();

        foreach (OperatorField f in this.OperatorFields)
            if (f.Type == type)
                r.Add(f);

        return r;
    }
}

所有这些都告诉你我收到了错误:

  

无法在ControlParameter'Type'中找到控件'hfFieldType'。

我做错了什么?

我是否需要使用 OnRowDataBound 方法以编程方式传递所选参数?

3 个答案:

答案 0 :(得分:4)

为了实现这个目的,我添加了创建两个方法(一个用于GridView,另一个用于ObjectDataSource),以及将ControlParameter中的Select Parameter更改为普通参数。

这个想法是每次创建它时设置参数......

protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList d = (DropDownList)e.Row.FindControl("ddlFields");
        string type = ((HiddenField)e.Row.FindControl("hfFieldType")).Value;

        _type = type;
        d.DataBind();
    }
}
protected void odsOperator_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["Type"] = _type;
}

private string _type = "";

并且ObjectDataSource将是

<asp:ObjectDataSource ID="odsOperator" runat="server" TypeName="OperatorFieldsDAO"
   SelectMethod="FindByType" onselecting="odsOperator_Selecting">
   <SelectParameters>
      <asp:Parameter Type="String" Name="Type" />
   </SelectParameters>
</asp:ObjectDataSource>

我希望它可以帮助任何人...

答案 1 :(得分:1)

因为你有:

<asp:HiddenField ID="hfFieldType" runat="server" .../>

在视图的TemplateField范围内,页面上可能没有,一个或多个该字段的实例。因此hfFieldType不是唯一ID,实际上实际ID将在运行时确定。

因此,您的控制参数无法找到它,因为它在页面某处名为hfFieldType的控件的属性中查找其值。

我在几年内没有使用过ObjectDataSource,但我怀疑你可能想要加入Selecting事件。

答案 2 :(得分:0)

ObjectDataSource无法找到用于输入的控件,除非它在标记中靠近。数据源必须位于包含用于输入的控件的标记中。 这似乎是一个范围问题。