You can add an attribute to the user control like below:
XTextBox.ascx(UserControl markup):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="XTextBox.ascx.cs" Inherits="UserControlTest.XTextBox" %>
<asp:TextBox ID="tbText" runat="server"></asp:TextBox>
XTextBox.ascx.cs (UserControl code):
using System;
using System.ComponentModel;
namespace UserControlTest
{
public partial class XTextBox : System.Web.UI.UserControl
{
[Category("Behavior"), Description("convert the current text to DateTime")]
public DateTime? MyDate
{
get
{
return ChangeDate();
}
set
{
tbText.Text = ToDateString(value);
}
}
private string ToDateString(DateTime? dt )
{
return dt.HasValue ? ((DateTime)dt).ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");
}
private DateTime? ChangeDate()
{
DateTime dt;
if (DateTime.TryParse(tbText.Text, out dt))
{
tbText.Attributes.Add("MyDate", tbText.Text);
return dt;
}
tbText.Attributes.Add("MyDate", "");
return null;
}
}
}
And use the control in your page:
TestForm.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="UserControlTest.TestForm" %>
<%@ Register Src="~/XTextBox.ascx" TagPrefix="uc1" TagName="XTextBox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:XTextBox runat="server" id="XTextBox" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
TestForm.aspx.cs:
using System;
namespace UserControlTest
{
public partial class TestForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = XTextBox.MyDate.HasValue? ((DateTime)XTextBox.MyDate).ToString("yyyy-MM-dd") : "";
}
protected void Button1_Click(object sender, EventArgs e){ }
}
}
How to test:
First time when the test Page loads, usercontrol's MyDate is null. So the label in the test form will not display anything. In the markup you can see the attribute is null:
<input type="text" mydate="" id="XTextBox_tbText" name="XTextBox$tbText">
Type valid date in the textbox in test form and click the button. The label should show the date and in the markup you can see the MyDate attribute.

MyDate!