Home All Groups Group Topic Archive Search About

Value for Composite control cannot be retrieved

Author
25 Feb 2006 7:32 PM
Jaime Stuardo
Hi all..

I have programmed a composite control that is compound by one TextBox and
one ImageButton. This is intended to be a popup calendar. All works well,
except when the value is retrieved.

For example, I placed that control inside a FormView control that is
databound. When the page is ran and the user enters a date in the textbox,
after he presses the submit image button on the page and it is posted back
to the same page, the default date value is retrieved for the date when the
datasource is going to update the record. When I debug the control, I can
see that the date textbox contains a null value.

This is the code of my control that is related to the date and the textbox.

-------------------------------------------------------------

[DefaultProperty("Date")]
[ToolboxData("<{0}:DateSelector runat=server></{0}:DateSelector>")]
public class DateSelector : CompositeControl
{
    private TextBox _txtDate = new TextBox();
    private Image _imgCalendar = new Image();
    private CompareValidator _dateCompareValidator = new CompareValidator();

    #region " .Propiedades. "

    [Bindable(true)]
    [Category("Data")]
    [DefaultValue("01/01/0001")]
    [Localizable(true)]
    [Description("Fecha seleccionada")]
    public DateTime Date
    {
        get
        {
            EnsureChildControls();
            return
(_txtDate.Text.Equals(String.Empty))?DateTime.Today:DateTime.Parse(_txtDate.Text);
        }
        set
        {
            EnsureChildControls();
            if (value == DateTime.MinValue)
                _txtDate.Text = "";
            else
                _txtDate.Text = value.ToString("dd/MM/yyyy");
        }
    }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("Imagen del botón del calendario")]
    [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design",
    typeof(System.Drawing.Design.UITypeEditor))]
    public string ImageFile
    {
        get
        {
            return _imgCalendar.ImageUrl;
        }
        set
        {
            _imgCalendar.ImageUrl = value;
        }
    }

    #endregion

    protected override void CreateChildControls()
    {
        // Agrega el textbox
        _txtDate.ID = "txtDate";
        _txtDate.Columns = 10;
        this.Controls.Add(_txtDate);
        // Agrega la imagen del calendario
        _imgCalendar.ID = "imgCalendar";
        string script = "javascript:popUpCalendar(this,
document.getElementById('" + _txtDate.ClientID + "'), 'dd-mm-yyyy',
'__doPostBack (document.getElementById(\\'" + _txtDate.ClientID + "\\'))')";
        _imgCalendar.Attributes.Add("onclick", script);
        _imgCalendar.ImageAlign = ImageAlign.AbsMiddle;
        _imgCalendar.Attributes.Add("style", "cursor:hand;");
        this.Controls.Add(_imgCalendar);
        base.CreateChildControls();
    }

    protected override void OnPreRender(EventArgs e)
    {
        // Agrega el stylesheet del calendario
        if (!String.IsNullOrEmpty(_urlStyleSheet))
        {
            HtmlLink style_link = new HtmlLink();
            style_link.Attributes.Add("href",
VirtualPathUtility.ToAbsolute(_urlStyleSheet));
            style_link.Attributes.Add("rel", "stylesheet");
            style_link.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(style_link);
        }
        // Agrega el archivo JS
        if (!String.IsNullOrEmpty(_urlScript))
        {
            // Inicializa la variable JS imgDir que contiene el path de las
imágenes
            HtmlGenericControl js_var = new HtmlGenericControl("script");
            js_var.Attributes.Add("type", "text/javascript");
            js_var.InnerHtml = "imgDir = '" +
VirtualPathUtility.ToAbsolute(VirtualPathUtility.GetDirectory(_imgCalendar.ImageUrl))
+ "';";
            Page.Header.Controls.Add(js_var);
            // Incluye el archivo JS
            HtmlGenericControl js = new HtmlGenericControl("script");
            js.Attributes.Add("type", "text/javascript");
            js.Attributes.Add("src",
VirtualPathUtility.ToAbsolute(_urlScript));
            Page.Header.Controls.Add(js);
        }
    }

    protected override object SaveViewState()
    {
        EnsureChildControls();
        object[] state = new object[2];
        object objBase = base.SaveViewState();
        state[0] = objBase;
        state[1] = _txtDate.Text;
        return state;
    }

    protected override void LoadViewState(object savedState)
    {
        object[] state = (object[])savedState;
        base.LoadViewState(state[0]);
        EnsureChildControls();
        _txtDate.Text = state[1].ToString();
    }
}

-------------------------------------------------------------

Any help will be greatly appreciated
Thanks

Jaime

Author
27 Feb 2006 2:03 PM
Warren Patterson
Hi,

Okay, I have a similiar problem with a control I am creating, I have shelved
that control for now, but since then I have come across something in my
travels through cyberspace :-)

_txtDate.ID = "txtDate";
should be something like
_txtDate.ID = me.ClientID; or _txtDate.ClientID = "txtDate";  not sure which
one, havent tried this yet.

Not sure if this will even help, but I thought I'd give you my 2 cents
worth.

HTH



