Solved

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

Posted on 2016-09-08
3
45 Views
Last Modified: 2016-09-09
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
Comment
Question by:knowlton
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41790391
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
 
LVL 5

Assisted Solution

by:knowlton
knowlton earned 0 total points
ID: 41791336
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
 
LVL 5

Author Closing Comment

by:knowlton
ID: 41791339
Thank you for your time.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question