locked
Problems setting default value and UIHint RRS feed

  • Question

  • User-1776315162 posted

    I have problem about setting defaultvalue for date.

    I use the following code to set the defaultvalue of date:

    [DataType(DataType.Date)]

    [DefaultValue(DateTime.Today)]

    public DateTime Campaign_Date_St { get; set; }

     However, I get this error:

    An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type 

    How should I change it so that the defaultvalue is always the day when the user inserts the data?

    Also, I find the code here:
        [Range(0, 1000)]
        [UIHint("IntegerSlider")]
        [DefaultValue(345)]
        public int Number { get; set; }

        [DataType(DataType.Date)]
        [UIHint("DateAjaxCalendar")]
        [Required]
        public DateTime Date { get; set; }

    http://blogs.msdn.com/davidebb/archive/2008/06/18/using-asp-net-dynamic-data-with-objectdatasource.aspx

    I wanna ask how I should implement the IntegerSlider.ascx and DateAjaxCalendar.

    Monday, July 14, 2008 5:42 AM

Answers

  • User-330204900 posted

    For a DefaultValue that is dynamic I would use a custom FieldTemplate.

    For a start I would make a copy of the relavent FieldTemplate and customize it to set the default value for instance in the DateTime FieldTemplate (or any other) I would add: 

    protected override void OnDataBinding(EventArgs e)
    {
    	if (Mode == System.Web.UI.WebControls.DataBoundControlMode.Insert)
    	{
    		// you will need to apply and Data format or DataType when setting the value
    		TextBox1.Text = DateTime.Now.ToString();
    	}
    	base.OnDataBinding(e);
    }

    If you want you could check to see if the DefaultValue attribute has been set before you set the value.

    Hope this helps [:D]

    P.S. I wrote an article in my blog a while back on default value (before DynamicData Futures) this my give you some ideas, and you may fine it useful DynamicData Default Values in ForegnKey_Edit & other FieldTemplates

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 14, 2008 9:49 AM
  • User-330204900 posted

    I suspect you problem is in the field templates markup have a look you probably find that the values of the textbox is set there also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2011 10:30 AM

