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
Solved

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

Posted on 2016-09-08
3
48 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 43

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check number of row prior to displaying gridview 10 62
jquery tab header text 1 23
how to just get time from a date 6 33
Visual Studio npm 1 12
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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