1

my (TextBox user control) Date property:

    [Category("Behavior"),
    Description("convert the current text to DateTime")]
    public DateTime? MyDate
    {
        get
        {                
            return ChangeDate();
        }
        set
        {
            Text = ToDateString(value);
        }
    }

and in asp.net:

<my:XTextBox ID="XTextBox1" runat="server"></my:XTextBox>

i want to see this property like Text Property on output:

<input name="XTextBox1" type="text" id="XTextBox1" MyDate="2013-10-11" />
                                                   ^^^^^^^^^^^^^^^^^^^

so in code behind i can access to MyDate like this?!

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = XTextBox1.MyDate;
}

thanks in advance..

2
  • What part of your code is not working? Commented Dec 28, 2013 at 11:11
  • the html output(input) not have attribute MyDate! Commented Dec 28, 2013 at 11:15

1 Answer 1

1

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.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.