/// <summary>
        /// Loads the appropriate jScript library out of the scripts directory and
        /// injects into a WebForms page.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="jQueryUiUrl">Optional url to jQuery as a virtual or absolute server path</param>
        public static void LoadjQueryUi(Control control, string jQueryUiUrl)
        {
            ClientScriptProxy p = ClientScriptProxy.Current;

            // jQuery UI isn't provided as a Web Resource so default to a fixed URL
            if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                //throw new InvalidOperationException(Resources.WebResourceNotAvailableForJQueryUI);
                jQueryUiUrl = UrlUtils.ResolveUrl(jQueryUiLocalFallbackUrl);
            }

            if (!string.IsNullOrEmpty(jQueryUiUrl))
            {
                p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUiUrl, ScriptRenderModes.Header);
            }
            else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                // Load from CDN Url specified
                p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUiCdnUrl, ScriptRenderModes.Header);

                // check if jquery loaded - if it didn't we're not online and use WebResource
                string scriptCheck =
                    @"if (typeof(jQuery.ui) == 'undefined')  
        document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));";

                p.RegisterClientScriptBlock(control,
                                            typeof(ControlResources), "jquery_ui",
                                            string.Format(scriptCheck, UrlUtils.ResolveUrl(jQueryUiLocalFallbackUrl)),
                                            true, ScriptRenderModes.Header);
            }

            return;
        }
        /// <summary>
        /// Loads jQuery depending on configuration settings (CDN, WebResource or site url)
        /// and injects the full script link into the page.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="jQueryUrl">Optional url to jQuery as a virtual or absolute server path</param>
        public static void LoadjQuery(Control control, string jQueryUrl)
        {
            ClientScriptProxy p = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(jQueryUrl))
            {
                p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUrl, ScriptRenderModes.HeaderTop);
            }
            else if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                p.RegisterClientScriptResource(control, typeof(ControlResources), ControlResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop);
            }
            else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                // Load from CDN Url specified
                p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryCdnUrl, ScriptRenderModes.HeaderTop);

                // check if jquery loaded - if it didn't we're not online and use WebResource
                string scriptCheck =
                    @"if (typeof(jQuery) == 'undefined')  
        document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));";

                jQueryUrl = p.GetClientScriptResourceUrl(control, typeof(ControlResources),
                                                         ControlResources.JQUERY_SCRIPT_RESOURCE);
                p.RegisterClientScriptBlock(control, typeof(ControlResources),
                                            "jquery_register", string.Format(scriptCheck, jQueryUrl), true,
                                            ScriptRenderModes.HeaderTop);
            }

            return;
        }
Exemple #3
0
        protected override void OnPreRender(EventArgs e)
        {
            Page.RegisterRequiresPostBack(this);

            if (string.IsNullOrEmpty(SelectedTab) && _Tabs.Count > 0)
            {
                SelectedTab = _Tabs[0].TabPageClientId;
            }

            // Persist our selection into a hidden var since it's all client side
            // Script updates this var
            ClientScriptProxy.RegisterHiddenField(this, HIDDEN_FORMVAR_PREFIX + ClientID, SelectedTab);

            string script =
                @"
function ActivateTab(tabId, num)
{
    var _Tabs = eval(tabId);
    if (_Tabs.onActivate && _Tabs.onActivate(num))
        return; 
   
    if (typeof(num)=='string') {
        for(var x=0; x< _Tabs.length; x++) {
            if (_Tabs[x].pageId == num)
            { num = x; break;}
        }
    }
    if (typeof(num)=='string') num=0;
    var Tab = _Tabs[num];
    for(var i=0; i<_Tabs.length; i++) {
        var t = _Tabs[i];
        document.getElementById(t.tabId).className = (t.enabled ? '" + TabCssClass + "' : '" + DisabledTabCssClass + @"');
        if (t.pageId)
            document.getElementById(t.pageId).style.display = 'none';
    }
    document.getElementById(Tab.tabId).className = '" + SelectedTabCssClass + @"';
    document.getElementById(Tab.pageId).style.display = '';
    document.getElementById('" + HIDDEN_FORMVAR_PREFIX + ClientID + @"').value=Tab.pageId;
}
";

            // Register only once per page
            ClientScriptProxy.RegisterClientScriptBlock(this, typeof(ControlResources), "ActivateTab", script, true);


            StringBuilder sb = new StringBuilder(2048);

            sb.AppendFormat("var {0} = [];\r\n", ClientID);
            for (int i = 0; i < TabPages.Count; i++)
            {
                string  iStr = i.ToString();
                TabPage tab  = TabPages[i];
                sb.Append(ClientID + "[" + iStr + "] = { id: " + iStr + "," +
                          "tabId: '" + ClientID + "_" + iStr + "'," +
                          "pageId: '" + tab.TabPageClientId + "'," +
                          "enabled: " + tab.Enabled.ToString().ToLower() + "};\r\n");
            }

            ClientScriptProxy.RegisterClientScriptBlock(this, typeof(ControlResources), "TabInit_" + ClientID, sb.ToString(), true);


            // Force the page to show the Selected Tab
            if (SelectedTab != "")
            {
                script = "ActivateTab('" + ClientID + "','" + SelectedTab + "');\r\n";
                ClientScriptProxy.RegisterStartupScript(this, typeof(ControlResources), "TabStartup_" + ClientID, script, true);
            }

            base.OnPreRender(e);
        }
        /// <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);
        }