编程语言
首页 > 编程语言> > c# – 了解ObjectDataSource和选择参数

c# – 了解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 Parameter,所以它填充了正确的值

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

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

Could not find control ‘hfFieldType’ in ControlParameter ‘Type’.

我究竟做错了什么?

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

解决方法:

为了使这个工作,我添加创建两个方法(一个用于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>

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

标签:c,asp-net,gridview,objectdatasource
来源: https://codeday.me/bug/20190522/1151718.html