了解 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 方法以编程方式传递所选参数?
I have in a GridView control a TemplateField like:
<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
<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:
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'.
What am I doing wrong?
Do I need to programatically pass that selected parameter using the OnRowDataBound method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使其正常工作,我添加了创建两种方法(一种用于 GridView,另一种用于 ObjectDataSource),并将选择参数从 ControlParameter 更改为普通参数。
这个想法是在每次创建行时设置参数...
并且 ObjectDataSource 将是
我希望它可以帮助任何人...
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.
The idea is to set the Parameter every time the row it's created...
and the ObjectDataSource will be
I hope it helps anyone ...
因为您有:
在您视图的
TemplateField
内,页面上可能没有、一个或多个该字段的实例。 因此,hfFieldType
不会是唯一的 ID,实际上实际的 ID 将在运行时确定。因此,您的控件参数无法找到它,因为它正在页面上某处名为
hfFieldType
的控件的属性中查找其值。我已经有几年没有使用 ObjectDataSource 了,但我怀疑您可能想加入 选择 事件。
Since you have:
Within a
TemplateField
of your view, there may be none, one or many instances of that field on the page. ThereforehfFieldType
would not be a unique ID, in fact the actual ID will be determined at runtime.Thus your control parameter cannot find it since it is looking for its value in a property of a control called
hfFieldType
somewhere on the page.I haven't worked with ObjectDataSource in a few years, but I suspect you might want to hook into the Selecting event.
ObjectDataSource 无法找到用于输入的控件,除非该控件位于标记中附近。 数据源必须位于包含用于输入的控件的标记内。
这似乎是一个范围界定问题。
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.