C# 了解 ObjectDataSource 和 Select 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1238083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:03:11  来源:igfitidea点击:

Understanding ObjectDataSource and Select Parameters

c#asp.netgridviewobjectdatasource

提问by balexandre

I have in a GridView control a TemplateField like:

我在 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>

I have a dropdown that I want to populate from a ObjectDataSource, but for each line I want to pass a Select Parameter so it populates with the right values

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

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


my OperatorFieldsDAO class is:

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

all of this to tell you that I get an error:

所有这些都是为了告诉你我收到了一个错误:

Could not find control 'hfFieldType' in ControlParameter 'Type'.

在 ControlParameter 'Type' 中找不到控件 'hfFieldType'。

What am I doing wrong?

我究竟做错了什么?

Do I need to programatically pass that selected parameter using the OnRowDataBoundmethod?

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

采纳答案by balexandre

to get this working I add to create two methods (one for the GridView and other for the ObjectDataSource), as well change the Select Parameter from a ControlParameter to a normal Parameter.

为了让它工作,我添加了两个方法(一个用于 GridView,另一个用于 ObjectDataSource),并将 Select Parameter 从 ControlParameter 更改为普通参数。

The idea is to set the Parameter every time the row it's created...

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

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 = "";

and the ObjectDataSource will be

并且 ObjectDataSource 将是

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

I hope it helps anyone ...

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

回答by Winston Smith

Since you have:

因为你有:

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

Within a TemplateFieldof your view, there may be none, one or many instances of that field on the page. Therefore hfFieldTypewould not be a unique ID, in fact the actual ID will be determined at runtime.

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

Thus your control parameter cannot find it since it is looking for its value in a property of a control called hfFieldTypesomewhere on the page.

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

I haven't worked with ObjectDataSource in a few years, but I suspect you might want to hook into the Selectingevent.

我已经有几年没有使用过 ObjectDataSource,但我怀疑您可能想要挂钩到Selecting事件。

回答by Ken

ObjectDataSource cannot find the control being used for input unless it's close by in the markup. The datasource has to be INSIDE the tag that contains the control used for input. It seems to be a scoping problem.

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