自定义
鉴于您的解决方案使用 自定义表单 来输入数据,以及 自定义 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 属性,这将自动将表单切换到编辑 模式,而不是插入 模式。单击提交按钮将保存对现有表单项的更改。
您无需担心代码中的验证,插入数据仍然有效。