Home All Groups Group Topic Archive Search About

Defining a ControlParameter of type DateTime for a DataSource

Author
7 Feb 2006 12:17 AM
Jeronimo Bertran
I created a DatePicker control that inherits from a textBox.   I have
defined an InnerDefaultProperty of type DateTime for the control as
follows:

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Description("Date input area")]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual DateTime Date
        {
            get
            {
                object o = ViewState["Date"];
                return ((o == null) ? DateTime.Now : (DateTime)o);
            }

            set
            {
                ViewState["Date"] = value;
            }
        }


I now want to use the date on the control as a parameter for an
ObjectDataSource.  When I configure my DataSource, the Define Parameters
window shows the DateTime parameter for the Select Method.  When I select
the Parameter Source as a control, the available controlIDs on the list
does not show my WebControl.  What do I have to do in order for my controls
to show on the list as valid parameters?

Thanks,

Jeronimo Bertran

Author
7 Feb 2006 9:10 AM
Steven Cheng[MSFT]
Hi Jeronimo,

Welcome to the ASP.NET newsgroup.

As for the defining a property in custom webcontrol which can be referenced
by DataSource control as ControlParameter, based on my research, we need to
use the "ControlValuePropertyAttribute" on the custom control to declare
that certain property.

#ControlValuePropertyAttribute Class
http://msdn2.microsoft.com/system.web.ui.controlvaluepropertyattribute.aspx

Also, the certain property need to be public and bindable. Here is simple
test control which expose a DateTime property which can be referenced by
DataSource control as control parameter:

=========================
namespace ControLibrary
{

    [ToolboxData("<{0}:DateTimePicker runat=server></{0}:DateTimePicker>")]
    [Designer(typeof(DateTimePickerDesigner),typeof(IDesigner))]
    [DefaultProperty("Date"), ControlValueProperty("Date",
typeof(DateTime), "1111/11/1")]
    public class DateTimePicker : WebControl, INamingContainer
    {
        private TextBox txt;



        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }




        [Category("Appearance")]
        [Description("Date input area")]
        [PersistenceMode(PersistenceMode.Attribute)]
        [DefaultValue(typeof(DateTime), "1111/11/1"), Bindable(true,
BindingDirection.TwoWay)]
        public virtual DateTime Date
        {
            get
            {
                object o = ViewState["Date"];
                return ((o == null) ? DateTime.Now : (DateTime)o);
            }

            set
            {
                ViewState["Date"] = value;
            }
        }


        protected override void CreateChildControls()
        {
            Controls.Clear();

            txt = new TextBox();

            txt.ID = "txtDate";
            txt.Text = DateTime.Now.ToString("yyyy/MM/dd");
            txt.TextChanged +=new EventHandler(txt_TextChanged);

            Controls.Add(txt);


        }

        protected void txt_TextChanged(object sender, EventArgs e)
        {
            DateTime date;
            try
            {
                date = DateTime.Parse(txt.Text);
                Date = date;
            }
            catch (Exception ex)
            {
                Page.Response.Write("<br/>" + ex.Message);
            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            txt.Text = Date.ToString("yyyy/MM/dd");
        }
    }


    public class DateTimePickerDesigner : ControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            DateTimePicker dtp = Component as DateTimePicker;

            string str = "<input type='text' value='{0}' />";

            str = string.Format(str, dtp.Date.ToString("yyyy/MM/dd"));

            return str;
        }
    }
}
===================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
7 Feb 2006 5:55 PM
Jeronimo Bertran
Thnaks that was very helpful.

Jeronimo
Author
9 Feb 2006 1:52 AM
Steven Cheng[MSFT]
You're welcome Jeronimo,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)