/// <summary> /// Handles the Click event of the btnToggleLabels control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnToggleLabels_Click(object sender, EventArgs e) { foreach (var control in pnlDetails.Controls) { if (control is IRockControl) { IRockControl labeledControl = control as IRockControl; if (string.IsNullOrWhiteSpace(labeledControl.Label)) { labeledControl.Label = string.Format("Rock:{0}", labeledControl.GetType().Name); } else { labeledControl.Label = string.Empty; } } else if (control is HtmlGenericControl) { HtmlGenericControl hg = (control as HtmlGenericControl); if (hg.TagName.Equals("h4", StringComparison.OrdinalIgnoreCase)) { hg.Visible = !hg.Visible; } } } }
/// <summary> /// Handles the CheckedChanged event of the tglLabels control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void tglLabels_CheckedChanged(object sender, EventArgs e) { foreach (var control in pnlDetails.ControlsOfTypeRecursive <WebControl>()) { if (control is IRockControl) { IRockControl rockControl = control as IRockControl; if (tglLabels.Checked) { rockControl.Label = string.Format("Rock:{0}", rockControl.GetType().Name); } else { rockControl.Label = string.Empty; } } } }
/// <summary> /// Renders the control which handles adding all the IRockControl common pieces (Label, Help, etc.). /// </summary> /// <param name="rockControl">The rock control.</param> /// <param name="writer">The writer.</param> /// <param name="additionalCssClass">The additional CSS class.</param> public static void RenderControl(IRockControl rockControl, HtmlTextWriter writer, string additionalCssClass = "") { bool renderLabel = (!string.IsNullOrEmpty(rockControl.Label)); bool renderHelp = (rockControl.HelpBlock != null && !string.IsNullOrWhiteSpace(rockControl.Help)); if (renderLabel) { var cssClass = new StringBuilder(); cssClass.AppendFormat("form-group {0}", rockControl.GetType().Name.SplitCase().Replace(' ', '-').ToLower()); if (((Control)rockControl).Page.IsPostBack && !rockControl.IsValid) { cssClass.Append(" has-error"); } if (rockControl.Required) { cssClass.Append(" required"); } if (!string.IsNullOrWhiteSpace(additionalCssClass)) { cssClass.Append(additionalCssClass); } writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass.ToString()); writer.RenderBeginTag(HtmlTextWriterTag.Div); if (!(rockControl is RockLiteral)) { writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-label"); writer.AddAttribute(HtmlTextWriterAttribute.For, rockControl.ClientID); } writer.RenderBeginTag(HtmlTextWriterTag.Label); writer.Write(rockControl.Label); if (renderHelp) { rockControl.HelpBlock.RenderControl(writer); } writer.RenderEndTag(); } rockControl.RenderBaseControl(writer); if (!renderLabel && renderHelp) { rockControl.HelpBlock.RenderControl(writer); } if (rockControl.RequiredFieldValidator != null) { if (rockControl.Required) { rockControl.RequiredFieldValidator.Enabled = true; if (string.IsNullOrWhiteSpace(rockControl.RequiredFieldValidator.ErrorMessage)) { rockControl.RequiredFieldValidator.ErrorMessage = rockControl.Label + " is Required."; } rockControl.RequiredFieldValidator.RenderControl(writer); } else { rockControl.RequiredFieldValidator.Enabled = false; } } if (renderLabel) { writer.RenderEndTag(); } }
/// <summary> /// Renders the control which handles adding all the IRockControl common pieces (Label, Help, etc.). /// </summary> /// <param name="rockControl">The rock control.</param> /// <param name="writer">The writer.</param> /// <param name="additionalCssClass">The additional CSS class.</param> public static void RenderControl(IRockControl rockControl, HtmlTextWriter writer, string additionalCssClass = "") { bool renderLabel = (!string.IsNullOrEmpty(rockControl.Label)); bool renderHelp = (rockControl.HelpBlock != null && !string.IsNullOrWhiteSpace(rockControl.Help)); bool renderWarning = (rockControl.WarningBlock != null && !string.IsNullOrWhiteSpace(rockControl.Warning)); /* 2020-02-11 MDP * If renderLabel is false (the label is blank), required inputs won't get highlighted if they are blank. * This is due to the lack of form-group around input that don't have label. * There isn't a fix for this yet. Adding a form-group div around these would cause styling and javascript hook problems (since the dom is different) * In the meantime, you could manually add a form-group div around these required inputs, but be careful so it doesn't break styling (even on custom themes) */ if (renderLabel) { var cssClass = new StringBuilder(); cssClass.AppendFormat("form-group {0} {1}", rockControl.GetType().Name.SplitCase().Replace(' ', '-').ToLower(), rockControl.FormGroupCssClass); if (((Control)rockControl).Page.IsPostBack && !rockControl.IsValid) { cssClass.Append(" has-error"); } if (rockControl.Required) { if ((rockControl is IDisplayRequiredIndicator) && !(rockControl as IDisplayRequiredIndicator).DisplayRequiredIndicator) { // if this is a rock control that implements IDisplayRequiredIndicator and DisplayRequiredIndicator is false, don't add the " required " cssclass } else { cssClass.Append(" required"); } } if (!string.IsNullOrWhiteSpace(additionalCssClass)) { cssClass.Append(" " + additionalCssClass); } writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass.ToString()); writer.RenderBeginTag(HtmlTextWriterTag.Div); if (!(rockControl is RockLiteral)) { writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-label"); writer.AddAttribute(HtmlTextWriterAttribute.For, rockControl.ClientID); } if (rockControl is WebControl) { // if the control has a Display Style, make sure the Label, Help, and Warning also get the same Display style // For example, you might have rockControl.Style["Display"] = "none", so you probably want the label, help, and warning to also get not displayed var rockControlDisplayStyle = (rockControl as WebControl).Style[HtmlTextWriterStyle.Display]; if (rockControlDisplayStyle != null) { writer.AddStyleAttribute(HtmlTextWriterStyle.Display, rockControlDisplayStyle); if (rockControl.HelpBlock != null) { rockControl.HelpBlock.Style[HtmlTextWriterStyle.Display] = rockControlDisplayStyle; } if (rockControl.WarningBlock != null) { rockControl.WarningBlock.Style[HtmlTextWriterStyle.Display] = rockControlDisplayStyle; } } } writer.RenderBeginTag(HtmlTextWriterTag.Label); writer.Write(rockControl.Label); if (renderHelp) { rockControl.HelpBlock.RenderControl(writer); } if (renderWarning) { rockControl.WarningBlock.RenderControl(writer); } writer.RenderEndTag(); if (rockControl is IRockControlAdditionalRendering) { ((IRockControlAdditionalRendering)rockControl).RenderAfterLabel(writer); } writer.Write(" "); // add space for inline forms, otherwise the label butts right up to the control writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-wrapper"); writer.RenderBeginTag(HtmlTextWriterTag.Div); } rockControl.RenderBaseControl(writer); if (renderLabel) { writer.RenderEndTag(); } if (!renderLabel && renderHelp) { rockControl.HelpBlock.RenderControl(writer); } if (!renderLabel && renderWarning) { rockControl.WarningBlock.RenderControl(writer); } if (rockControl.RequiredFieldValidator != null) { if (rockControl.Required) { rockControl.RequiredFieldValidator.Enabled = true; if (string.IsNullOrWhiteSpace(rockControl.RequiredFieldValidator.ErrorMessage)) { // if the control is a Label, use that. Otherwise, if the control has PlaceHolder, use that. string requiredName = string.Empty; if (rockControl.Label.IsNotNullOrWhiteSpace()) { requiredName = rockControl.Label; } else if (rockControl is RockTextBox) { requiredName = (rockControl as RockTextBox).Placeholder; } rockControl.RequiredFieldValidator.ErrorMessage = requiredName + " is required."; } rockControl.RequiredFieldValidator.RenderControl(writer); } else { rockControl.RequiredFieldValidator.Enabled = false; } } if (renderLabel) { writer.RenderEndTag(); } }
/// <summary> /// Renders the control which handles adding all the IRockControl common pieces (Label, Help, etc.). /// </summary> /// <param name="rockControl">The rock control.</param> /// <param name="writer">The writer.</param> /// <param name="additionalCssClass">The additional CSS class.</param> public static void RenderControl( IRockControl rockControl, HtmlTextWriter writer, string additionalCssClass = "" ) { bool renderLabel = ( !string.IsNullOrEmpty( rockControl.Label ) ); bool renderHelp = ( rockControl.HelpBlock != null && !string.IsNullOrWhiteSpace( rockControl.Help ) ); if ( renderLabel ) { var cssClass = new StringBuilder(); cssClass.AppendFormat( "form-group {0} {1}", rockControl.GetType().Name.SplitCase().Replace( ' ', '-' ).ToLower(), rockControl.FormGroupCssClass ); if ( ( (Control)rockControl ).Page.IsPostBack && !rockControl.IsValid ) { cssClass.Append( " has-error" ); } if ( rockControl.Required ) { if ( ( rockControl is IDisplayRequiredIndicator ) && !( rockControl as IDisplayRequiredIndicator ).DisplayRequiredIndicator ) { // if this is a rock control that implements IDisplayRequiredIndicator and DisplayRequiredIndicator is false, don't add the " required " cssclass } else { cssClass.Append( " required" ); } } if (!string.IsNullOrWhiteSpace(additionalCssClass)) { cssClass.Append( " " + additionalCssClass ); } writer.AddAttribute( HtmlTextWriterAttribute.Class, cssClass.ToString() ); writer.RenderBeginTag( HtmlTextWriterTag.Div ); if ( !( rockControl is RockLiteral ) ) { writer.AddAttribute( HtmlTextWriterAttribute.Class, "control-label" ); writer.AddAttribute( HtmlTextWriterAttribute.For, rockControl.ClientID ); } if ( rockControl is WebControl ) { // if the control has a Display Style, make sure the Label and Help also get the same Display style // For example, you might have rockControl.Style["Display"] = "none", so you probably want the label and help to also get not displayed var rockControlDisplayStyle = ( rockControl as WebControl ).Style[HtmlTextWriterStyle.Display]; if ( rockControlDisplayStyle != null ) { writer.AddStyleAttribute( HtmlTextWriterStyle.Display, rockControlDisplayStyle ); if (rockControl.HelpBlock != null) { rockControl.HelpBlock.Style[HtmlTextWriterStyle.Display] = rockControlDisplayStyle; } } } writer.RenderBeginTag( HtmlTextWriterTag.Label ); writer.Write( rockControl.Label ); if ( renderHelp ) { rockControl.HelpBlock.RenderControl( writer ); } writer.RenderEndTag(); } rockControl.RenderBaseControl( writer ); if ( !renderLabel && renderHelp ) { rockControl.HelpBlock.RenderControl( writer ); } if ( rockControl.RequiredFieldValidator != null ) { if ( rockControl.Required ) { rockControl.RequiredFieldValidator.Enabled = true; if ( string.IsNullOrWhiteSpace( rockControl.RequiredFieldValidator.ErrorMessage ) ) { rockControl.RequiredFieldValidator.ErrorMessage = rockControl.Label + " is Required."; } rockControl.RequiredFieldValidator.RenderControl( writer ); } else { rockControl.RequiredFieldValidator.Enabled = false; } } if ( renderLabel ) { writer.RenderEndTag(); } }
/// <summary> /// Renders the control which handles adding all the IRockControl common pieces (Label, Help, etc.). /// </summary> /// <param name="rockControl">The rock control.</param> /// <param name="writer">The writer.</param> /// <param name="additionalCssClass">The additional CSS class.</param> public static void RenderControl(IRockControl rockControl, HtmlTextWriter writer, string additionalCssClass = "") { bool renderLabel = (!string.IsNullOrEmpty(rockControl.Label)); bool renderHelp = (rockControl.HelpBlock != null && !string.IsNullOrWhiteSpace(rockControl.Help)); bool renderWarning = (rockControl.WarningBlock != null && !string.IsNullOrWhiteSpace(rockControl.Warning)); if (renderLabel) { var cssClass = new StringBuilder(); cssClass.AppendFormat("form-group {0} {1}", rockControl.GetType().Name.SplitCase().Replace(' ', '-').ToLower(), rockControl.FormGroupCssClass); if (((Control)rockControl).Page.IsPostBack && !rockControl.IsValid) { cssClass.Append(" has-error"); } if (rockControl.Required) { if ((rockControl is IDisplayRequiredIndicator) && !(rockControl as IDisplayRequiredIndicator).DisplayRequiredIndicator) { // if this is a rock control that implements IDisplayRequiredIndicator and DisplayRequiredIndicator is false, don't add the " required " cssclass } else { cssClass.Append(" required"); } } if (!string.IsNullOrWhiteSpace(additionalCssClass)) { cssClass.Append(" " + additionalCssClass); } writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass.ToString()); writer.RenderBeginTag(HtmlTextWriterTag.Div); if (!(rockControl is RockLiteral)) { writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-label"); writer.AddAttribute(HtmlTextWriterAttribute.For, rockControl.ClientID); } if (rockControl is WebControl) { // if the control has a Display Style, make sure the Label, Help, and Warning also get the same Display style // For example, you might have rockControl.Style["Display"] = "none", so you probably want the label, help, and warning to also get not displayed var rockControlDisplayStyle = (rockControl as WebControl).Style[HtmlTextWriterStyle.Display]; if (rockControlDisplayStyle != null) { writer.AddStyleAttribute(HtmlTextWriterStyle.Display, rockControlDisplayStyle); if (rockControl.HelpBlock != null) { rockControl.HelpBlock.Style[HtmlTextWriterStyle.Display] = rockControlDisplayStyle; } if (rockControl.WarningBlock != null) { rockControl.WarningBlock.Style[HtmlTextWriterStyle.Display] = rockControlDisplayStyle; } } } writer.RenderBeginTag(HtmlTextWriterTag.Label); writer.Write(rockControl.Label); if (renderHelp) { rockControl.HelpBlock.RenderControl(writer); } if (renderWarning) { rockControl.WarningBlock.RenderControl(writer); } writer.RenderEndTag(); if (rockControl is IRockControlAdditionalRendering) { ((IRockControlAdditionalRendering)rockControl).RenderAfterLabel(writer); } writer.Write(" "); // add space for inline forms, otherwise the label butts right up to the control writer.AddAttribute(HtmlTextWriterAttribute.Class, "control-wrapper"); writer.RenderBeginTag(HtmlTextWriterTag.Div); } rockControl.RenderBaseControl(writer); if (renderLabel) { writer.RenderEndTag(); } if (!renderLabel && renderHelp) { rockControl.HelpBlock.RenderControl(writer); } if (!renderLabel && renderWarning) { rockControl.WarningBlock.RenderControl(writer); } if (rockControl.RequiredFieldValidator != null) { if (rockControl.Required) { rockControl.RequiredFieldValidator.Enabled = true; if (string.IsNullOrWhiteSpace(rockControl.RequiredFieldValidator.ErrorMessage)) { rockControl.RequiredFieldValidator.ErrorMessage = rockControl.Label + " is Required."; } rockControl.RequiredFieldValidator.RenderControl(writer); } else { rockControl.RequiredFieldValidator.Enabled = false; } } if (renderLabel) { writer.RenderEndTag(); } }
/// <summary> /// Renders the control which handles adding all the IRockControl common pieces (Label, Help, etc.). /// </summary> /// <param name="rockControl">The rock control.</param> /// <param name="writer">The writer.</param> /// <param name="additionalCssClass">The additional CSS class.</param> public static void RenderControl( IRockControl rockControl, HtmlTextWriter writer, string additionalCssClass = "" ) { bool renderLabel = ( !string.IsNullOrEmpty( rockControl.Label ) ); bool renderHelp = ( rockControl.HelpBlock != null && !string.IsNullOrWhiteSpace( rockControl.Help ) ); if ( renderLabel ) { var cssClass = new StringBuilder(); cssClass.AppendFormat( "form-group {0}", rockControl.GetType().Name.SplitCase().Replace( ' ', '-' ).ToLower() ); if ( ( (Control)rockControl ).Page.IsPostBack && !rockControl.IsValid ) { cssClass.Append( " has-error" ); } if ( rockControl.Required ) { cssClass.Append( " required" ); } if (!string.IsNullOrWhiteSpace(additionalCssClass)) { cssClass.Append( additionalCssClass ); } writer.AddAttribute( HtmlTextWriterAttribute.Class, cssClass.ToString() ); writer.RenderBeginTag( HtmlTextWriterTag.Div ); if ( !( rockControl is RockLiteral ) ) { writer.AddAttribute( HtmlTextWriterAttribute.Class, "control-label" ); writer.AddAttribute( HtmlTextWriterAttribute.For, rockControl.ClientID ); } writer.RenderBeginTag( HtmlTextWriterTag.Label ); writer.Write( rockControl.Label ); if ( renderHelp ) { rockControl.HelpBlock.RenderControl( writer ); } writer.RenderEndTag(); } rockControl.RenderBaseControl( writer ); if ( !renderLabel && renderHelp ) { rockControl.HelpBlock.RenderControl( writer ); } if ( rockControl.RequiredFieldValidator != null ) { if ( rockControl.Required ) { rockControl.RequiredFieldValidator.Enabled = true; if ( string.IsNullOrWhiteSpace( rockControl.RequiredFieldValidator.ErrorMessage ) ) { rockControl.RequiredFieldValidator.ErrorMessage = rockControl.Label + " is Required."; } rockControl.RequiredFieldValidator.RenderControl( writer ); } else { rockControl.RequiredFieldValidator.Enabled = false; } } if ( renderLabel ) { writer.RenderEndTag(); } }