gpt4 book ai didi

c# - 了解 ObjectDataSource 和选择参数

转载 作者:太空狗 更新时间:2023-10-29 23:19:30 26 4
gpt4 key购买 nike

我在 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 填充的下拉列表,但对于每一行,我都想传递一个选择参数,以便它填充正确的值

<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 更改为普通参数。

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

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# - 了解 ObjectDataSource 和选择参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com