/// <summary> /// Implementation of OnPreRender that links the Validator in the page with javascript code /// </summary> /// <param name="e"></param> protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); ClientScriptManager cm = Page.ClientScript; BaseValidator extendedValidator = this.GetExtendedValidator(); CustomValidator customValidator = extendedValidator as CustomValidator; if (extendedValidator != null) { if (customValidator != null) { customValidator.ClientValidationFunction = "CustomServerSideValidationEvaluateIsValid"; } else { string controlId = extendedValidator.ClientID; cm.RegisterExpandoAttribute(controlId, "evaluationfunction", "GenericServerSideValidationEvaluateIsValid", false); } } //Call GetCallbackEventReference to be able to do a Callback on the client-side cm.GetCallbackEventReference(this, "arg", "ServerSideValidationValidate", "context"); //Registers this control as one that requires postback handling when the page is posted back to the server. //This is to prevent full page validation when the postback is in fact a callback triggered by this control this.Page.RegisterRequiresPostBack(this); }
/// <summary> /// Adds the attributes to render. /// </summary> /// <param name="writer"></param> protected override void AddAttributesToRender(HtmlTextWriter writer) { base.AddAttributesToRender(writer); if (this.EnableClientScript) { ClientScriptManager csm = this.Page.ClientScript; // Build the argument which will be passed to the server when the callback happens. string argument = String.Format("ValidatorGetValue({0}.controltovalidate)", this.ClientID); // The actual callback function. string callbackFunction = csm.GetCallbackEventReference(this, argument, "__callbackValidatorCallback", "'" + this.ClientID + "'"); csm.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "CustomValidatorEvaluateIsValid", false); csm.RegisterExpandoAttribute(this.ClientID, "clientvalidationfunction", "__callbackValidatorValidate", false); csm.RegisterExpandoAttribute(this.ClientID, "callbackfunction", callbackFunction, false); if (this.ValidateEmptyText) { csm.RegisterExpandoAttribute(this.ClientID, "validateemptytext", "true", false); } } }
public static void RegisterExpandAttribute(Page p) { ClientScriptManager cs = p.ClientScript; cs.RegisterExpandoAttribute("Message", "title", "New title from client script.", true); }
/// <summary> /// Registers a name/value pair as a custom (expando) attribute of the specified control given a control ID, attribute name, and attribute value. /// </summary> /// <param name="controlId">The <see cref="T:System.Web.UI.Control"/> on the page that contains the custom attribute. /// </param><param name="attributeName">The name of the custom attribute to register. /// </param><param name="attributeValue">The value of the custom attribute. /// </param> public void RegisterExpandoAttribute(string controlId, string attributeName, string attributeValue) { _clientScriptManager.RegisterExpandoAttribute(controlId, attributeName, attributeValue); }