动态数据 - 如何在列表中显示主键字段

发布于 2024-08-07 00:48:31 字数 434 浏览 9 评论 0原文

我终于开始深入研究动态数据(早就应该了)。我注意到,当我查看网站时,主表列表上不会显示主键字段。

例如,我有一个包含状态代码的表,其中包含两个字段。

StatusCode int identity Primary Key
StatusCodeDescription varchar(25)

在我网站的 StatusCodes/List.aspx 页面上,仅显示 StatusCodeDescription。

我意识到我无法编辑主键,但我想显示它。动态数据可以实现这一点吗?

我自己可以想到一些解决方法,老实说,我可以自己进一步深入文档来找到它,但我希望有更多经验的人知道答案,并可以节省我一些寻找的时间。指向适当文档的链接甚至会更好。

I'm finally digging into Dynamic Data (long overdue). I notice that when I'm viewing the web site, on the main table lists, the primary key field is not displayed.

For example, I have a table that holds Status Codes, with two fields.

StatusCode int identity Primary Key
StatusCodeDescription varchar(25)

On my web site's StatusCodes/List.aspx page, only the StatusCodeDescription shows.

I realize that I can't edit the primary key, but I would like to show it. Is this possible with Dynamic Data?

I can think of some workarounds myself, and in all honesty, I could dig into the documentation further to find it myself, but I'm hoping that someone with more experience knows the answer and can save me some time looking. A link to the appropriate documentation would even be good.

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

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

发布评论

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

评论(3

谁的新欢旧爱 2024-08-14 00:48:31

经过一天的大部分时间研究后,我有另一个可能更“正确”的答案。据我所知,这是基于每个表的(这意味着您必须为每个表执行一次)

使用 ScaffoldColumn 属性:

我使用部分类扩展了为表 (StatusCodes) 自动生成的类,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

namespace ScholarshipAdmin
{
    [MetadataType(typeof(StatusCodesMetaData))]
    public partial class StatusCodes 
    {

    }

    public class StatusCodesMetaData
    {
        [ScaffoldColumn(true)] 
        public object StatusCode;
    }
}

After spending most of the day researching, I have another answer that is probably more "correct". From what I can see, this is on a per table basis (meaning you'd have to do this once per table)

Using the ScaffoldColumn attribute:

I extended the class that is automatically generated for the table (StatusCodes) using a partial class as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

namespace ScholarshipAdmin
{
    [MetadataType(typeof(StatusCodesMetaData))]
    public partial class StatusCodes 
    {

    }

    public class StatusCodesMetaData
    {
        [ScaffoldColumn(true)] 
        public object StatusCode;
    }
}
給妳壹絲溫柔 2024-08-14 00:48:31

我自己找到了一个可能的答案。我会发布此内容,以防将来其他人来寻找答案。请注意,只有当表都具有单列主键时,这才有效。

我还将保留这个问题,以防其他人有更好、更简单的答案,该答案适用于具有多个主键的表。我宁愿选择别人的答案,也不愿选择我自己的答案。

在 List.aspx 中,我将以下内容添加到 GridView1。

<asp:TemplateField>
   <HeaderTemplate>
      <%# table.PrimaryKeyColumns[0].Name %>
   </HeaderTemplate>
   <ItemTemplate>
      <%# DataBinder.Eval(Container.DataItem, table.PrimaryKeyColumns[0].Name) %>
   </ItemTemplate>
</asp:TemplateField>

I found one possible answer myself. I'll post this in case someone else comes looking for the answer in the future. Note that this will only work if the tables all have a one column primary key.

I'll also keep this question out there in case someone else has a better, simpler answer that will work with tables that have multiple primary keys. I'd rather select someone else's answer than my own.

In the List.aspx, I added the following to GridView1.

<asp:TemplateField>
   <HeaderTemplate>
      <%# table.PrimaryKeyColumns[0].Name %>
   </HeaderTemplate>
   <ItemTemplate>
      <%# DataBinder.Eval(Container.DataItem, table.PrimaryKeyColumns[0].Name) %>
   </ItemTemplate>
</asp:TemplateField>
尹雨沫 2024-08-14 00:48:31

以防万一其他人像我一样出现在这个 10 年前的线程中,我正在使用 Linq to SQL 和 DyanamicData,并且我能够通过在 O/R Designer 属性中将“自动生成的值”更改为 False 来解决此问题 到目前为止,

我看到的一个潜在陷阱是,当尝试插入一行时,仍然会提示用户输入该字段

Just in case anyone else ends up here in this 10 year old thread like me, I am using Linq to SQL with DyanamicData and I was able to solve this by changing "Auto Generated Value" to False in the O/R Designer properties for that column

One potential pitfall that I have seen so far is that when trying to insert a row, the user is still prompted for that field

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