Category Archives: DynamicValidator

ASP TextBox with HtmlEditorExtender and ResizableControlExtender Issue

I m trying to build a resizable RichText Editor control.

I tried to use the HtmlEditorExtender and ResizableControlExtender but it seems as if they are mutually exclusive (If one works the other does not). The extender that works is the one that appears first.

Any suggestions

This is my code:

<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>
 
 <script>
     function ResizeTextBox(sender, eventargs)
     {
         var element = sender.control.element;
         element.children[2].style.height = element.style.height;
         return false;
     }
</script>

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" Enabled="false" />
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" Enabled="false" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" />
<asp:ResizableControlExtender ID="ResizableControlExtender1" TargetControlID="TextBox1" HandleCssClass="ResizableControlCSS" MinimumWidth="300" MinimumHeight="300" onclientresizing="ResizeTextBox" runat="server"></asp:ResizableControlExtender>
<asp:HtmlEditorExtender ID="HtmlEditorExtender1" TargetControlID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueEditString %>' CssClass="DDTextBox" TextMode="MultiLine" Width="300" Height="100" ></asp:TextBox>

[RESOLVED] How to use Dynamic Validator Control in Asp.Net 3.5

 Hello,


I am not able to find any Site which will tell how to use Dynamicvalidator control in asp.net 3.5 from scratch. SO please any help deeply appreciated.



Thanks

Hello,

HEre goes the complete overvies of DynamicData and DynamicValidator...

http://msdn.microsoft.com/en-us/library/aa478956.aspx

Thanks.

[RESOLVED] validating date and time

Hi,

I have 2 textboxes in my page.  One is supposed to hold the Date and the other Time.  These 2 data are merged when saving into one database field having a datetime datatype.  Upon retrieve, the datetime field value is split into 2 and put into the Date Textbox and Time TextBox.

Upon input, when I am trying to update, the validator always says "Invalid Time."

Please help!

Thanks for you input!

Tinac99

 

Designer Page

===========

Date: <asp:TextBox ID="TextBox1" runat="server" CssClass="DDTextBox" Text='<%# (FieldValueEditString != "") ? String.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(FieldValueEditString)) : "" %>' Columns="20"></asp:TextBox>

Time<asp:TextBox ID="TextBox2" runat="server" CssClass="DDTextBox" Text='<%# (FieldValueEditString != "") ? String.Format("{0:hh:mm tt}",Convert.ToDateTime(FieldValueEditString)) : "" %>'></asp:TextBox>

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" Enabled="false" ErrorMessage="Please Enter Date." />


<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1"
    CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static"
    Enabled="false" ErrorMessage="Please enter Date."
    ValidationExpression="(([0]?[1-9]|1[012])[- /.]([0]?[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d)|((1[012]|0[1-9])(3[01]|2\d|1\d|0[1-9])(19|20)\d\d) " />


<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" />


<asp:CustomValidator runat="server" ID="DateValidator" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" EnableClientScript="false" Enabled="false" OnServerValidate="DateValidator_ServerValidate" />

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" CssClass="DDControl DDValidator" ControlToValidate="TextBox2" Display="Static" Enabled="false"/>


<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="TextBox2"  ErrorMessage="Please enter Time."
    ValidationExpression="{0:t}"></asp:RegularExpressionValidator>

<asp:CustomValidator runat="server" ID="DateValidator2" CssClass="DDControl DDValidator" ControlToValidate="TextBox2" Display="Static" EnableClientScript="false" Enabled="false" OnServerValidate="DateValidator_ServerValidate" />

 

 

I think the time validation expression is not quite right...check out...

And also provide more details on what user input it is breaking....?? and also if possible some code...on how you are formatting the date and time together....

Hi,

tinac99

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="TextBox2"  ErrorMessage="Please enter Time."
    ValidationExpression="{0:t}"></asp:RegularExpressionValidator>
 

 

"{0:t}" is not a regular expression but a date time format specifier.

DateTime dateTime = System.DateTime.Now;
   Response.Write(string.Format("{0:t}", dateTime));


The output will be the format time such as "2:43 PM","4:50 AM".

If you want to use regular expression to validate above time, you can try this:

^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$

In validator control:

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="TextBox2"  ErrorMessage="Please enter Time."
    ValidationExpression="^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$"></asp:RegularExpressionValidator>

Hope this can help you.

hey

try using the range validator?

<asp:RangeValidator runat="server" ID="DateRV" ErrorMessage="Incorrect Date"
                        ControlToValidate="DateTxt" Text="*"></asp:RangeValidator>
     DateRV.MinimumValue = DateTime.MinValue.ToString("dd/MM/yyyy");
            DateRV.MaximumValue = DateTime.MaxValue.ToString("dd/MM/yyyy");


 

