突出显示 ASP.NET 页面中的所有用户控件

发布于 2024-08-13 05:08:38 字数 269 浏览 6 评论 0原文

我想通过在页面周围添加边框来突出显示页面中使用的所有用户控件。我们这样做是为了方便调试。我可以重写用户控件基类中的 RenderControl 方法来轻松完成此操作,但我们有很多不使用基类的用户控件(遗留)。

然后我采取了不同的方法。我尝试在页面基类的PreRender方法中遍历页面控件,所有页面都会使用该方法,并为所有用户控件添加一个带边框的div。但我发现如果控件集合包含代码块(即 <% ... %>),则无法从控件集合中添加或删除。

有什么建议吗?

谢谢

I want to highlight all user controls used in a page by adding a border around it. We are doing this to facilitate debugging. I could override RenderControl method in user control base class to do that easily but we have lot of user controls(legacy) that are not using base class.

I then took a different approach. I tried traversing page controls in PreRender method of page base class, which is used by all pages, and add a div with border to all user controls. But I found that it is not possible to add or remove from control collection if it contains code blocks (i.e. <% ... %>).

Any suggestions?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2024-08-20 05:08:38

以下是如何突出显示具有焦点的活动输入控件。您需要处理输入控件的onfocusonblur客户端事件。并通过设置控件className<来应用或删除控件的CSS样式/em> 属性

将其添加到您的 css 文件中:

.highlight
{
  background-color: #fefbd2; /*highlight with yellow*/
  color: #000080;            /*make text blue*/
}

在您的 App_Code 目录中创建一个帮助器类,例如

public static class Helpers
{
  /// <summary>
  /// Adds the onfocus and onblur attributes to all input controls found in the specified parent,
  /// to change their apperance with the control has the focus
  /// </summary>
  public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes)
  {
   foreach (Control ctl in container.Controls)
   {
     if ((onlyTextBoxes && ctl is TextBox) ||
          (!onlyTextBoxes && (ctl is TextBox || ctl is DropDownList ||
          ctl is ListBox || ctl is CheckBox || ctl is RadioButton ||
          ctl is RadioButtonList || ctl is CheckBoxList))) 
     {
        WebControl wctl = ctl as WebControl;
        wctl.Attributes.Add("onfocus", string.Format("this.className = '{0}';", className));
        wctl.Attributes.Add("onblur", "this.className = '';");
     }
     else
     {
        if (ctl.Controls.Count > 0)
           SetInputControlsHighlight(ctl, className, onlyTextBoxes);
     }
   }
  }
}

然后覆盖任何页面的 OnLoad 方法。

protected override void OnLoad(EventArgs e)
{
  Helpers.SetInputControlsHighlight(this, "highlight", false);
  base.OnLoad(e);
}

Here is how you can highlight an active input control that has the focus. You need to handle the onfocus and onblur client-side events of the input controls.and apply or remove the css style to the control by setting the controls className attribute

Add this to your css file:

.highlight
{
  background-color: #fefbd2; /*highlight with yellow*/
  color: #000080;            /*make text blue*/
}

In your App_Code directory create a helper class like

public static class Helpers
{
  /// <summary>
  /// Adds the onfocus and onblur attributes to all input controls found in the specified parent,
  /// to change their apperance with the control has the focus
  /// </summary>
  public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes)
  {
   foreach (Control ctl in container.Controls)
   {
     if ((onlyTextBoxes && ctl is TextBox) ||
          (!onlyTextBoxes && (ctl is TextBox || ctl is DropDownList ||
          ctl is ListBox || ctl is CheckBox || ctl is RadioButton ||
          ctl is RadioButtonList || ctl is CheckBoxList))) 
     {
        WebControl wctl = ctl as WebControl;
        wctl.Attributes.Add("onfocus", string.Format("this.className = '{0}';", className));
        wctl.Attributes.Add("onblur", "this.className = '';");
     }
     else
     {
        if (ctl.Controls.Count > 0)
           SetInputControlsHighlight(ctl, className, onlyTextBoxes);
     }
   }
  }
}

Then just override the OnLoad method of any page.

protected override void OnLoad(EventArgs e)
{
  Helpers.SetInputControlsHighlight(this, "highlight", false);
  base.OnLoad(e);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文