自动为动态数据应用程序中的新记录设置属性值

发布于 2024-09-15 21:52:29 字数 204 浏览 2 评论 0原文

我有一个 SalesOrderItem 表,它有一个 UnitPrice 列,以防销售人员希望覆盖该订单项的产品单价。

在 ASP.NET 动态数据应用程序中,根据产品的标准 UnitPrice 在新 SalesOrderItem 上设置 UnitPrice 属性的最佳方法是什么?换句话说,当用户在销售订单的“商品”屏幕上单击“新建”时,“单价”字段应该已填写产品的正常单价。

I have a SalesOrderItem table, and it has a UnitPrice column in case the salesperson wishes to override the unit price of the Product for that order item.

In an ASP.NET Dynamic Data application, what would be the best way to set that UnitPrice property on a new SalesOrderItem based on the standard UnitPrice for the product? In other words, when the user clicks New on the 'Items' screen for a sales order, the UnitPrice field should already be filled in with the normal unit price for the product.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-09-22 21:52:29

如果 UnitPrice 默认值是常量,则只需设置 DefaultValueAttribute。由于默认值必须在运行时计算,因此我们必须采取不同的方法。

从问题中尚不清楚Product记录是如何定位的。假设最初呈现插入页面时它在服务器上可用,您可以创建一个UnitPrice 列的字段模板。为了举例,我们还假设 UnitPrice 的列类型为 decimal

找到字段模板文件夹(默认位于“~/DynamicData/FieldTemplates”)。使用 Decimal_Edit.ascx* 文件作为指导,创建一组新的字段模板文件,标记为“UnitPrice_Insert.ascx”、“UnitPrice_Insert.ascx.cs”和“UnitPrice_Insert.ascx.designer.cs”。使用代码隐藏文件计算并设置默认值:

protected void Page_Init(object sender, EventArgs e) {
    decimal defaultUnitPrice;
    // Calculate and set the defaultUnitPrice here.
    // "this.Page" might be useful for locating the Product record.
    FieldValue = defaultUnitPrice;
}

最后,注释 UnitPrice 列的元数据以使用刚刚创建的模板:

[UIHint("UnitPrice")]

现在“插入”屏幕将使用“UnitPrice_Insert.ascx”从而显示计算出的默认值,而“列表”、“详细信息”和“编辑”屏幕仍将使用“Decimal.ascx”和“Decimal_Edit.ascx”模板,它们显示实际的列值,而不是默认值。

If the UnitPrice default value was a constant, it would be as simple as setting the DefaultValueAttribute on the UnitPrice metadata column. Since the default value must be calculated at runtime, we have to take a different approach.

It is not clear from the question how the Product record is located. Assuming it is available on the server when the Insert page is initially rendered, you could create a field template for the UnitPrice column. For the sake of example, let's also assume that the column type of UnitPrice is decimal.

Locate the field templates folder (by default in "~/DynamicData/FieldTemplates"). Using the Decimal_Edit.ascx* files as a guide, create a new set of field template files labeled "UnitPrice_Insert.ascx", "UnitPrice_Insert.ascx.cs", and "UnitPrice_Insert.ascx.designer.cs". Use the code-behind file to calculate and set the default value:

protected void Page_Init(object sender, EventArgs e) {
    decimal defaultUnitPrice;
    // Calculate and set the defaultUnitPrice here.
    // "this.Page" might be useful for locating the Product record.
    FieldValue = defaultUnitPrice;
}

Finally, annotate the metadata for the UnitPrice column to use the template that was just created:

[UIHint("UnitPrice")]

Now the Insert screen will use "UnitPrice_Insert.ascx" and thereby show the calculated default value, while the List, Details, and Edit screens will still use the "Decimal.ascx" and "Decimal_Edit.ascx" templates, which show the actual column value, not the default value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文