• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 68
  • Last Modified:

C# / ASP.NET - Custom Server Control - Property does not seem to keep setting.

I have a custom server control being consumed by a webform.

In the Page Load of the webform I am trying to set the Property "DateText" inside the server control, and it appears to work, but when the form is finished loading, the value is not there.

webform:

("EndDt" is an instance of the custom server control)

If Not Page.IsPostBack Then

...

...

If Not (Request.Params("EndDt") Is Nothing) Then
                EndDt.DateText = Request.Params("EndDt")
            Else
                EndDt.DateText = getDateByCulture(DateTime.Today) 'System.DateTime.Today.Month.ToString() & "/" & System.DateTime.Today.Day.ToString() & "/" & System.DateTime.Today.Year.ToString()
            End If

...


...

Open in new window



I set a breakpoint inside the setter for "DateText" in the server control, and the execution stops there when DateText is set.

But, once the page is done rendering, the <input> field containing the date value is empty.  : /


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DealerSocket.Web.WebForms.Controls.CSharp
{
    [DefaultProperty("CaptionText")]
    [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]
    public class DatePicker : WebControl
    {
        private string ClearSetting = "";
        private string MakeReadOnlySetting = "";
        private string TextBoxEnabledSetting = "";


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

            string resourceName = "";
            //javascript
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.javascript.bootstrap.min.js";
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.javascript.datePickerSC.js";
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.javascript.date.js";
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.javascript.moment.js";            
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.javascript.moment-datepicker.js";
            RCSR(resourceName);
            
  

            //css
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.css.bootstrap.min.css";
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.css.font-awesome.min.css";
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.css.datepicker.css";
            RCSR(resourceName);
            resourceName = "DealerSocket.Web.WebForms.Controls.CSharp.css.datepicker2.css";
            RCSR(resourceName);

            this.EnableViewState = true;
        }

        private void RCSR(string s)
        {
            ClientScriptManager cs = this.Page.ClientScript;
            cs.RegisterClientScriptResource(typeof(DealerSocket.Web.WebForms.Controls.CSharp.DatePicker), s);
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("");
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("");
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool TextBoxEnabled
        {
            get
            {
                bool s = (bool)ViewState["TextBoxEnabled"];
                return s;
            }

            set
            {
                TextBoxEnabledSetting = "disabled";
                ViewState["TextBoxEnabled"] = value;
                if (value)
                {
                    TextBoxEnabledSetting = "";
                }
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("false")]
        [Localizable(true)]
        public bool ShowClearButton
        {
            get
            {
                bool s = (bool)ViewState["ShowClearButton"];
                return s;
            }

            set
            {
                ClearSetting = "none";
                ViewState["ShowClearButton"] = value;
                if (value)
                {
                    ClearSetting = "table-cell";
                }
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool MakeReadOnly
        {
            get
            {
                bool s = (bool)ViewState["MakeReadOnly"];                    
                return s;
            }

            set
            {
                MakeReadOnlySetting = "readonly";
                ViewState["MakeReadOnly"] = value;
                if (value)
                {
                    MakeReadOnlySetting = "";
                }
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DateText
        {
            get
            {
                String s = (String)ViewState["DateText"];
                return s;
            }

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

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string GetPreviousBusinessDay
        {
            get
            {
                DateTime dt = new DateTime();
                dt = DateTime.Now;


                do
                {
                    dt = dt.AddDays(-1);
                }
                while (IsWeekend(dt));
                DateText = dt.ToShortDateString();                
                return dt.ToShortDateString();                
            }
        }

        private bool IsWeekend(DateTime date)
        {
            return date.DayOfWeek == DayOfWeek.Saturday ||
                   date.DayOfWeek == DayOfWeek.Sunday;
        } 

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string GetNextBusinessDay
        {
            get
            {
                DateTime dt = new DateTime();
                dt = DateTime.Now;


                do
                {
                    dt = dt.AddDays(1);
                }
                while (IsWeekend(dt));
                DateText = dt.ToShortDateString();
                return dt.ToShortDateString();
            }
        }




        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CaptionText
        {
            get
            {
                String s = (String)ViewState["CaptionText"];
                return s;
            }

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

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ID
        {
            get
            {
                String s = (String)ViewState["ID"];
                return s;
            }

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


        protected override void RenderContents(HtmlTextWriter output)
        {
            string temp = CaptionText;
            temp += "<div id='ctl00_body_" + ID + "_divDatePick' class='input-group date' style='width: 100%;'>";
            temp += "<input name='ctl00$body$" + ID + "$IntntlDatePick' type='datetime' id='IntntlDatePick' class='form-control' autocomplete='off' onclick='javascript:openDP($(this));'" + MakeReadOnlySetting + " " + TextBoxEnabledSetting + " />";
            temp += "<span class='input-group-addon' style='cursor: pointer;' onclick='javascript:openDP($(this));'>";
            temp += "<span class='fa fa-calendar'></span></span>";
            temp += "<span id='ctl00_body" + ID + "_clrparen' class='input-group-addon' style='cursor: pointer; display:" + ClearSetting + ";' onclick='javascript:ClearButtonRefreshState($(this).siblings(\"input\").attr(\"name\"));'>";
            temp += "<span id='ctl00_body" + ID + "_clrbtn' style='cursor: pointer;' class='fa fa-eraser' onclick='javascript:ClearButtonRefreshState($(this).parentsUntil(\"div\").children(\"input\").attr(\"name\"));'></span></span>";
            temp += "</div>";

            output.Write(temp);
        }


    }
}

Open in new window

0
Tom Knowlton
Asked:
Tom Knowlton
  • 2
2 Solutions
 
zephyr_hex (Megan)DeveloperCommented:
Are you sure this should be:

EndDt.DateText.Value = "some string";

Open in new window


or

EndDt.DateText.Text = "some string";

Open in new window


or

EndDt.Text = "some string";

Open in new window


(instead of EndDt.DateText = "some string")

Also, could you please show us the aspx for the control?
0
 
Tom KnowltonWeb developerAuthor Commented:
Well, I think I fixed that problem.

I was not updating the rendered output with the Date value passed into DateText.

temp += "<input name='ctl00$body$" + ID + "$IntntlDatePick' type='datetime' id='IntntlDatePick' class='form-control' autocomplete='off' onclick='javascript:openDP($(this));'" + MakeReadOnlySetting + " " + TextBoxEnabledSetting + " value='" + DateTextSetting + "' />";

Open in new window

0
 
Tom KnowltonWeb developerAuthor Commented:
Thank you for your time.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now