Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2007, 08:21 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Bug in the highlight method for form objects

Here's the code from Helpers.cs

Code:
      public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes)
      {
         foreach (Control ctl in container.Controls)
         {
            if ((onlyTextBoxes && ctl is TextBox) || 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);
            }
         }
      }


Now if you look at it, doesn't matter whether onlyTextBoxes is true or false, all form objects will get the onfocus and onblur actions. I have fixed the code to work correctly and here is the correct code:

Code:
    public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes) {
      foreach (Control ctl in container.Controls) {
        if ((!onlyTextBoxes && ( ctl is TextBox || ctl is DropDownList ||
          ctl is ListBox || ctl is CheckBox || ctl is RadioButton ||
          ctl is RadioButtonList || ctl is CheckBoxList)) || (onlyTextBoxes && ctl is TextBox)) {
          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);
        }
      }
    }


Hope it helps someone :) I actually found out also that IE7 does not render onfocus for checkboxes but FF2 does render. I was testing both browsers and found out the highlight on FF2. As I didn't want it I changed the boolean to true at BasePage.cs. But the highlight was still there. Then I investigated the code and now it's fixed to work correctly.

 
Old April 28th, 2009, 01:52 AM
Registered User
 
Join Date: Apr 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default it is what I was looking for...

Your code works perfect, tks!!!
 
Old April 28th, 2009, 10:53 AM
Authorized User
 
Join Date: Mar 2007
Posts: 39
Thanks: 0
Thanked 1 Time in 1 Post
Default

You may also be interested in the enhancements I added to the Highlight method. If an input control has an existing inline class it will be lost after the control's onfocus/onblur events are fired. The modified code below preserves the controls' original class during both onfocus & onblur events.

One other enhancement is to highlight DropDownLists using the 'onmousedown' event.

Code:
IfTypeOf (ctl) Is DropDownList Then
   'use "onmousedown" for dropdownlist ("onfocus" causes a postback) 
   wctl.Attributes.Add("onmousedown", String.Format("this.className = '{0}';", className))
Else
   'wctl.Attributes.Add("onfocus", String.Format("this.className = ' {0}';", className))
   wctl.Attributes.Add("onfocus", String.Format("this.className = '{0} ' + this.className;", className)) 'fix: preserves current class
EndIf
   wctl.Attributes.Add("onblur", String.Format("this.className = '{0}';", wctl.CssClass)) ' fix: restores original class
   'wctl.Attributes.Add("onblur", "this.className = '';") 
Hope this helps, Scott.
 
Old June 11th, 2009, 08:05 PM
Registered User
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default still struggling with this one

using it in IE7 and FF2 or any browser, I am not able to get any action on this at all.

I know I miss something, I am assuming I am supposed to set or make reference to the events somewhere other than in the helpers.cs and the basepage.cs files as indicated on page 44 and 45.

does anyone have any clue on the element I am missing?





Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable.Select() method bug jpichler C# 3 October 5th, 2009 12:24 PM
Highlight text using form radio aspless Javascript 9 June 25th, 2008 03:50 AM
XSLT php:function: call objects non static method polarbear Pro PHP 1 June 6th, 2008 05:39 PM
Highlight and Color records in a continuous form MichaelCree Access VBA 8 March 28th, 2005 05:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.