Show quoteHide quote
"Jaime Stuardo" <jstua***@manquehue.net> wrote in message
news:ujIk$IkOGHA.2604@TK2MSFTNGP09.phx.gbl...
> Hi all..
>
> I have programmed a composite control that is compound by one TextBox and
> one ImageButton. This is intended to be a popup calendar. All works well,
> except when the value is retrieved.
>
> For example, I placed that control inside a FormView control that is
> databound. When the page is ran and the user enters a date in the textbox,
> after he presses the submit image button on the page and it is posted back
> to the same page, the default date value is retrieved for the date when
> the datasource is going to update the record. When I debug the control, I
> can see that the date textbox contains a null value.
>
> This is the code of my control that is related to the date and the
> textbox.
>
> -------------------------------------------------------------
>
> [DefaultProperty("Date")]
> [ToolboxData("<{0}:DateSelector runat=server></{0}:DateSelector>")]
> public class DateSelector : CompositeControl
> {
>    private TextBox _txtDate = new TextBox();
>    private Image _imgCalendar = new Image();
>    private CompareValidator _dateCompareValidator = new
> CompareValidator();
>
>    #region " .Propiedades. "
>
>    [Bindable(true)]
>    [Category("Data")]
>    [DefaultValue("01/01/0001")]
>    [Localizable(true)]
>    [Description("Fecha seleccionada")]
>    public DateTime Date
>    {
>        get
>        {
>            EnsureChildControls();
>            return
> (_txtDate.Text.Equals(String.Empty))?DateTime.Today:DateTime.Parse(_txtDate.Text);
>        }
>        set
>        {
>            EnsureChildControls();
>            if (value == DateTime.MinValue)
>                _txtDate.Text = "";
>            else
>                _txtDate.Text = value.ToString("dd/MM/yyyy");
>        }
>    }
>
>    [Bindable(true)]
>    [Category("Appearance")]
>    [DefaultValue("")]
>    [Localizable(true)]
>    [Description("Imagen del botón del calendario")]
>    [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design",
>    typeof(System.Drawing.Design.UITypeEditor))]
>    public string ImageFile
>    {
>        get
>        {
>            return _imgCalendar.ImageUrl;
>        }
>        set
>        {
>            _imgCalendar.ImageUrl = value;
>        }
>    }
>
>    #endregion
>
>    protected override void CreateChildControls()
>    {
>        // Agrega el textbox
>        _txtDate.ID = "txtDate";
>        _txtDate.Columns = 10;
>        this.Controls.Add(_txtDate);
>        // Agrega la imagen del calendario
>        _imgCalendar.ID = "imgCalendar";
>        string script = "javascript:popUpCalendar(this,
> document.getElementById('" + _txtDate.ClientID + "'), 'dd-mm-yyyy',
> '__doPostBack (document.getElementById(\\'" + _txtDate.ClientID +
> "\\'))')";
>        _imgCalendar.Attributes.Add("onclick", script);
>        _imgCalendar.ImageAlign = ImageAlign.AbsMiddle;
>        _imgCalendar.Attributes.Add("style", "cursor:hand;");
>        this.Controls.Add(_imgCalendar);
>        base.CreateChildControls();
>    }
>
>    protected override void OnPreRender(EventArgs e)
>    {
>        // Agrega el stylesheet del calendario
>        if (!String.IsNullOrEmpty(_urlStyleSheet))
>        {
>            HtmlLink style_link = new HtmlLink();
>            style_link.Attributes.Add("href",
> VirtualPathUtility.ToAbsolute(_urlStyleSheet));
>            style_link.Attributes.Add("rel", "stylesheet");
>            style_link.Attributes.Add("type", "text/css");
>            Page.Header.Controls.Add(style_link);
>        }
>        // Agrega el archivo JS
>        if (!String.IsNullOrEmpty(_urlScript))
>        {
>            // Inicializa la variable JS imgDir que contiene el path de las
> imágenes
>            HtmlGenericControl js_var = new HtmlGenericControl("script");
>            js_var.Attributes.Add("type", "text/javascript");
>            js_var.InnerHtml = "imgDir = '" +
> VirtualPathUtility.ToAbsolute(VirtualPathUtility.GetDirectory(_imgCalendar.ImageUrl))
> + "';";
>            Page.Header.Controls.Add(js_var);
>            // Incluye el archivo JS
>            HtmlGenericControl js = new HtmlGenericControl("script");
>            js.Attributes.Add("type", "text/javascript");
>            js.Attributes.Add("src",
> VirtualPathUtility.ToAbsolute(_urlScript));
>            Page.Header.Controls.Add(js);
>        }
>    }
>
>    protected override object SaveViewState()
>    {
>        EnsureChildControls();
>        object[] state = new object[2];
>        object objBase = base.SaveViewState();
>        state[0] = objBase;
>        state[1] = _txtDate.Text;
>        return state;
>    }
>
>    protected override void LoadViewState(object savedState)
>    {
>        object[] state = (object[])savedState;
>        base.LoadViewState(state[0]);
>        EnsureChildControls();
>        _txtDate.Text = state[1].ToString();
>    }
> }
>
> -------------------------------------------------------------
>
> Any help will be greatly appreciated
> Thanks
>
> Jaime
>

Bookmark and Share