All replies

  • User-428635645 posted

    Hi,

     

    Yes - unfortunately due to the nature of Attributes you cant put anything dynamic in their values as they need to be constant for compile time.

     

    What i would suggest is that you put in a string such as "[today]" as the default value, and then create a custom field template that in code looks for that value, and replaces it with DateTime.Today in the OnDataBinding event.

     

    Dan

    Monday, July 14, 2008 7:31 AM
  • User-330204900 posted

    For a DefaultValue that is dynamic I would use a custom FieldTemplate.

    For a start I would make a copy of the relavent FieldTemplate and customize it to set the default value for instance in the DateTime FieldTemplate (or any other) I would add: 

    protected override void OnDataBinding(EventArgs e)
    {
    	if (Mode == System.Web.UI.WebControls.DataBoundControlMode.Insert)
    	{
    		// you will need to apply and Data format or DataType when setting the value
    		TextBox1.Text = DateTime.Now.ToString();
    	}
    	base.OnDataBinding(e);
    }

    If you want you could check to see if the DefaultValue attribute has been set before you set the value.

    Hope this helps [:D]

    P.S. I wrote an article in my blog a while back on default value (before DynamicData Futures) this my give you some ideas, and you may fine it useful DynamicData Default Values in ForegnKey_Edit & other FieldTemplates

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 14, 2008 9:49 AM
  • User-330204900 posted

    I wanna ask how I should implement the IntegerSlider.ascx and DateAjaxCalendar.

    From what I've tested try adding

    [UIHint("DateAjaxCalendar")]
    and
    [UIHint("IntegerSlider")]

    They seemed to work for me albeit with a few display anomalies. [8-)]

    Monday, July 14, 2008 12:02 PM
  • User-1776315162 posted

    I still have many questions.
    I want the start date set to today and end date set to after 6 months.

    I try to add this function in the code behind of DateTime_Edit.ascx. But all the date fields of insert.aspx don't have default value.

        protected override void OnDataBinding(EventArgs e)
        {
            if (Mode == System.Web.UI.WebControls.DataBoundControlMode.Insert)
            {
            // you will need to apply and Data format or DataType when setting the value
            TextBox1.Text = DateTime.Now.ToString();
            }
            base.OnDataBinding(e);
        }

    Or do you mean I need to create other files under FieldTemplates folder e.g. DateTimeSt_Insert.ascx and DateTimeEn_insert.ascx for that?

    After editing the DateTimeSt_Insert.ascx and DateTimeEn_insert.ascx, what should I change in the Campaign.cs partial class in order to show the result?


    Also, my question about UIHint:
    I need to write a usercontrol IntegerSlider.ascx and DateAjaxCalendar under the folder FieldTemplates, right? Do you have any sample code for DateAjaxCalendar.ascx? I tried to add the ajaxcontroltoolket calendarextendar to it and a few display anomalies result. But in David Ebbo's blog, the calendar displays corrently.

    After writing these file, I have to change the code of CampaignMetaData class to the following code. Correct?

    [MetadataType(typeof(CampaignMetadata))]
    [DisplayColumnAttribute("Campaign_Name_EN")]
    public partial class Campaign
    {
        partial void OnCampaign_Name_ENChanging(string value)
        {
        }
    }

    public class CampaignMetadata{
        [DataType(DataType.Date)]
        [UIHint("DateTimeSt_Insert")]
        [UIHint("DateAjaxCalendar")]
        public DateTime Campaign_Date_St { get; set; }

        [DataType(DataType.Date)]
        [UIHint("DateTimeEn_Insert")]
        [UIHint("DateAjaxCalendar")]
        public DateTime Campaign_Date_En { get; set; }

        [UIHint("IntegerSlider")]
        public int amount {get; set;}
    }

     

    One more question, how can I change the order of columns shown in the list.aspx page?
    Thanks for all your answers.

    Monday, July 14, 2008 11:04 PM
  • User-1776315162 posted

    http://forums.asp.net/t/1289692.aspx

    I read this post and marcind's response:

    routes.Add(new Route("{*resource}", new StopRoutingHandler()) {

    Constraints = new RouteValueDictionary(new { resource = @".*WebResource\.axd" })

    }

    );

    I should add the above code in RegisterRoutes(RouteCollection routes) of Global.asax file. Right?

    Monday, July 14, 2008 11:22 PM
  • User-330204900 posted

    Yep thats correct I'm goinig to be testing this out later this morning and I post back to that thread to let you know how I get on.

    Tuesday, July 15, 2008 12:42 AM
  • User-1776315162 posted
    How can I indicate the use of one usercontrol on one field and another usercontrol on another field?

    I discover that I make a big mistake,
    the filename of start date should change to DateTimeSt_Insert.aspx, DateTimeEn_Insert.aspx
    And the code should change to:

    [
    DataType(DataType.Date)]
    [
    UIHint("DateTimeSt")]
    public DateTime Campaign_Date_St { get; set; }
    [
    DataType(DataType.Date)]
    [
    UIHint("DateTimeEn")]
    public DateTime Campaign_Date_En { get; set; }

    UIHint use the filename before _, i.e. DateTimeSt and DateTimeEn in this case.
    Tuesday, July 15, 2008 2:51 AM
  • User-330204900 posted

    DateTimeSt_Insert.aspx, DateTimeEn_Insert.aspx

    first of all the FieldTemplate names would end in _Edit not _Insert and yes all you need to do is add a UIHint of the FieldTemplate name you want to use.

    Tuesday, July 15, 2008 5:14 AM
  • User-797310475 posted

     Actually, they can end in _Insert, but they will only be activated in Insert mode (I know, we don't have any examples of this right now, but it's something that you can do).

    Tuesday, July 15, 2008 11:12 AM
  • User-330204900 posted

    Actually, they can end in _Insert, but they will only be activated in Insert mode (I know, we don't have any examples of this right now, but it's something that you can do).

    Thats cool. From what you said can I asume that if you have a FieldTemplate with an Edit and an Insert implementation the the Insert will override when ever in inser t mode?

    Tuesday, July 15, 2008 12:35 PM
  • User-797310475 posted

    yes, the Insert implementation will be used when in insert mode. by default if there is no Insert implementation it will fall back to the Edit version, which is why everything works with just the _Edit versions.

    Tuesday, July 15, 2008 1:34 PM
  • User1813417060 posted

    protected override void OnDataBinding(EventArgs e) { if (Mode == System.Web.UI.WebControls.DataBoundControlMode.Insert) { // you will need to apply and Data format or DataType when setting the value TextBox1.Text = DateTime.Now.ToString(); } base.OnDataBinding(e); }

    hmz why is it not working here?

    The code is executed as I can see with the debugger, but nothing appears.

    Here is my complete code 

    using System;
    using System.Collections.Specialized;
    using System.ComponentModel.DataAnnotations;
    using System.Web.DynamicData;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class DateTimeNow_InsertField : System.Web.DynamicData.FieldTemplateUserControl {
        private static DataTypeAttribute DefaultDateAttribute = new DataTypeAttribute(DataType.DateTime);
        protected void Page_Load(object sender, EventArgs e) {
            TextBox1.ToolTip = Column.Description;
            
            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);
            SetUpCustomValidator(DateValidator);
            
        }
        protected override void OnDataBinding(EventArgs e)
        {
            if (Mode == System.Web.UI.WebControls.DataBoundControlMode.Insert)
            {
                // you will need to apply and Data format or DataType when setting the value
                TextBox1.Text = DateTime.Now.ToString("dd-MM-yyyy");
            }
            base.OnDataBinding(e);
        }
        private void SetUpCustomValidator(CustomValidator validator) {
            if (Column.DataTypeAttribute != null) {
                switch (Column.DataTypeAttribute.DataType) {
                    case DataType.Date:
                    case DataType.DateTime:
                    case DataType.Time:
                        validator.Enabled = true;
                        DateValidator.ErrorMessage = HttpUtility.HtmlEncode(Column.DataTypeAttribute.FormatErrorMessage(Column.DisplayName));
                        break;
                }
            }
            else if (Column.ColumnType.Equals(typeof(DateTime))) {
                validator.Enabled = true;
                DateValidator.ErrorMessage = HttpUtility.HtmlEncode(DefaultDateAttribute.FormatErrorMessage(Column.DisplayName));
            }
        }
    
        protected void DateValidator_ServerValidate(object source, ServerValidateEventArgs args) {
            DateTime dummyResult;
            args.IsValid = DateTime.TryParse(args.Value, out dummyResult);
        }
    
        protected override void ExtractValues(IOrderedDictionary dictionary) {
            dictionary[Column.Name] = ConvertEditedValue(TextBox1.Text);
        }
    
        public override Control DataControl {
            get {
                return TextBox1;
            }
        }
    
    
    }
    
    Wednesday, June 8, 2011 10:22 AM
  • User-330204900 posted

    I suspect you problem is in the field templates markup have a look you probably find that the values of the textbox is set there also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2011 10:30 AM
  • User1813417060 posted

    Yes, thats was the trick,

    thanx Steve.

    By the way, I have 2 custom EntityTemplates

    1 for insert and 1 for edit of some tabledata.

    In the edit Template I want to evaluate whether the user is authenticatedor not and depending on that I want some fields to be in the readonly mode. Where can I set this, because in the code of the EntityTemplate, I tried the next code without succes:

    DynamicControl2.Mode = DataBoundControlMode.ReadOnly;

    And how do I evaluate whether the user is authenticated, because User.Identity,IsAuthenticated does not seem to work.

    Wednesday, June 8, 2011 12:46 PM
  • User-330204900 posted

    Have you debuged that and checked whether the code is reached?

    Wednesday, June 8, 2011 5:34 PM
  • User1813417060 posted

    Yes, the code is executed normally (I placed it in a try catch) without any exception, but the mode does not change

    Thursday, June 9, 2011 2:15 AM