简体   繁体   English

为营销人员选择Web表单中的日期输入字段

[英]Selecting a date input field in Web form for marketers

I am using Sitecore WFFM and the date field comes with a drop down list of dd / mm /YYYY with a starting date that I define. 我正在使用Sitecore WFFM ,日期字段附带一个dd / mm /YYYY的下拉列表,其中包含我定义的开始日期。 If I want to use this for DOB field I would like this drop down list to have a null value instead of 1 Jan 2000 so the user is forced to select. 如果我想将此用于DOB字段,我希望此下拉列表具有null值而不是1 Jan 2000因此用户被迫选择。 How can I enforce a "select" value instead of the default date defined in form designer for that field? 如何强制执行“选择”值而不是该字段的表单设计器中定义的默认日期?

One way is to create a custom WFFM field by subclassing Sitecore.Form.Web.UI.Controls.DateSelector . 一种方法是通过继承Sitecore.Form.Web.UI.Controls.DateSelector来创建自定义WFFM字段。

In the OnInit method, insert the empty "select" items at the start of each dropdownlist: OnInit方法中,在每个下拉列表的开头插入空的“select”项:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    month.Items.Insert(0, new ListItem("", ""));
    day.Items.Insert(0, new ListItem("", ""));
    year.Items.Insert(0, new ListItem("", ""));

    SelectedDate = String.Empty;
    month.SelectedValue = String.Empty;
    day.SelectedValue = String.Empty;
    year.SelectedValue = String.Empty;
}

In order to ensure proper validation, you will also need to override the Result property as well as set the ValidationProperty attribute on the class itself. 为了确保正确验证,您还需要覆盖Result属性以及在类本身上设置ValidationProperty属性。

[ValidationProperty("Value")]
public class CustomDate : DateSelector 
{
    protected override void OnInit(EventArgs e) { ... } // See above

    /// <summary>
    /// Gets the value of the date field
    /// </summary>
    public new string Value
    {
        get
        {
            if (month.SelectedIndex > 0 && day.SelectedIndex > 0 && year.SelectedIndex > 0)
                return DateUtil.ToIsoDate(new DateTime(DateUtil.IsoDateToDateTime(StartDate).Year + year.SelectedIndex, month.SelectedIndex + 1, day.SelectedIndex + 1).Date);

            return String.Empty;
        }
    }

    /// <summary>
    /// Retuns the new result
    /// </summary>
    public override ControlResult Result
    {
        get
        {
            return new ControlResult(base.ControlName, Value, null);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 营销人员的sitecore Web表单-发送邮件操作 - sitecore web form for marketers - Send Mail action 以编程方式为营销人员保存 Sitecore Web 表单 - Saving Sitecore Web form for marketers programmatically 在市场营销人员的Web表单中预填充表单字段 - Pre-populating Form Fields in Web Forms for Marketers 从datTimePicker选择日期 - Selecting a date form the datTimePicker 根据“营销人员Web表单”中的下拉列表选择将电子邮件路由到特定电子邮件地址 - Route an email to specific email address depending on dropdown selection in Web Form for Marketers 提交带日期字段的表单时出错 - Error submitting form with date field asp.net web 表单中的“__VIEWSTATE_KEY”隐藏输入字段是什么,它的用途是什么 - What is "__VIEWSTATE_KEY" hidden input field in a asp.net web form and what is it used for Web 表单上的消息和输入框 - Message and Input Boxes on a Web Form 选择整个实体时,实体框架会返回陈旧数据,而仅选择一个字段时,实体框架将返回最新数据 - Entity Framework returns stale data when selecting the whole entity but up to date when selecting just a field 将HTML 5 DatePicker与Date输入字段一起使用 - Using the HTML 5 DatePicker with the Date input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM