使用ASP.NET动态数据创建网站时如何自动生成GUID? [英] How to generate GUIDs automatically while creating a site using ASP.NET Dynamic Data?

查看:159
本文介绍了使用ASP.NET动态数据创建网站时如何自动生成GUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server数据库,其中有很多表. 每个表都有一个主列,实际上是GUID列. 我创建了新的ASP.NET Dynamic数据站点来管理这些表.问题在于,没有生成GUID,这就是为什么该站点会中断所有表的原因.

I am having an SQL Server database, which is having a lots of table. Each of the table is having a primary column, which in fact is a GUID column. I have created new ASP.NET Dynamic data site to manage these tables. The problem is that, the GUIDs are not being generated and that's why the site breaks for all tables.

我尝试通过在SQL表中添加"newID()"来进行操作,但仍然失败. 我相信我需要以某种方式对代码进行更改.我相信这可能是许多使用ASP.Net Dynamic网站的开发人员所面临的问题.关于如何解决此问题的任何想法?

I tried by adding "newID()" in SQL table, but it still fails. I believe I need to somehow do changes in code. I believe this might be an issue faced by many developers working with ASP.Net Dynamic site.. Any idea of how to fix this?

推荐答案

我认为我作弊,但我刚刚创建了一个新的GUID字段模板. 要使用,请为您的课程创建扩展元数据.缩短的版本在这里显示.并将其与您的班级联系起来.

I think I cheated but I just created a new GUID Field template. To use create the extension metadata for your class. Shortened version shown here. And associate it with your class.

然后将GUID.ascx和GUID_Edit.ascx添加到您的项目.当您插入新记录时,将生成一个GUID并以只读形式显示在您的表单中.关键是使UIHint成为GUID类型.

Then add the GUID.ascx and GUID_Edit.ascx to your project. When you insert a new record a GUID will be generated and show up read only in your form. Key is making the UIHint to be a GUID type.

下面的代码...

--- EventCalendarMetadata.cs

--- EventCalendarMetadata.cs

namespace DFEventsWeb
{
    [MetadataType( typeof ( EventCalendar_Metadata ) )  ]
    public partial class EVENT_CALENDAR
    {
        [DisplayName("EVENTS")]
        private abstract class EventCalendar_Metadata
        {
            [Display(Name = "ID")]
            [UIHint("GUID")]
            public object EVENT_ID { get; set; }

.... other fields removed ...
        }
    }
}

-GUID.ascx

--- GUID.ascx

<%@ Control Language="C#" CodeBehind="GUID.ascx.cs" Inherits="DFEventsWeb.GUID" %>
<asp:Literal runat="server" ID="Literal1" Text="<%# FieldValueString %>" />

-GUID.ascx.cs 命名空间DFEventsWeb {

--- GUID.ascx.cs namespace DFEventsWeb {

    public partial class GUID : System.Web.DynamicData.FieldTemplateUserControl
    {
        private const int MAX_DISPLAYLENGTH_IN_LIST = 100;

        public override string FieldValueString
        {
            get
            {
                string value = base.FieldValueString;
                if (ContainerType == ContainerType.List)
                {
                    if (value != null && value.Length > MAX_DISPLAYLENGTH_IN_LIST)
                    {
                        value = value.Substring(0, MAX_DISPLAYLENGTH_IN_LIST - 3) + "...";
                    }
                }
                return value;
            }
        }

        public override Control DataControl
        {
            get
            {
                return Literal1;
            }
        }
    }
}

-GUID_Edit.ascx

--- GUID_Edit.ascx

<%@ Control Language="C#" CodeBehind="GUID_Edit.ascx.cs" Inherits="DFEventsWeb.GUID_EditField" %>
<asp:Literal ID="Literal1" runat="server" Text='<%# OVERRIDE_GUID %>'></asp:Literal>

-GUID_Edit.ascx.cs

--- GUID_Edit.ascx.cs

namespace DFEventsWeb
{
    public partial class GUID_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        const int MAX_TEXT_COLUMN_SIZE = 200; // jeh

        string m_GUIDString;
        public string OVERRIDE_GUID 
        {
            get { return m_GUIDString; }
            set { m_GUIDString = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            OVERRIDE_GUID = ( FieldValueEditString.Length <= 0 ? System.Guid.NewGuid().ToString() : FieldValueEditString );
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(Literal1.Text);
        }

        public override Control DataControl
        {
            get
            {
                return Literal1;
            }
        }
    }
}

这篇关于使用ASP.NET动态数据创建网站时如何自动生成GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