Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-08
3
Medium Priority
?
62 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 2000 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:Tom Knowlton
Tom 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:Tom Knowlton
ID: 41791339
Thank you for your time.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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