views:

134

answers:

3

I have the following code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DurationPicker.ascx.cs" Inherits="Permias.DurationPicker" %>
<%@ Register TagPrefix="Club" Namespace="ClubSite" %>
<div class="controlblock">
    <table>
        <tr>
            <td>
                Start Date:
            </td>
            <td>
                <Club:DatePicker ID="dp1" runat="server" />
            </td>
            <td>
                <Club:TimePicker ID="tp1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                End Date:
            </td>
            <td>
                <Club:DatePicker ID="dp2" runat="server" />
            </td>
            <td>
                <Club:TimePicker ID="tp2" runat="server" />
            </td>
        </tr>
    </table>
</div>

In my code behind I have

public System.DateTime startDateTime
    {
        get
        {
            return dp1.SelectedDate.Add(tp1.SelectedTime.TimeOfDay);
        }
        set
        {
            dp1.SelectedDate = value;
            tp1.SelectedTime = value;
        }
    }

However dp1 is underlined in red, which means it can't find dp1.. why is this?

+1  A: 

You need more information in the @Register directive. Where is this control located? Either an assembly reference or a src reference.

Bryan
src attribute is missing
citronas
dp1 is a datepicker
EquinoX
A: 

dp1 must be defined in your code-behind. Are you relying on the designer to create a field in the code behind? If so, perhaps it did not create the dp1 reference in the DurationPicker.ascx.designer.cs file. You will need a line something like this:

protected ClubSite.DatePicker dp1;
mcliedtk
A: 

none of these seems to resolve my issue....

in my solution I have a App_Code folder and inside that I have DataPicker.cs which is under the namespace ClubSite

namespace ClubSite
{
    public static class commonScript
    {

        public static void WritePopupRoutines(System.Web.UI.Page Page)
        {
            StringBuilder sb = new StringBuilder();
            sb = new StringBuilder();
            sb.AppendLine("var __popup_panel;");

            sb.AppendLine("function __popup_clear() {");
            sb.AppendLine(" if (__popup_panel != null ) ");
            sb.AppendLine(" {");
            sb.AppendLine(" document.getElementById(__popup_panel).style.display='none';");
            sb.AppendLine(" __popup_panel=null;");
            sb.AppendLine(" }");
            sb.AppendLine("}");
            sb.AppendLine("function __popup_losefocus(panel)");
            sb.AppendLine("{");
            sb.AppendLine(" if (!panel.contains(document.activeElement))");
            sb.AppendLine(" {");
            sb.AppendLine(" panel.style.display='none';");
            sb.AppendLine(" }");
            sb.AppendLine("}");

            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "PopupRoutines", sb.ToString(), true);
        }
    }

    public class DatePicker : WebControl, INamingContainer
    {
        private Calendar _innerCal;
        private TextBox _innerTbx;
        private string errorText = null;
        private bool _panelvisible = false;

        public DatePicker() : base(HtmlTextWriterTag.Div)
        {

        }

        public System.DateTime SelectedDate
        {
            get
            {
                EnsureChildControls();
                System.DateTime d=System.DateTime.Now;
                try
                {
                    d = System.DateTime.Parse(_innerTbx.Text);
                    errorText = null;
                    _innerCal.SelectedDate = d;
                }
                catch
                {
                    errorText = "Date needs to be specified as mm/dd/yyyy";
                }
                return d;
            }
            set
            {
                EnsureChildControls();
                _innerCal.SelectedDate = value;
                _innerTbx.Text = value.ToShortDateString();
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            _innerTbx = new TextBox();
            this.Controls.Add(_innerTbx);

            _innerCal = new Calendar();
            _innerCal.SelectionChanged += new System.EventHandler(_innerCal_SelectionChanged);
            _innerCal.VisibleMonthChanged += new MonthChangedEventHandler(_innerCal_MonthChanged);
            Controls.Add(_innerCal);
        }

        protected override System.Web.UI.HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            if (this.Width.IsEmpty)
            {
                this.Width = new Unit(150);
            }
            base.AddAttributesToRender(writer);
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            _innerTbx.Attributes.Add("Align", "AbsMiddle");
            _innerTbx.Width = new Unit(100);
            _innerTbx.RenderControl(writer);

            string innerid = this.UniqueID + "_inner";

            writer.AddAttribute("Align", "AbsMiddle");
            writer.AddAttribute("src", "images/dropdownbtn.gif");
            writer.AddAttribute("onClick", "__datepicker_showpopup('" + innerid + "')");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();

            if (errorText != null)
            {
                writer.AddStyleAttribute("color", "red");
                writer.AddStyleAttribute("display", "block");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(errorText);
                writer.RenderEndTag();
            }

            writer.AddStyleAttribute("position", "relative");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddStyleAttribute("position", "absolute");
            writer.AddStyleAttribute("left", "0px");
            writer.AddStyleAttribute("top", "0px");
            writer.AddStyleAttribute("z-index", "100");

            string panelvisible = _panelvisible ? "block" : panelvisible = "none";

            writer.AddStyleAttribute("display", panelvisible);
            writer.AddStyleAttribute("background-color", "white");
            writer.AddAttribute("id", innerid);
            writer.AddAttribute("onfocusout", "__popup_losefocus(this)");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            _innerCal.RenderControl(writer);

            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);
            commonScript.WritePopupRoutines(Page);
            StringBuilder sb = new StringBuilder();
            if (_panelvisible)
            {
                sb.AppendLine("__popup_panel = '" + this.UniqueID + "_inner';");
            }
            sb.AppendLine("function __datepicker_showpopup(name)");
            sb.AppendLine("{");
            sb.AppendLine(" if (__popup_panel != null)");
            sb.AppendLine(" {");
            sb.AppendLine(" document.getElementById(__popup_panel).style.display='none';");
            sb.AppendLine(" }");
            sb.AppendLine(" __popup_panel=name;");
            sb.AppendLine(" var panel=document.getElementById(__popup_panel);");
            sb.AppendLine(" panel.style.display='block';");
            sb.AppendLine(" var links=panel.getElementsByTagName('A');");
            sb.AppendLine(" links[0].focus();");
            sb.AppendLine(" window.event.cancelBubble=true;");
            sb.AppendLine("}");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "popup", sb.ToString(), true);
            Page.MaintainScrollPositionOnPostBack = true;
        }

        private void _innerCal_SelectionChanged(object sender, System.EventArgs e)
        {
            EnsureChildControls();
            _innerTbx.Text = _innerCal.SelectedDate.ToShortDateString();
        }

        private void _innerCal_MonthChanged(object sender, MonthChangedEventArgs e)
        {
            _panelvisible = true;
        }
    }
}
EquinoX