Last active
June 2, 2017 17:18
-
-
Save JosiahSiegel/9007822 to your computer and use it in GitHub Desktop.
#ASP .NET TextBox: Set default & per page range validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyProject.Pages.MyPage" %> | |
<%@ Register TagPrefix="uc" TagName="StartDateRequired" Src="~/WebUserControl/StartDateRequired.ascx" %> | |
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> | |
</asp:Content> | |
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> | |
<uc:StartDateRequired ID="TextBoxStartDate" runat="server" /> | |
</asp:Content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void Page_LoadComplete(object sender, EventArgs e) | |
{ | |
TextBoxStartDate.MaxValue = DateTime.Now.ToShortDateString(); | |
TextBoxStartDate.MinValue = DateTime.Now.AddMonths(-2).ToShortDateString(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StartDateRequired.ascx.cs" Inherits="MyProject.WebUserControl.StartDateRequired" %> | |
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %> | |
<!-- Reusable text box start date with pop up calendar --> | |
<asp:TextBox ID="TextBoxStartDate" runat="server"></asp:TextBox> | |
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" | |
ErrorMessage="Start Date is required." Text="*" ValidationGroup="AllValidations" | |
ControlToValidate="TextBoxStartDate" SetFocusOnError="true" | |
CssClass="failureNotification"></asp:RequiredFieldValidator> | |
<asp:RegularExpressionValidator ID="regexpDate" runat="server" | |
ErrorMessage="Date must in the the format [m]m/[d]d/yyyy" Text="*" ControlToValidate="TextBoxStartDate" | |
ValidationExpression="^\d{1,2}/\d{1,2}/\d{4}$" ValidationGroup="AllValidations" /> | |
<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBoxStartDate" runat="server"></asp:CalendarExtender> | |
<asp:RangeValidator ID="RangeValidator2" runat="server" ErrorMessage="Start Date outside permitted range." | |
ControlToValidate="TextBoxStartDate" Type="Date" ValidationGroup="AllValidations" Text="*" SetFocusOnError="True" CssClass="failureNotification"> | |
</asp:RangeValidator> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
namespace MyProject.WebUserControl | |
{ | |
public partial class StartDateRequired : System.Web.UI.UserControl | |
{ | |
public string StringKey | |
{ | |
get | |
{ | |
return TextBoxStartDate.Text; | |
} | |
} | |
public string StringValue | |
{ | |
get | |
{ | |
return TextBoxStartDate.Text; | |
} | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
RangeValidator2.MaximumValue = DateTime.Now.ToShortDateString(); | |
RangeValidator2.MinimumValue = DateTime.Now.AddMonths(-1).ToShortDateString(); | |
} | |
public string MaxValue | |
{ | |
get { return (string)RangeValidator2.MaximumValue; } | |
set | |
{ | |
RangeValidator2.MaximumValue = value; | |
} | |
} | |
public string MinValue | |
{ | |
get { return (string)RangeValidator2.MinimumValue; } | |
set | |
{ | |
RangeValidator2.MinimumValue = value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment