【问题标题】:How can i populate dropdown fom database in dnn如何从 dnn 中的数据库填充下拉列表
【发布时间】:2015-04-29 10:00:32
【问题描述】:

我想在 DNN 站点中添加新页面,该页面填充数据库中国家和州的两个下拉列表。

请建议我们如何做到这一点?

【问题讨论】:

    标签: dotnetnuke


    【解决方案1】:

    这里我将向您展示如何使用 DNN 模块创建动态页面。更多信息请参考this link。

    1. 首先在 Visual Studio 中打开您的 DNN 网站。您可以在根目录中看到 DesktopModules 文件夹,右键单击该目录并添加新目录,并将其命名为您的模块名称,例如“CountryState”。现在右键单击 CountryState 文件夹并添加->添加新项目->选择 Web User Control 并将其命名为您的模块名称,如“CountryState.ascx”,它会在 DesktopModules/CountryState 文件夹中显示 CountryState.ascx 和 CountryState.ascx.cs 文件。
    2. 下一步将继承类“System.Web.UI.UserControl”更新为“PortalModuleBase”。这是因为 PortalModuleBase 类定义了一个自定义基类,该基类由 Portal 内的所有桌面门户模块继承。 PortalModuleBase 类定义了门户框架用来正确显示门户模块的门户特定属性。
    3. 现在使用您的主机凭据登录您的网站。
    4. 在 DNN 面板中,转到 Host->Extensions。
    5. 点击扩展页面右上角的创建新模块按钮。
    6. 创建新模块按钮打开一个带有创建模块表单 DropDownList 的弹出窗口。从该下拉菜单中选择Control。
    7. 它将向您显示表单,从相应文件夹路径中选择您的 .ascx Web 用户控件,然后单击创建模块按钮。
    8. 最后在您的页面上拖放模块。
    9. 享受吧! ;)

    【讨论】:

      【解决方案2】:

      我有一篇博客文章,其中包含有关如何在 DNN 中完成此操作的代码示例。

      CASCADING COUNTRY / STATE DROPDOWNS: A STUDY IN DOTNETNUKE 7 WEBAPI SERVICES AND AJAX

      【讨论】:

        【解决方案3】:

        Keval 的解决方案对于一个快速而肮脏的解决方案来说是可以的,但对于一些更可持续的东西,我更喜欢将我的模块保留在他们自己的解决方案中,而不是 DNN。然后我使用project post build events 将所需的代码复制到 bin 和 desktop 模块文件夹。这使得源代码控制更容易,并更快地打开解决方案。

        不同文件扩展名的构建脚本

        xcopy "$(ProjectDir)*.ascx" C:\Webs\Properteez\DesktopModules\$(ProjectName)\*.ascx   /S /C /Y /D
        xcopy "$(ProjectDir)*.css" C:\Webs\Properteez\DesktopModules\$(ProjectName)\*.css   /S /C /Y /D
        xcopy "$(ProjectDir)*.txt" C:\Webs\Properteez\DesktopModules\$(ProjectName)\*.txt   /S /C /Y /D
        xcopy "$(ProjectDir)App_LocalResources\*.resx" C:\Webs\Properteez\DesktopModules\$(ProjectName)\App_LocalResources\*.resx   /S /C /Y /D
        xcopy "$(ProjectDir)Styles\*.css" C:\Webs\Properteez\DesktopModules\$(ProjectName)\Styles\*.css   /S /C /Y /D
        xcopy "$(ProjectDir)Scripts\*.js" C:\Webs\Properteez\DesktopModules\$(ProjectName)\Scripts\*.js   /S /C /Y /D
        xcopy "$(ProjectDir)Images\*.*" C:\Webs\Properteez\DesktopModules\$(ProjectName)\Images\*.*   /S /C /Y /D
        xcopy "$(TargetDir)$(TargetFileName)" C:\Webs\Properteez\bin   /C /Y /Q /D
        

        还有,

        从数据库中为 Country 和 State 填充两个下拉列表

        可以通过 PetaPoco 的“DNN 方式”与 DB 进行通信。

        模型示例(这需要直接映射到您的数据库表):

        #region Usings
        
        using System;
        using DotNetNuke.ComponentModel.DataAnnotations;
        using DotNetNuke.Common.Utilities;
        
        #endregion
        
        namespace Properteez.Data.Entities
        {
            [TableName("Sync_Property")]
            [PrimaryKey("PropertyId", "PropertyId")]
            public class Property
            {
                #region Properties
        
                public int PropertyId { get; set; }
                public string Name { get; set; }
                public string ShortDesc { get; set; }
                public int UserId { get; set; }
                public bool IsDeleted { get; set; }
        // You can use the [IgnoreColumn] attribute if a property does not map to a DB field
            }
        }
        

        存储库方法示例:

            public static Property Get(int id)
            {
                Property item;
                using (IDataContext ctx = DataContext.Instance())
                {
                    var rep = ctx.GetRepository<Property>();
                    item = rep.GetById(id);
                }
                return item;
            }
        
            public static List<Property> GetAllSinceLastSync(DateTime lastSyncDate)
            {
                List<Property> items;
                using (IDataContext ctx = DataContext.Instance())
                {
                    var rep = ctx.GetRepository<Property>();
                    items = rep.Get()
                        // Filter list to valid properteez that have been added or changed since last sync
                        .Where(p => (!p.IsDeleted && !p.IsSold) && p.LastModifiedOnDate >= lastSyncDate)
                        .ToList();
                }
                return items;
            }
        
            public static List<Property> GetAll()
            {
                List<Property> items;
                using (IDataContext ctx = DataContext.Instance())
                {
                    var rep = ctx.GetRepository<Property>();
                    items = rep.Get().ToList();
                }
                return items;
            }
        
            public static int Create(Property item)
            {
                item.CreatedOnDate = DateTime.Now;
                item.LastModifiedOnDate = DateTime.Now;
        
                using (IDataContext ctx = DataContext.Instance())
                {
                    var rep = ctx.GetRepository<Property>();
                    rep.Insert(item);
                }
                return item.PropertyId;
            }
        
            public static void Update(Property item)
            {
                item.LastModifiedOnDate = DateTime.Now;
        
                using (IDataContext ctx = DataContext.Instance())
                {
                    var rep = ctx.GetRepository<Property>();
                    rep.Update(item);
                }
            }
        
            public static void Delete(Property item)
            {
                using (IDataContext ctx = DataContext.Instance())
                {
                    var rep = ctx.GetRepository<Property>();
                    rep.Delete(item);
                }
            }
        

        调试

        您可以通过构建(将 .dll 复制到 DNN 实例的 bin 文件夹)来调试应用程序,进入调试>附加到进程并选择“W3wp”进程

        注意,您需要以管理员身份运行 Visual Studio。

        【讨论】:

          猜你喜欢
          • 2011-11-09
          • 2014-12-25
          • 1970-01-01
          • 1970-01-01
          • 2018-10-03
          • 2016-01-09
          相关资源
          最近更新 更多