?
Solved

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

Posted on 2016-09-08
3
Medium Priority
?
60 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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