ASP.NET 4.0 在嵌入式列表视图中使用 DynamicControl
我有一个嵌入式列表视图(在另一个列表视图中),并且两个列表视图都成功允许 CRUD 操作。
嵌入式列表视图的 asp 如下所示:
<asp:ObjectDataSource ID="objConcentrations" runat="server"
TypeName="PICUdrugs.BLL.infusionBL" DataObjectTypeName="PICUdrugs.DAL.infusionConcentration"
InsertMethod="insertConcentration" SelectMethod="getConcentrations"
deleteMethod="deleteConcentration" UpdateMethod="updateConcentration"
OldValuesParameterFormatString="orig{0}" ConflictDetection="CompareAllValues"
OnUpdated="objConcentrations_CRUD" OnInserted="objConcentrations_CRUD">
<SelectParameters>
<asp:Parameter Name="infDilutionID" Type="Int32"/>
</SelectParameters>
</asp:ObjectDataSource>
<asp:ListView ID="infusionConcListView" runat="server" DataSourceID="objConcentrations"
ItemPlaceholderID="itemPlaceHolder1" InsertItemPosition="LastItem" DataKeyNames="infusionConcentrationID"
OnDataBound="concentrationLV_allDataBound" EnableViewState="False">
外部列表视图的 onItemDataBound 事件设置嵌入式 objectDataSource 上的 select 参数的默认值,如下所示:
protected void infDilutnLV_itemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ObjectDataSource infConcentrations = e.Item.FindControl("objConcentrations") as ObjectDataSource;
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (infConcentrations != null)
{
infusionDilution infDil = (infusionDilution)dataItem.DataItem;
Parameter parameter = infConcentrations.SelectParameters[0];
parameter.DefaultValue = infDil.infusionDilutionID.ToString();
}
}
}
然后,objectDataSource 在数据访问层中使用实体框架。我已经为实体类设置了 dataAnnotations,如下所示:
namespace PICUdrugs.DAL
{
[MetadataType(typeof(infusionDilutionMetaData))]
public partial class infusionDilution
{
}
public class infusionDilutionMetaData
{
[Range(0, 3000, ErrorMessage = "volume must be between 0 and 3000 ml")]
public int? finalVol { get; set; }
}
[MetadataType(typeof(infusionConcentrationMetaData))]
public partial class infusionConcentration
{
}
public class infusionConcentrationMetaData
{
[Range(0.001, 1000, ErrorMessage = "concentration must be between 0.001 and 1000 units/ml")]
public double Concentration { get; set; }
}
}
虽然动态控件在外部(非嵌套)列表视图上完美工作,但当我将任何控件更改为内部(嵌套)列表视图上的动态控件时,我收到错误:
Could not determine a MetaTable. A MetaTable could not be determined for the data source 'objConcentrations' and one could not be inferred from the request URL. Make sure that the table is mapped to the dats source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute.
i恐怕我是一个业余程序员,我不确定是否可以在嵌套列表视图中使用动态字段。是否有任何解决方法,或者我的代码中是否存在错误?非常感谢您的帮助。
I have an embedded listview (within another listview), and both listviews are succesfully allowing CRUD operations.
the asp for the embedded listview looks like this:
<asp:ObjectDataSource ID="objConcentrations" runat="server"
TypeName="PICUdrugs.BLL.infusionBL" DataObjectTypeName="PICUdrugs.DAL.infusionConcentration"
InsertMethod="insertConcentration" SelectMethod="getConcentrations"
deleteMethod="deleteConcentration" UpdateMethod="updateConcentration"
OldValuesParameterFormatString="orig{0}" ConflictDetection="CompareAllValues"
OnUpdated="objConcentrations_CRUD" OnInserted="objConcentrations_CRUD">
<SelectParameters>
<asp:Parameter Name="infDilutionID" Type="Int32"/>
</SelectParameters>
</asp:ObjectDataSource>
<asp:ListView ID="infusionConcListView" runat="server" DataSourceID="objConcentrations"
ItemPlaceholderID="itemPlaceHolder1" InsertItemPosition="LastItem" DataKeyNames="infusionConcentrationID"
OnDataBound="concentrationLV_allDataBound" EnableViewState="False">
with the onItemDataBound event of the outer listview setting the default value for the select parameter on the embedded objectDataSource as so:
protected void infDilutnLV_itemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ObjectDataSource infConcentrations = e.Item.FindControl("objConcentrations") as ObjectDataSource;
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (infConcentrations != null)
{
infusionDilution infDil = (infusionDilution)dataItem.DataItem;
Parameter parameter = infConcentrations.SelectParameters[0];
parameter.DefaultValue = infDil.infusionDilutionID.ToString();
}
}
}
The objectDataSource then uses an entity framework in the data access layer. I have set up dataAnnotations for the entity classes as so:
namespace PICUdrugs.DAL
{
[MetadataType(typeof(infusionDilutionMetaData))]
public partial class infusionDilution
{
}
public class infusionDilutionMetaData
{
[Range(0, 3000, ErrorMessage = "volume must be between 0 and 3000 ml")]
public int? finalVol { get; set; }
}
[MetadataType(typeof(infusionConcentrationMetaData))]
public partial class infusionConcentration
{
}
public class infusionConcentrationMetaData
{
[Range(0.001, 1000, ErrorMessage = "concentration must be between 0.001 and 1000 units/ml")]
public double Concentration { get; set; }
}
}
While the dynamicControls are working perfectly on the outer (non-nested) listview, when I change any controls to dynamic controls on the inner (nested) listview, I get the error:
Could not determine a MetaTable. A MetaTable could not be determined for the data source 'objConcentrations' and one could not be inferred from the request URL. Make sure that the table is mapped to the dats source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute.
i am afraid I am very much an amateur programmer, and I am unsure if it is even possible to use dynamicFields in a nested listview. Are there any workarounds, or is there an error in my code I am missing? Thank you very much for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不敢相信我没有看到这个 - 如果我浪费了任何人的时间,我很抱歉。
当 Page_Init 设置外部 ListView 的 EnableDynamicData 时,我需要对嵌套 ListView 使用 onInit 处理程序:
I can't believe I didn't see this - sorry if I wasted anyone's time.
While the Page_Init set the EnableDynamicData of the outer ListView, I need to use an onInit handler for the nested ListView: