/// <summary> /// Raises the PreRender event. /// </summary> /// <param name="e"> /// An EventArgs object that contains the event data. /// </param> /// <remarks> /// <para> /// This method notifies the server control to perform any necessary prerendering /// steps prior to saving view state and rendering content. /// Registers client side dynamic scripts for the Picker control. /// Page code behind should not change Picker.PickerFormat after PreRender (e.g. PrerenderComplete) /// ClientScript validation would hold different format than Picker.Value . /// </para> /// <para> /// ClientScript registration is made here because is the last useful life cycle phase to register /// client script resources. Array declaration is made here too to not duplicate the /// ClientScriptManager object declaration. /// </para> /// <para> /// Array declaration is made in Picker instead of the adapter because m_Format.Months is not /// visible for protection level and we don't want to make it visible. /// Besides, array name is made unambiguous in case of different instances of Picker have /// different formats (LongMonthdate, ShortMonthDate). /// </para> /// </remarks> protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); const string clientScriptPath = "AutoComplete.Scripts.IM.AutoComplete.js"; const string jqueryScriptPath = "AutoComplete.Scripts.jquery.js"; const string jqueryUIScriptPath = "AutoComplete.Scripts.jquery-ui.js"; if (Enabled) { ClientScriptManager clientScriptManager = Page.ClientScript; clientScriptManager.RegisterClientScriptResource(GetType(), clientScriptPath); if (RegisterJQueryAndJQueryUI) { clientScriptManager.RegisterClientScriptResource(GetType(), jqueryScriptPath); clientScriptManager.RegisterClientScriptResource(GetType(), jqueryUIScriptPath); } if (RegisterDefaultStyleSheet) { AddDefaultStyleSheet(); } AutoCompleteSettingsDto settings = AutoCompleteSettingsMapper.GetDtoFrom(this); string initializeAutoComplete = AutoCompleteInitializer.GetJsInitialization(settings); Type controlType = GetType(); string scriptName = "autoComplete" + ClientID; if (!clientScriptManager.IsStartupScriptRegistered(controlType, scriptName)) { clientScriptManager.RegisterStartupScript(controlType, scriptName, initializeAutoComplete, true); } } }
private static string GetInitializeAutoCompleteScript(HtmlHelper htmlHelper, string name, Uri url, string labelField, string valueField, int minChars, string selectionCallback, string errorCallback, string errorMessage, string noResultsMessage, HttpMethod httpMethod) { const string defaultMessage = "A value must be provided for the specified argument."; if (string.IsNullOrEmpty(name)) { throw new ArgumentException(string.Format("Required argument: name.\r\n{0}", defaultMessage)); } string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (string.IsNullOrEmpty(fullName)) { throw new ArgumentException(string.Format("Required argument: fullName.\r\n{0}", defaultMessage)); } if (url == null) { throw new ArgumentException(string.Format("Required argument: url for: {0}\r\n{1}", name, defaultMessage)); } if (string.IsNullOrEmpty(url.OriginalString)) { throw new ArgumentException(string.Format("Required argument: url for: {0}\r\n{1}", name, defaultMessage)); } if (string.IsNullOrEmpty(valueField)) { throw new ArgumentException(string.Format("Required argument: valueField for: {0}\r\n{1}", name, defaultMessage)); } string controlId = fullName; string absoluteUrl = UrlHelper.GenerateContentUrl(url.ToString(), htmlHelper.ViewContext.HttpContext); // script with the invocation of InitializeAutocComplete on the rendered TextBox TagBuilder scriptTagBuilder = new TagBuilder("script"); scriptTagBuilder.MergeAttribute("type", "text/javascript"); AutoCompleteSettingsDto settings = AutoCompleteSettingsMapper.GetDtoFrom (controlId, new Uri(absoluteUrl, UriKind.RelativeOrAbsolute), httpMethod, minChars, labelField, valueField, selectionCallback, errorCallback, errorMessage, noResultsMessage); string initializeAutoComplete = AutoCompleteInitializer.GetJsInitialization(settings); scriptTagBuilder.InnerHtml = initializeAutoComplete; return(scriptTagBuilder.ToString(TagRenderMode.Normal)); }