I don't think this can be done in asp.net. I've been trying to validate a datetime string using min & max dates and there doesn't seem to be any way to get this to work correctly.

[RESOLVED] Using Datepicker with FormView

Hi All,

I have a Web forms application using EF Code First. I am using ModelBinding, and all is going well. I decided to try my hand at DynamicDataEntities which I installed from NuGet packageManager. I have a FormView that is bound to my entity data model, and Im using the DynamicDataTemplates. I was just letting ASP build my form for me with the input fields, which worked fine, but, I need more control over the date fields. I want to add a jQuery Datepicker on the Date fields but I am having trouble.  I was using this:

 <%--I was using a DynamicDataEntiy to render my form from my Model--%>

<asp:FormView ID="addDogForm" runat="server" ItemType="PoochiesPrix_4.Models.DogInfo"
        InsertMethod="addDogForm_InsertItem" DefaultMode="Insert"
        RenderOuterTable="false" OnItemInserted="addDogForm_ItemInserted">
        <InsertItemTemplate>
        <fieldset>
            <ol>
                <%--<asp:DynamicEntity ID="DynamicEntity1" runat="server" Mode="Insert" />--%>
           </ol>
            <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Insert" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" OnClick="cancelButton_Click" />
        </fieldset>
    </InsertItemTemplate>
    </asp:FormView>

This worked fine, but, In my Model:

//I have date fields that I want to show a Datepicker for

