/// <summary> /// Helper function that is used to load script resources for various AJAX controls /// Loads a script resource based on the following scriptLocation values: /// /// * WebResource /// Loads the Web Resource specified out of WebResources. Specify the resource /// that applied in the resourceName parameter /// /// * Url/RelativeUrl /// loads the url with ResolveUrl applied /// /// * empty (no value) /// No action is taken /// </summary> /// <param name="control">The control instance for which the resource is to be loaded</param> /// <param name="scriptLocation">WebResource, a Url or empty (no value)</param> /// <param name="resourceName">The name of the resource when WebResource is used for scriptLocation</param> /// <param name="topOfHeader">Determines if scripts are loaded into the header whether they load at the top or bottom</param> public void LoadControlScript(Control control, string scriptLocation, string resourceName, ScriptRenderModes renderMode) { ClientScriptProxy proxy = ClientScriptProxy.Current; // Specified nothing to do if (string.IsNullOrEmpty(scriptLocation)) { return; } ScriptItem scriptItem = new ScriptItem(); scriptItem.Src = scriptLocation; scriptItem.RenderMode = renderMode; if (scriptLocation == "WebResource") { scriptItem.Resource = resourceName; scriptItem.ResourceAssembly = null; scriptItem.Src = resourceName; } //// It's a relative url //if (ClientScriptProxy.LoadScriptsInHeader) // proxy.RegisterClientScriptIncludeInHeader(control, control.GetType(), // control.ResolveUrl(scriptLocation), topOfHeader); //else // proxy.RegisterClientScriptInclude(control, control.GetType(), // Path.GetFileName(scriptLocation), control.ResolveUrl(scriptLocation)); AddScript(scriptItem); }
/// <summary> /// This method just builds the various JavaScript blocks as strings /// and assigns them to the ClientScript object. /// </summary> /// <param name="e"></param> protected override void OnPreRender(EventArgs e) { if (!IsCallback) { ClientScriptProxy = ClientScriptProxy.Current; // If we're not in a callback provide script to client if (!string.IsNullOrEmpty(jQueryScriptLocation)) { ScriptLoader.LoadjQuery(this.Page); } ScriptLoader.LoadwwjQuery(this.Page, false); // Generate the class wrapper and class loader function GenerateControlSpecificJavaScript(); } else { if (PageProcessingMode == CallbackProcessingModes.PagePreRender) { HandleCallback(); } } }
/// <summary> /// Renders the scripts contained in this control /// </summary> /// <param name="writer"></param> protected override void Render(HtmlTextWriter writer) { // Use ClientScript Proxy for all script injection except inline ClientScriptProxy scriptProxy = ClientScriptProxy.Current; writer.WriteLine(); // empty line foreach (ScriptItem script in InternalScripts) { // We only need to handle inline here - others have been handled in OnPreRender if (script.RenderMode != ScriptRenderModes.Inline) { continue; } // Allow inheriting from control which is the default behavior if (script.RenderMode == ScriptRenderModes.Inherit) { script.RenderMode = RenderMode; } // Fix up url to allow ~/ syntax string src = ResolveUrl(script.Src); if (script.AllowMinScript && !HttpContext.Current.IsDebuggingEnabled) { src = src.ToLower().Replace(".js", MinScriptExtension); } writer.Write("<script src=\"" + src + "\" type=\"text/javascript\"></script>\r\n"); } }
/// <summary> /// Code that embeds related resources (.js and css) /// </summary> /// <param name="scriptProxy"></param> protected void RegisterResources(ClientScriptProxy scriptProxy) { // Use ScriptProxy to load jQuery and ww.Jquery ScriptLoader.LoadjQuery(this); ScriptLoader.LoadjQueryUi(this, null); // if a theme is provided embed a link to the stylesheet if (!string.IsNullOrEmpty(Theme)) { string cssPath = this.CssBasePath.Replace("/base/", "/" + Theme + "/"); scriptProxy.RegisterCssLink(Page, typeof(WebResources), cssPath, cssPath); } }
/// <summary> /// Handle writing out scripts to header or 'ASP.NET script body' /// </summary> /// <param name="e"></param> protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Use ClientScript Proxy for all script injection except inline scriptProxy = ClientScriptProxy.Current; // Now process all but inline scripts - // inline scripts must be rendered in Render() foreach (ScriptItem script in InternalScripts) { RegisterScriptItem(script); } }
/// <summary> /// Code that embeds related resources (.js and css) /// </summary> /// <param name="scriptProxy"></param> protected void RegisterResources(ClientScriptProxy scriptProxy) { scriptProxy.LoadControlScript(this, jQueryJs, WebResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop); scriptProxy.RegisterClientScriptInclude(Page, typeof(WebResources), CalendarJs, ScriptRenderModes.Header); string cssPath = CalendarCss; if (!string.IsNullOrEmpty(Theme)) { cssPath = cssPath.Replace("/base/", "/" + Theme + "/"); } scriptProxy.RegisterCssLink(Page, typeof(WebResources), cssPath, cssPath); }
protected override void OnInit(EventArgs e) { base.OnInit(e); if (!DesignMode) { ScriptProxy = ClientScriptProxy.Current; // Use ScriptProxy to load jQuery and ww.Jquery if (!string.IsNullOrEmpty(jQueryScriptLocation)) { ScriptLoader.LoadjQuery(this); } ScriptLoader.LoadwwjQuery(this, false); } }
///// <summary> ///// Renders the container ///// </summary> ///// <param name="writer"></param> //protected override void Render(HtmlTextWriter writer) //{ // if (Text == "" && !DesignMode) // { // base.Render(writer); // return; // } // if (RenderMode == RenderModes.Text) // Text = HtmlUtils.DisplayMemo(Text); // string TStyle = Style["position"]; // bool IsAbsolute = false; // if (TStyle != null && TStyle.Trim() == "absolute") // IsAbsolute = true; // // <Center> is still the only reliable way to get block structures centered // if (!IsAbsolute && Center) // { // writer.AddStyleAttribute(HtmlTextWriterStyle.MarginLeft, "auto"); // writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, "auto"); // // In designmode we want to write out a container so it // // so the designer properly shows the controls as block control // if (DesignMode) // writer.Write("<div style='width:100%'>"); // } // writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID); // writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass); // writer.AddStyleAttribute(HtmlTextWriterStyle.Width, Width.ToString()); // foreach (string Key in Style.Keys) // { // writer.AddStyleAttribute(Key, Style[Key]); // } // writer.RenderBeginTag(HtmlTextWriterTag.Div); // writer.RenderBeginTag(HtmlTextWriterTag.Table); // writer.RenderBeginTag(HtmlTextWriterTag.Tr); // // Set up image <td> tag // writer.RenderBeginTag(HtmlTextWriterTag.Td); // if (ErrorImage != "") // { // string ImageUrl = ErrorImage.ToLower(); // if (ImageUrl == "warningresource") // ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), WebResources.WARNING_ICON_RESOURCE); // else if (ImageUrl == "inforesource") // ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), WebResources.INFO_ICON_RESOURCE); // else // ImageUrl = ResolveUrl(ErrorImage); // writer.AddAttribute(HtmlTextWriterAttribute.Src, ImageUrl); // writer.RenderBeginTag(HtmlTextWriterTag.Img); // writer.RenderEndTag(); // } // writer.RenderEndTag(); // image <td> // // Render content <td> tag // writer.RenderBeginTag(HtmlTextWriterTag.Td); // if (UserMessage != "") // writer.Write("<span style='font-weight:normal'>" + UserMessage + "</span><hr />"); // writer.Write(Text); // writer.RenderEndTag(); // Content <td> // writer.RenderEndTag(); // </tr> // writer.RenderEndTag(); // </table> // writer.RenderEndTag(); // </div> // if (!IsAbsolute) // { // if (Center) // { // if (DesignMode) // writer.Write("</div>"); // </div> // } // writer.WriteBreak(); // } //} protected override void OnPreRender(EventArgs e) { ClientScriptProxy = ClientScriptProxy.Current; if (Visible && !string.IsNullOrEmpty(Text) && DisplayTimeout > 0) { // Embed the client script library as Web Resource Link ScriptLoader.LoadjQuery(this); string Script = @"setTimeout(function() { $('#" + ClientID + @"').fadeOut('slow') }," + DisplayTimeout.ToString() + ");"; //@"window.setTimeout(""document.getElementById('" + ClientID + @"').style.display='none';""," + DisplayTimeout.ToString() + @");"; ClientScriptProxy.RegisterStartupScript(this, typeof(ErrorDisplay), "DisplayTimeout", Script, true); } base.OnPreRender(e); }
/// <summary> /// Read the HtmlGeneric Script controls and parse them into /// Internal scripts at page load time /// </summary> /// <param name="e"></param> protected override void OnInit(EventArgs e) { // Save a Per Request instance in Page.Items so we can retrieve it // generically from code with wwScriptContainer.Current if (!Page.Items.Contains(STR_CONTEXTID)) { Page.Items[STR_CONTEXTID] = this; } else { // ScriptContainer already exists elsewhere on the page - combine scripts ScriptContainer container = Page.Items[STR_CONTEXTID] as ScriptContainer; foreach (HtmlGenericControl scriptItem in container.Scripts) { this.Scripts.Add(scriptItem); } } base.OnInit(e); scriptProxy = ClientScriptProxy.Current; // Pick up HtmlGeneric controls parse into Script objects // and add to Internal scripts for final processing foreach (HtmlGenericControl ctl in Scripts) { // Take generic control and parse into Script object ScriptItem script = ParseScriptProperties(ctl); if (script == null) { continue; } // Insert into the list at the head of the list before 'manually' // added script entries //AddScript(script); // Register these items immediately at startup // script refs defined in doc take precendence over code added ones RegisterScriptItem(script); } }
protected override void OnInit(EventArgs e) { base.OnInit(e); ClientScriptProxy = ClientScriptProxy.Current; }
/// <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) { // Ignore if we're calling this in a Callback if (AjaxMethodCallback.IsCallback) { return; } base.OnPreRender(e); // MS AJAX aware script management ClientScriptProxy scriptProxy = ClientScriptProxy.Current; // Register resources RegisterResources(scriptProxy); // Capture and map the various option parameters StringBuilder sbOptions = new StringBuilder(512); sbOptions.Append("{"); if (!string.IsNullOrEmpty(OnClientSelection)) { sbOptions.Append("select: " + OnClientSelection + ","); } else { sbOptions.AppendLine( @"select: function (e, ui) { $(""#" + this.UniqueID + @""").val(ui.item.value); },"); } if (CallbackHandler != null) { // point the service Url back to the current page method if (AjaxMethodCallback != null) { ServerUrl = Page.Request.Url.LocalPath + "?" + "Method=AutoCompleteCallbackHandler&CallbackTarget=" + AjaxMethodCallback.ID; } } if (!string.IsNullOrEmpty(ServerUrl)) { sbOptions.Append("source: \"" + ServerUrl + "\","); } sbOptions.AppendFormat("autoFocus: {0},delay: {1},minLength: {2},", AutoFocus.ToString().ToLower(), Delay, MinLength); // strip off trailing , sbOptions.Length--; sbOptions.Append("}"); // Write out initilization code for calendar StringBuilder sbStartupScript = new StringBuilder(400); sbStartupScript.AppendLine("$(document).ready( function() {"); sbStartupScript.AppendFormat(" var autocompl = $(\"#{0}\").autocomplete({1});\r\n", ClientID, sbOptions.ToString()); // close out the script function sbStartupScript.AppendLine("} );"); scriptProxy.RegisterStartupScript(Page, typeof(WebResources), "_cal" + UniqueID, sbStartupScript.ToString(), true); }
/// <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(WebResources), WebResources.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: " + WebUtils.EncodeJsDate(MaxDate.Value) + ","); } if (MinDate.HasValue) { sbOptions.Append("minDate: " + WebUtils.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(WebResources), "__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)) { WestwindJsonSerializer ser = new WestwindJsonSerializer(); 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(WebResources), 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(WebResources), "_cal" + UniqueID, sbStartupScript.ToString(), true); }