使用 Kentico 9 以自定义形式加载数据

Load data in custom form by using Kentico 9

我搜索了整个网络,但找不到任何好的主题来揭示正确的方法。

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

"Home" 子页面包含一个 自定义表单 ,它与 SQL table 保持连接,每次您按下提交时都会填充某些数据。

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

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

我无法理解您如何 re-fill 使用 ID 的表单及其数据。

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

这是您使用 Kenticos 内置表单申请的联系表单,还是自定义表单?如果它是自定义表单,您可以使用包含 ID 的 link 创建一个转换。如果是biz form,还是可以在Page Types中创建一个transformation(新建一个page type和select"The page type is only a container without custom fields"),然后写一个custom query得到biz form数据,并使用中继器显示具有该转换的数据。

自定义

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

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

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 方式

您真的应该考虑使用 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;
            }
        }
    }
}

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