public class DogInfo
    {
        [Key, Display(Name = "DogInfoID")]
        [ScaffoldColumn(false)]
        public int DogInfoID { get; set; }

        [Required(ErrorMessage = "Name is required"), StringLength(35), Display(Name = "Dogs Name")]
        public string name { get; set; }

        [Required(ErrorMessage = "License number is required"), StringLength(15), Display(Name = "License Number")]
        public string licenseNumber { get; set; }

        [Required(ErrorMessage = "A date is required"), Display(Name = "Date License was Renewed")]
        [Range(typeof(DateTime), "1/1/2014", "1/1/3000", ErrorMessage = "Please provide a renewal date after 1/1/2014")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
        public DateTime dateLicenseRenewed { get; set; }

        [Required(ErrorMessage = "A date is required"), Display(Name = "Date License Expires")]
        [Range(typeof(DateTime), "1/1/2014", "1/1/3000", ErrorMessage = "Please provide a date after 1/1/2014")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
        public DateTime dateLicenseExpires { get; set; }

        [Display(Name = "Coat Color")]
        public string coatColor { get; set; }

        [Display(Name = "Eye Color")]
        public string eyeColor { get; set; }

        [Required(ErrorMessage = "Enter Male or Female"), StringLength(6, MinimumLength = 4), Display(Name = "Sex")]
        public string sex { get; set; }

        [Display(Name = "Breed")]
        public string breed { get; set; }

        [Required(ErrorMessage = "Required. If you do not know your dogs DOB, enter an approximate date."), Display(Name = "Birth Date")]
        public DateTime birthDate { get; set; }

        [Display(Name = "Microchip Number")]
        public string microchipNumber { get; set; }

        [Display(Name = "Breed Registration")]
        public string breedRegistration { get; set; }

        [Display(Name = "Breed Shelter Name")]
        public string breedShelterName { get; set; }

        [Display(Name = "Date Acquired")]
        [Range(typeof(DateTime), "1/1/1900", "1/1/3000", ErrorMessage = "Please an approximate date after 1/1/1900")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
        public Nullable<DateTime> dateAcquired { get; set; }

        [ScaffoldColumn(false)]
        public Guid OwnerID { get; set; }

        [EnumDataType(typeof(DogType)), Display(Name = "Dog Type")]
        public DogType dogType{ get; set; }


        [ScaffoldColumn(false)]
        public ICollection<DogPic> DogPics { get; set; }

        [ScaffoldColumn(false)]
        public List<Veternarian> Veternarians { get; set; }
    }
    public enum DogType
    {
        Adult,
        Puppy
    }

I have tried explicitedly adding the entities to the page in its each DynamicControl like this:

HTML:

<asp:FormView ID="addDogForm" runat="server" ItemType="PoochiesPrix_4.Models.DogInfo"
        InsertMethod="addDogForm_InsertItem" DefaultMode="Insert"
        RenderOuterTable="false" OnItemInserted="addDogForm_ItemInserted">
        <InsertItemTemplate>
        <fieldset>
            <ol>
                <%--<asp:DynamicEntity ID="DynamicEntity1" runat="server" Mode="Insert" />--%>
                <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="name" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl2" runat="server" DataField="licenseNumber" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl3" runat="server" DataField="dateLicenseRenewed" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl4" runat="server" DataField="dateLicenseExpires" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl5" runat="server" DataField="coatColor" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl6" runat="server" DataField="eyeColor" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl7" runat="server" DataField="sex" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl8" runat="server" DataField="breed" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl9" runat="server" DataField="birthDate" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl10" runat="server" DataField="microchipNumber" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl11" runat="server" DataField="breedRegistration" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl12" runat="server" DataField="breedShelterName" Mode="Insert" />
                <asp:DynamicControl ID="DynamicControl13" runat="server" DataField="dateAcquired" Mode="Insert" />
                 <asp:DynamicControl ID="DynamicControl14" runat="server" DataField="dogType" Mode="Insert" />

            </ol>
            <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Insert" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" OnClick="cancelButton_Click" />
        </fieldset>
    </InsertItemTemplate>
    </asp:FormView>

Then editing the DateTime_Edit.ascx, Dynamic Template mark up:

<%-- Use the Juice Datepicker--%>

<juice:Datepicker runat="server" id="t1" targetcontrolid="DynamicControl3" />
<asp:TextBox ID="TextBox1" runat="server" CssClass="DDTextBox" Text='<%# FieldValueEditString %>' Columns="20"></asp:TextBox

Then in my DateTime_Edit.ascx.cs, I set a date range for my Calender:

protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.ToolTip = Column.Description;

            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);
            SetUpCustomValidator(DateValidator);

        //when my calendar displays, restrict the date range the user can pick.

            RangeAttribute ra = (RangeAttribute)Column.Attributes[typeof(RangeAttribute)];
            if (ra != null)
            {
                t1.MinDate = ra.Minimum.ToString();
                t1.MaxDate = ra.Maximum.ToString();

            }
                
        }

I am getting an error that the value is null in my calendar. I know why this is happening, because I am using EF Code first, and I am dropping and recreating my Database evertime I run my application if my Model changes. I am not sure how to set a default value in my code behind for this Calendar so that i do not get an error. I would like to set the current date as the date that displays. I believe that this will eliminate the null error too. I'm not sure though, since EF is using my database to build the form. i hope that I explained this clear enough. Iwould like some direction on how to go about setting default values on my DynamidDataTemplate fileds.

Hi,

If you want to set default date for the jQuery Datepicker, you could try:

$( ".selector" ).datepicker( "setDate", new Date());
// Or on the init
$( ".selector" ).datepicker({ defaultDate: new Date() });

For more information, you could refer to:

http://stackoverflow.com/questions/14580749/jquery-datepicker-set-default-date

http://stackoverflow.com/questions/4915990/set-todays-date-as-default-date-in-jquery-ui-datepicker

Regards

Shawn,

Thanks allot for the info.It helped allot. I have all of my DatePickers working now.I found that I just needed to set each Datepicker for TexBox1 in the DynamicDataEntity Template, DateTime_Edit.ascx:

<juice:Datepicker runat="server" id="t1" targetcontrolid="TextBox1" />
<juice:Datepicker runat="server" id="t2" targetcontrolid="TextBox1" />
<juice:Datepicker runat="server" id="t3" targetcontrolid="TextBox1" />
<juice:Datepicker runat="server" id="t4" targetcontrolid="TextBox1" />
<asp:TextBox ID="TextBox1" runat="server" CssClass="DDTextBox" Text='<%# FieldValueEditString %>' Columns="20"></asp:TextBox>

Then in the DateTime_Edit.cs file, i did this:

RangeAttribute ra = (RangeAttribute)Column.Attributes[typeof(RangeAttribute)];
            if (ra != null)
            {
                t1.MinDate = ra.Minimum.ToString();
                t1.MaxDate = ra.Maximum.ToString();
                t2.MinDate = ra.Minimum.ToString();
                t2.MaxDate = ra.Maximum.ToString();
                t4.MinDate = ra.Minimum.ToString();
                t4.MaxDate = ra.Maximum.ToString();

            }

In my Page, I put a script:

<script type="text/javascript">
        $(function () {
            $('#t1').datepicker();
            $('#t1').datepicker('setDate', new Date());
        });
    </script>

For each one that I wanted a default date set in. Actually, when I set the targetcontrolid to "TextBox1, the page ran fine." Thanks for the links though. They are good. Cheers!