Very Strange Problem! (Derived Controls) - Help!

Discussion in 'ASP .Net Building Controls' started by Sosh, Jan 13, 2006.

  1. Sosh

    Sosh Guest

    Hi,

    I'm pulling my hair out trying to work this out. Pehaps I am
    missunderstanding something - hopefully someone can shed some light on this:

    1) I have a class library that contains a bunch of custom web controls. Two
    of these controls are derived from the TextBox control, and overide the
    onLoad method in order to add some client side attributes, eg:

    namespace SomeControls{

    public class Control1 : System.Web.UI.WebControls.TextBox

    {
    public Control1 ()
    {
    }

    protected override void OnLoad(EventArgs e)
    {
    this.Attributes["onblur"] = "some javascript";
    base.OnLoad(e);
    }
    }

    }

    AND.........

    namespace SomeControls{

    public class Control2 : System.Web.UI.WebControls.TextBox

    {
    public Control2 ()
    {
    }

    protected override void OnLoad(EventArgs e)
    {
    this.Attributes["onfocus"] = "some other javascript";
    base.OnLoad(e);
    }
    }

    }

    2) I have a some user controls, that contain the custom controls, which are
    added Declaratively

    3) I Dynamically load one of the user controls into my page depending on
    certain factors.



    My problem is this: When the page is rendered, some of the custom controls
    are rendered incorrectly - they seem to be taking elements from each of the
    controls that derive from the TextBox control. For instance, a control that
    was declared as SomeControls:Control1 will be rendered with the attributes
    from BOTH SomeControls:Control1 and SomeControls:Control2. (eg both the
    onBlur and onFocus attributes) !!? To add to the strangeness, sometimes
    this happens, and sometimes this does not - If there are 10 instances of
    SomeControls:Control1 on the page, sometimes the top who will contain the
    elements from SomeControls:Control1 and SomeControls:Control2, while the
    rest will render correctly with just the SomeControls:Control1 code.

    Its like the two classes SomeControls:Control1 and SomeControls:Control2 are
    interfereing with each other somehow.

    I hope this makes some sense - it's pretty hard to explain, and personally I
    am loosing faith that I can figure out what is going on here. Can someone
    exlplain? Please!

    Thanks V much

    S




    I originally posted this to frameword.aspnet, but thought this group would
    be more appropriate.
    Sosh, Jan 13, 2006
    #1
    1. Advertising

  2. Sosh

    chris martin Guest

    > Hi,
    >
    > I'm pulling my hair out trying to work this out. Pehaps I am
    > missunderstanding something - hopefully someone can shed some light on
    > this:
    >
    > 1) I have a class library that contains a bunch of custom web
    > controls. Two of these controls are derived from the TextBox control,
    > and overide the onLoad method in order to add some client side
    > attributes, eg:
    >
    > namespace SomeControls{
    >
    > public class Control1 : System.Web.UI.WebControls.TextBox
    >
    > {
    > public Control1 ()
    > {
    > }
    > protected override void OnLoad(EventArgs e)
    > {
    > this.Attributes["onblur"] = "some javascript";
    > base.OnLoad(e);
    > }
    > }
    > }
    >
    > AND.........
    >
    > namespace SomeControls{
    >
    > public class Control2 : System.Web.UI.WebControls.TextBox
    >
    > {
    > public Control2 ()
    > {
    > }
    > protected override void OnLoad(EventArgs e)
    > {
    > this.Attributes["onfocus"] = "some other javascript";
    > base.OnLoad(e);
    > }
    > }
    > }
    >
    > 2) I have a some user controls, that contain the custom controls,
    > which are added Declaratively
    >
    > 3) I Dynamically load one of the user controls into my page depending
    > on certain factors.
    >
    > My problem is this: When the page is rendered, some of the custom
    > controls are rendered incorrectly - they seem to be taking elements
    > from each of the controls that derive from the TextBox control. For
    > instance, a control that was declared as SomeControls:Control1 will be
    > rendered with the attributes from BOTH SomeControls:Control1 and
    > SomeControls:Control2. (eg both the onBlur and onFocus attributes)
    > !!? To add to the strangeness, sometimes this happens, and sometimes
    > this does not - If there are 10 instances of SomeControls:Control1 on
    > the page, sometimes the top who will contain the elements from
    > SomeControls:Control1 and SomeControls:Control2, while the rest will
    > render correctly with just the SomeControls:Control1 code.
    >
    > Its like the two classes SomeControls:Control1 and
    > SomeControls:Control2 are interfereing with each other somehow.
    >
    > I hope this makes some sense - it's pretty hard to explain, and
    > personally I am loosing faith that I can figure out what is going on
    > here. Can someone exlplain? Please!
    >
    > Thanks V much
    >
    > S
    >
    > I originally posted this to frameword.aspnet, but thought this group
    > would be more appropriate.
    >


    Just a hunch. But, try calling base.OnLoad(e) before adding your attributes.
    You may need to manually remove unwanted attributes too.
    chris martin, Jan 13, 2006
    #2
    1. Advertising

  3. Sosh

    Sosh Guest

    "chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
    news:...
    >> Hi,
    >>
    >> I'm pulling my hair out trying to work this out. Pehaps I am
    >> missunderstanding something - hopefully someone can shed some light on
    >> this:
    >>
    >> 1) I have a class library that contains a bunch of custom web
    >> controls. Two of these controls are derived from the TextBox control,
    >> and overide the onLoad method in order to add some client side
    >> attributes, eg:
    >>
    >> namespace SomeControls{
    >>
    >> public class Control1 : System.Web.UI.WebControls.TextBox
    >>
    >> {
    >> public Control1 ()
    >> {
    >> }
    >> protected override void OnLoad(EventArgs e)
    >> {
    >> this.Attributes["onblur"] = "some javascript";
    >> base.OnLoad(e);
    >> }
    >> }
    >> }
    >>
    >> AND.........
    >>
    >> namespace SomeControls{
    >>
    >> public class Control2 : System.Web.UI.WebControls.TextBox
    >>
    >> {
    >> public Control2 ()
    >> {
    >> }
    >> protected override void OnLoad(EventArgs e)
    >> {
    >> this.Attributes["onfocus"] = "some other javascript";
    >> base.OnLoad(e);
    >> }
    >> }
    >> }
    >>
    >> 2) I have a some user controls, that contain the custom controls,
    >> which are added Declaratively
    >>
    >> 3) I Dynamically load one of the user controls into my page depending
    >> on certain factors.
    >>
    >> My problem is this: When the page is rendered, some of the custom
    >> controls are rendered incorrectly - they seem to be taking elements
    >> from each of the controls that derive from the TextBox control. For
    >> instance, a control that was declared as SomeControls:Control1 will be
    >> rendered with the attributes from BOTH SomeControls:Control1 and
    >> SomeControls:Control2. (eg both the onBlur and onFocus attributes)
    >> !!? To add to the strangeness, sometimes this happens, and sometimes
    >> this does not - If there are 10 instances of SomeControls:Control1 on
    >> the page, sometimes the top who will contain the elements from
    >> SomeControls:Control1 and SomeControls:Control2, while the rest will
    >> render correctly with just the SomeControls:Control1 code.
    >>
    >> Its like the two classes SomeControls:Control1 and
    >> SomeControls:Control2 are interfereing with each other somehow.
    >>
    >> I hope this makes some sense - it's pretty hard to explain, and
    >> personally I am loosing faith that I can figure out what is going on
    >> here. Can someone exlplain? Please!
    >>
    >> Thanks V much
    >>
    >> S
    >>
    >> I originally posted this to frameword.aspnet, but thought this group
    >> would be more appropriate.
    >>

    >
    > Just a hunch. But, try calling base.OnLoad(e) before adding your
    > attributes. You may need to manually remove unwanted attributes too.
    >
    >


    Thanks chris, I'll try that. Why do you think I should have to manually
    remove attributes that are not added in that class though?
    Sosh, Jan 23, 2006
    #3
  4. Sosh

    Sosh Guest

    Thanks very much fluxtah - moving the attribute additions to that method
    seems to have made the problem go away. Am still very interested to know
    what was going on before thought!

    "fluxtah" <> wrote in message
    news:...
    >
    > Hiya, new guy here :)
    >
    > The best way to add attributes to a derived control is to override
    > AddAttributesToRender method.
    >
    > protected override void AddAttributesToRender(HtmlTextWriter writer)
    > {
    > base.AddAttributesToRender(writer);
    >
    > writer.AddAttribute("onblur", "some javascript");
    > }
    >
    >
    >
    > --
    > fluxtah
    > ------------------------------------------------------------------------
    > Posted via http://www.codecomments.com
    > ------------------------------------------------------------------------
    >
    Sosh, Jan 31, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sosh
    Replies:
    1
    Views:
    315
    Karl Seguin [MVP]
    Jan 13, 2006
  2. Replies:
    1
    Views:
    384
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    372
    Victor Bazarov
    May 23, 2007
  4. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,247
  5. Sosh

    Very Strange Problem! (Derived Controls) - Help!

    Sosh, Jan 13, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    90
Loading...

Share This Page