【问题标题】:Load data in custom form by using Kentico 9使用 Kentico 9 以自定义形式加载数据
【发布时间】:2017-01-03 14:39:18
【问题描述】:

我在整个网络上进行了搜索,但找不到任何可以揭示正确方法的好主题。

我有一个使用 Kentico 9 CMS 开发的非常简单的网站。此网页仅包含两个子页面和一个用于在这些子页面之间导航的标题。

“主页”子页面包含一个自定义表单,该表单保持连接到一个 SQL 表,每次您按下提交时都会使用某些数据填充该表。

另一方面,另一个页面使用 自定义 Web 部件 显示存储的数据,该部件使用 BizFormItemProvider 连接到数据库,并且该对象用作在控件中绑定数据的层。

现在是我的重点。如果您看到,有一个“编辑”某个行的按钮,我的意图是重定向到“主页”(其中包含表单)并通过 QueryString 发送尝试编辑的行的 ID。

我无法理解如何使用 ID 重新填写表单的数据

也许是因为我以前从未使用过 CMS,所以我正在寻找纯 ASP.NET 之类的开发,但它可能不是正确的。

【问题讨论】:

    标签: kentico


    【解决方案1】:

    自定义

    鉴于您的解决方案使用 自定义表单 来输入数据,以及 自定义 Web 部件 来列出存储的数据,因此您需要使用自定义处理数据编辑的解决方案。

    在主页上的自定义 webpart 中,在加载事件中,您可以检索表单数据并在表单控件上设置值。

    protected void Page_Load(object sender, EventArgs e)
    {
        // Ensure that the form is not being posted back,
        // to prevent entered data from being overwritten
        if(!IsPostBack)
        {
            // Get the form item ID from the query string
            var personId = QueryHelper.GetInteger("personId", 0);
            if(personId > 0)
            {
                // Get the biz form item, and set form control values
                var bizFormItem = BizFormItemProvider.GetItem(personId, "customFormClassName");
                txtFirstName.Text = bizFormItem.GetStringValue("FirstName", string.Empty);
            }
        }
    }
    

    同样,当点击提交时,您可以使用新数据更新现有的表单项

    protected void btnSubmit_OnClick(object sender, EventArgs e)
    {
        // Get the form item ID from the query string
        var personId = QueryHelper.GetInteger("personId", 0);
        if(personId > 0)
        {
            // Retrieve the existing biz form item,
            // and update it from the form control values
            var bizFormItem = BizFormItemProvider.GetItem(personId, "customFormClassName");
            bizFormItem.SetValue("FirstName", txtFirstName.Text);
            bizFormItem.Update();
        }
        else
        {
            // Your code for inserting a new form item...
        }
    }
    

    肯蒂科方式

    您真的应该考虑使用 Kentico 表单引擎来完成这项任务。不要使用自定义表单来输入数据,而是使用内置的在线表单 webpart。

    The benefits are numerous,如:

    • 能够通过 CMS 设置表单布局,并使用替代布局
    • 向表单提交者发送自动确认电子邮件,以及向管理员发送通知电子邮件

    要完成您的任务,您可以customise the On-line form webpart 支持加载现有数据。 在bizform.ascx.cs 文件中,将代码添加到SetupControl 方法中:

    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Existing code...
        }
        else
        {
            // Existing code...
    
            // Get the form item ID from the query string
            var personId = QueryHelper.GetInteger("personId", 0);
            if(personId > 0)
            {
                // Get the biz form item, and set form control values
                var bizFormItem = BizFormItemProvider.GetItem(personId, "customFormClassName");
                if(bizFormItem != null)
                {
                    // Set the item ID
                    viewBiz.ItemID = bizFormItem.ItemID;
                }
            }
        }
    }
    

    一旦您设置了ItemID 属性,这将自动将表单切换到编辑 模式,而不是插入 模式。单击提交按钮将保存对现有表单项的更改。 您无需担心代码中的验证,插入数据仍然有效。

    【讨论】:

    • 你的反应太棒了!非常感谢。正如你所说,我必须深入挖掘“肯蒂科之路”。再次感谢...
    【解决方案2】:

    这是您使用 Kenticos 内置表单应用程序的联系表单,还是自定义表单?如果它是自定义表单,您可以使用包含 ID 的链接创建转换。如果是biz表单,仍然可以在Page Types中创建一个转换(新建一个页面类型,选择“页面类型只是没有自定义字段的容器”),然后写custom query获取biz表单数据,并使用转发器显示具有该转换的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 2018-03-01
      • 2015-04-10
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多