Пример #1
0
        /// <summary>
        /// Most of the work happens here for generating the hook up script code
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // MS AJAX aware script management
            ClientScriptProxy scriptProxy = ClientScriptProxy.Current;

            // Register resources
            RegisterResources(scriptProxy);

            string dateFormat = DateFormat;

            if (string.IsNullOrEmpty(dateFormat) || dateFormat == "Auto")
            {
                // Try to create a data format string from culture settings
                // this code will fail if culture can't be mapped on server hence the empty try/catch
                try
                {
                    dateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
                }
                catch { }
            }

            dateFormat = dateFormat.ToLower().Replace("yyyy", "yy");

            // Capture and map the various option parameters
            StringBuilder sbOptions = new StringBuilder(512);

            sbOptions.Append("{");

            string onSelect = OnClientSelect;

            if (DisplayMode == DatePickerDisplayModes.Button)
            {
                sbOptions.Append("showOn: 'button',");
            }
            else if (DisplayMode == DatePickerDisplayModes.ImageButton)
            {
                string img = ButtonImage;
                if (img == "WebResource")
                {
                    img = scriptProxy.GetWebResourceUrl(this, typeof(ControlResources), ControlResources.CALENDAR_ICON_RESOURCE);
                }
                else
                {
                    img = ResolveUrl(ButtonImage);
                }

                sbOptions.Append("showOn: 'button', buttonImageOnly: true, buttonImage: '" + img + "',buttonText: 'Select date',");
            }
            else if (DisplayMode == DatePickerDisplayModes.Inline)
            {
                // need to store selection in the page somehow for inline since it's
                // not tied to a textbox
                scriptProxy.RegisterHiddenField(this, ClientID, Text);
                onSelect = ClientID + "OnSelect";
            }

            if (!string.IsNullOrEmpty(onSelect))
            {
                sbOptions.Append("onSelect: " + onSelect + ",");
            }

            if (DisplayMode != DatePickerDisplayModes.Inline)
            {
                if (!string.IsNullOrEmpty(OnClientBeforeShow))
                {
                    sbOptions.Append("beforeShow: function(y,z) { $('#ui-datepicker-div').maxZIndex(); " +
                                     OnClientBeforeShow + "(y,z); },");
                }
                else
                {
                    sbOptions.Append("beforeShow: function() { $('#ui-datepicker-div').maxZIndex(); },");
                }
            }

            if (MaxDate.HasValue)
            {
                sbOptions.Append("maxDate: " + UrlUtils.EncodeJsDate(MaxDate.Value) + ",");
            }

            if (MinDate.HasValue)
            {
                sbOptions.Append("minDate: " + UrlUtils.EncodeJsDate(MinDate.Value) + ",");
            }

            if (ShowButtonPanel)
            {
                sbOptions.Append("showButtonPanel: true,");
            }

            sbOptions.Append("dateFormat: '" + dateFormat + "'}");


            // Write out initilization code for calendar
            StringBuilder sbStartupScript = new StringBuilder(400);

            sbStartupScript.AppendLine("$( function() {");


            if (DisplayMode != DatePickerDisplayModes.Inline)
            {
                scriptProxy.RegisterClientScriptBlock(Page,
                                                      typeof(ControlResources),
                                                      "__attachDatePickerInputKeys",
                                                      AttachDatePickerKeysScript, true);

                sbStartupScript.AppendFormat("var cal = jQuery('#{0}').datepicker({1}).attachDatepickerInputKeys();\r\n",
                                             ClientID, sbOptions);
            }
            else
            {
                sbStartupScript.AppendLine("var cal = jQuery('#" + ClientID + "Div').datepicker(" + sbOptions.ToString() + ")");

                if (SelectedDate.HasValue && SelectedDate.Value > new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc))
                {
                    CustomJsonSerializer ser = new CustomJsonSerializer();
                    ser.DateSerializationMode = JsonDateEncodingModes.NewDateExpression;
                    string jsDate = ser.Serialize(SelectedDate);

                    sbStartupScript.AppendLine("cal.datepicker('setDate'," + jsDate + ");");
                }
                else
                {
                    sbStartupScript.AppendLine("cal.datepicker('setDate',new Date());");
                }

                // Assign value to hidden form var on selection
                scriptProxy.RegisterStartupScript(this, typeof(ControlResources), UniqueID + "OnSelect",
                                                  "function  " + ClientID + "OnSelect(dateStr) {\r\n" +
                                                  ((!string.IsNullOrEmpty(OnClientSelect)) ? OnClientSelect + "(dateStr);\r\n" : "") +
                                                  "jQuery('#" + ClientID + "')[0].value = dateStr;\r\n}\r\n", true);
            }

            sbStartupScript.AppendLine("} );");
            scriptProxy.RegisterStartupScript(Page, typeof(ControlResources), "_cal" + UniqueID,
                                              sbStartupScript.ToString(), true);
        }
Пример #2
0
        protected override void OnPreRender(EventArgs e)
        {
            StringBuilder startupScript = new StringBuilder(2048);

            string DhId = DragHandleID;

            if (string.IsNullOrEmpty(DragHandleID))
            {
                DragHandleID = ClientID;
            }

            Control Ctl = FindControl(DragHandleID);

            if (Ctl != null)
            {
                DhId = Ctl.ClientID;
            }

            startupScript.AppendLine("\t$('#" + ClientID + "')");

            if (Closable && !string.IsNullOrEmpty(DragHandleID))
            {
                string imageUrl = CloseBoxImage;
                if (imageUrl == "WebResource")
                {
                    imageUrl = ScriptProxy.GetWebResourceUrl(this, GetType(), ControlResources.CLOSE_ICON_RESOURCE);
                }

                StringBuilder closableOptions = new StringBuilder("imageUrl: '" + imageUrl + "'");

                if (!string.IsNullOrEmpty(DragHandleID))
                {
                    closableOptions.Append(",handle: $('#" + DragHandleID + "')");
                }

                if (!string.IsNullOrEmpty(ClientDialogHandler))
                {
                    closableOptions.Append(",handler: " + ClientDialogHandler);
                }

                if (FadeOnClose)
                {
                    closableOptions.Append(",fadeOut: 'slow'");
                }

                startupScript.AppendLine("\t\t.closable({ " + closableOptions + "})");
            }

            string options = "";

            if (Draggable)
            {
                // force auto stacking of windows (last dragged to top of zIndex)
                options = "{  stack: \"*\", opacity: 0.80, dragDelay: " + DragDelay.ToString();

                if (!string.IsNullOrEmpty(DragHandleID))
                {
                    options += ",handle:'#" + DragHandleID + "'";
                }

                if (!string.IsNullOrEmpty(Cursor))
                {
                    options += ",cursor:'" + Cursor + "'";
                }

                options += " }";

                startupScript.AppendLine("\t\t.draggable(" + options + " )");
            }

            if (ShadowOffset != 0)
            {
                startupScript.AppendLine(
                    "\t\t.shadow({ opacity:" +
                    ShadowOpacity.ToString(CultureInfo.InvariantCulture.NumberFormat) +
                    ",offset:" +
                    ShadowOffset.ToString() + "})");
            }

            if (Centered)
            {
                startupScript.AppendLine(
                    "\t\t.centerInClient()");
            }

            startupScript.Length = startupScript.Length - 2;  // strip last CR/LF \r\n
            startupScript.AppendLine(";");

            string script = "$( function() {\r\n" + startupScript + "});";

            ScriptProxy.RegisterStartupScript(this, GetType(), ID + "_DragBehavior",
                                              script, true);

            base.OnPreRender(e);
        }