SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    set a button visible/invisible from a class

    Hi Everybody.

    I am newbie...sorry if this is a very basic question.

    I have a web site with 20 web pages ,

    1) Each page has one has a Standard button bar with 5 buttons ( ADD, EDIT, DELETE, VIEW , EXIT), buttons

    will be visible or invisible depending on the operations being done.

    The above operation is universal for all the pages.

    How do i write a class with methods to make the above operation universal? I want to put this class in app_code directory .


    I Tried to put classes in app_code. but unable to hide show buttons of the individual page.I get a message

    "Object reference not set to instance of object". My team is now learning the fundamentals oops. so we do not know how to proceed!!!

    So my question is

    1) how do i make the universal method recognize the button of an invidual page (which called the method) and make it Visible/invisible ?


    One of my friend asked me to try.....

    Public Sub myGlobalSub(pge As Page, display As Boolean)
    pge.FindControl("btnAdd").Visible = display
    End Sub
    --------------------------------------------------------------------------------

    and by calling it

    VB:
    --------------------------------------------------------------------------------
    myGlobalSub(Me, True) 'Turns it on
    myGlobalSub(Me, False)'Turns it off
    --------------------------------------------------------------------------------

    But I still get error ...Object reference not set to instance of object


    Please help me -- Thank you
    Sara

  2. #2
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,466
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I would recommend making the Standard button Bar a user control and manage the visibility through that rather than having the same bar in several pages.

    Something to keep in mind when creating classes and OOP in general is a class should only have to know about itself. You shouldn't be passing a Page to it.
    Sara

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    And, if it is a user control, it will be aware of the Page object once things are initialized.

    As for how to do this, I would:

    1) Create an interface definition for my button bar. This would be the public api to let pages do things like show/hide buttons. Lets call this IButtonBar.

    2) Create an interface definition called IButtonBarContainer which has a single property: ButtonBar.

    3) Create a base page class with an IButtonBar property. It should return the Master property cast as IButtonBarContainer (eg: get { return (IButtonBarContainer)Master; })

    4) Make your site's Master page implement IButtonBarContainer, and place your ButtonBar control on the master page.

    5) Set your site's page base class to your page base class.

    6) Refernce the button bar via the page.

    This sounds a bit complex, but it is pretty simple once you do it once or twice. The reason to handle it this way is you decouple the different pieces while maintaining a strongly-typed interface.

    [PS: Anyone thing a good walk-thru of this would make a neato blog post?]

  4. #4
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by wwb_99
    [PS: Anyone thing a good walk-thru of this would make a neato blog post?]

  5. #5
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,466
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by wwb_99

    [PS: Anyone thing a good walk-thru of this would make a neato blog post?]
    Yes please.
    Sara

  6. #6
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99
    [PS: Anyone thing a good walk-thru of this would make a neato blog post?]
    Oh, yes!!


    My sujection, to the question posted

    public interface IButtonBar
    {
    event EventHandler Add;
    event EventHandler Edit;
    event EventHandler Delete;
    event EventHandler Exit;
    ButtonBarDisplayMode DisplayMode { get; set;};
    }

    public enum ButtonBarDisplayMode
    {
    View,
    Edit,
    // Anyother display mode name that you may need for the ButtonBar.
    }

    cheers,
    rui
    Ciao, Rui...

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Blog post is up.

    I hope you don't mind Pufa, but I did borrow a bit of your interface.

  8. #8
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99
    Blog post is up.

    I hope you don't mind Pufa, but I did borrow a bit of your interface.
    Great!! I don't mind, I did borrow yours to!
    Ciao, Rui...

  9. #9
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    No worries.

    BTW, main reason I ditched the enum is that it was easier to just map from boolean values to the Button's Enabled property. I happen to like the Enum better, but it would have ended up being a demo on how to use enums as flags, etc.

  10. #10
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99
    No worries.

    BTW, main reason I ditched the enum is that it was easier to just map from boolean values to the Button's Enabled property. I happen to like the Enum better, but it would have ended up being a demo on how to use enums as flags, etc.
    I agree with you. The enum makes the interface more easy to use but they are a "pain" to implement. Usually involves "ugly" conditional or case staments, and I end up spending sometime scratching my head trying to figure a way to make it more clean.

    I started playing around more with this Interface "thing", in asp.net projects after following tips in your blog post and the MVP Pattern. So, many thanks to you for pointing us in the right direction.

    In many of the links I followed from your "Recomend reading" blog post, and also on your MasterPage/ButtonBar blog post, I happen to notice that almost none of them used the EventHandlerList property (Events) of the Control class to implement events and the optimization it provides.

    Is it just laziness!?

    cheers,
    Rui
    Ciao, Rui...

  11. #11
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Good question. Quite frankly I have never even heard of the EventListHandler until this post. How/when does one use it?

  12. #12
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The property is name Events and its of type EventListHandler. Its a protected property of the Control class.

    The goal with the Events property is optimize the definition of events in classes that have lots of events and its use is only available to C# developers.

    http://msdn2.microsoft.com/en-us/lib...ol.events.aspx

    for each event that you declare, like

    public event EventHandler Save;

    The complier creates a private delegate for it and the add and remove methods are marked as sychronized and acquire locks every time you access them.

    Using the Events property, and the C# event property constructor, the delegates are "combined" with each other and the there is no private delegate. Also the add and remove methods are not automaticaly marked as synchronized and don't acquire locks.

    The only place where I've seen this explained is in this book.
    Developing Microsoft ASP.NET Server Controls and Components

    cheers,
    rui
    Attached Images Attached Images
    Ciao, Rui...

  13. #13
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was looking in MDSN and apparently you can do something similar to the C# events property constructor in VB 2005.

    How to: Declare Events That Conserve Memory Use

    How to: Declare Events That Avoid Blocking

    edited: one more

    How to: Handle Multiple Events Using Event Properties


    cheers
    rui
    Ciao, Rui...

  14. #14
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Friends

    Thanks for the time and replies.

    Initially every thing flew just above my head :-) .....then I studied a bit of basics........so could understand your messages a bit better.

    your replies were very helpful
    Thanks
    Sara


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •