Newbie:Extending an Image Button

Discussion in 'ASP .Net Web Controls' started by MattC, Apr 27, 2004.

  1. MattC

    MattC Guest

    Hi,

    I'm trying to extend the image button webcontrol to allow it to have an
    on/off status rendering a different image for each state.

    Problem is when i drag it into the design view it just says error creating
    control.

    Here's my code so far for the control:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    namespace UIControls
    {
    /// <summary>
    /// Summary description for OnOffImageButton.
    /// </summary>
    [Designer("UIControls"),
    ToolboxData("<{0}:OnOffImageButton runat=server></{0}:OnOffImageButton>")]
    public class OnOffImageButton : System.Web.UI.WebControls.ImageButton
    {
    private bool status;
    private string onimageurl;
    private string savedimage;
    public OnOffImageButton(){}
    [Category("Appearance"),
    DefaultValue(""),
    Description("Image to be displayed before clicking and when clicking off")]
    public string OnImageURL
    {
    set
    {
    onimageurl = value;
    }
    get
    {
    return onimageurl;
    }
    }

    public bool Status
    {
    get
    {
    return status;
    }
    }

    public void Switch()
    {
    if(status)
    {
    TurnOff();
    }
    else
    {
    TurnOn();
    }

    status = !status;
    }

    public void TurnOff()
    {
    savedimage = base.ImageUrl;
    base.ImageUrl = onimageurl;
    }

    public void TurnOn()
    {
    base.ImageUrl = savedimage;
    }
    public bool IsButtonOn()
    {
    return status;
    }
    protected override void OnPreRender(EventArgs e)
    {
    this.EnableViewState = true;

    base.OnPreRender (e);
    }
    protected override void LoadViewState(object savedState)
    {
    onimageurl = (String)ViewState["onimageurl"];
    status = (bool)ViewState["status"];
    base.LoadViewState (savedState);
    }
    }
    /// <summary>
    /// Provides a moderate level of fidelity for the CategorizedCheckBoxList
    control in the VS.net IDE.
    /// </summary>
    internal class OnOffImageButtonControlDesigner :
    System.Web.UI.Design.ControlDesigner
    {
    /// <summary>
    /// Provides easy access the properties set in the IDE.
    /// </summary>
    protected OnOffImageButton ooib;
    /// <summary>
    /// Initializes the designer
    /// </summary>
    /// <param name="component"></param>
    public override void Initialize(IComponent component)
    {
    // Make sure that this designer is attached to a OnOffImageButton
    if(component is OnOffImageButton)
    {
    base.Initialize (component);
    ooib = (OnOffImageButton)component;
    }
    }
    }
    }
     
    MattC, Apr 27, 2004
    #1
    1. Advertising

  2. Hi Matt,

    Move the mouse over the error and you should see a tooltip with a detailed
    error text. Please post that.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "MattC" <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to extend the image button webcontrol to allow it to have an
    > on/off status rendering a different image for each state.
    >
    > Problem is when i drag it into the design view it just says error creating
    > control.
    >
    > Here's my code so far for the control:
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > namespace UIControls
    > {
    > /// <summary>
    > /// Summary description for OnOffImageButton.
    > /// </summary>
    > [Designer("UIControls"),
    > ToolboxData("<{0}:OnOffImageButton runat=server></{0}:OnOffImageButton>")]
    > public class OnOffImageButton : System.Web.UI.WebControls.ImageButton
    > {
    > private bool status;
    > private string onimageurl;
    > private string savedimage;
    > public OnOffImageButton(){}
    > [Category("Appearance"),
    > DefaultValue(""),
    > Description("Image to be displayed before clicking and when clicking

    off")]
    > public string OnImageURL
    > {
    > set
    > {
    > onimageurl = value;
    > }
    > get
    > {
    > return onimageurl;
    > }
    > }
    >
    > public bool Status
    > {
    > get
    > {
    > return status;
    > }
    > }
    >
    > public void Switch()
    > {
    > if(status)
    > {
    > TurnOff();
    > }
    > else
    > {
    > TurnOn();
    > }
    >
    > status = !status;
    > }
    >
    > public void TurnOff()
    > {
    > savedimage = base.ImageUrl;
    > base.ImageUrl = onimageurl;
    > }
    >
    > public void TurnOn()
    > {
    > base.ImageUrl = savedimage;
    > }
    > public bool IsButtonOn()
    > {
    > return status;
    > }
    > protected override void OnPreRender(EventArgs e)
    > {
    > this.EnableViewState = true;
    >
    > base.OnPreRender (e);
    > }
    > protected override void LoadViewState(object savedState)
    > {
    > onimageurl = (String)ViewState["onimageurl"];
    > status = (bool)ViewState["status"];
    > base.LoadViewState (savedState);
    > }
    > }
    > /// <summary>
    > /// Provides a moderate level of fidelity for the CategorizedCheckBoxList
    > control in the VS.net IDE.
    > /// </summary>
    > internal class OnOffImageButtonControlDesigner :
    > System.Web.UI.Design.ControlDesigner
    > {
    > /// <summary>
    > /// Provides easy access the properties set in the IDE.
    > /// </summary>
    > protected OnOffImageButton ooib;
    > /// <summary>
    > /// Initializes the designer
    > /// </summary>
    > /// <param name="component"></param>
    > public override void Initialize(IComponent component)
    > {
    > // Make sure that this designer is attached to a OnOffImageButton
    > if(component is OnOffImageButton)
    > {
    > base.Initialize (component);
    > ooib = (OnOffImageButton)component;
    > }
    > }
    > }
    > }
    >
    >
     
    Victor Garcia Aprea [MVP], Apr 28, 2004
    #2
    1. Advertising

  3. MattC

    David Jessee Guest

    It looks like the error might be occurring in the LoadViewState.

    If you want to manually place and retrieve your values in the viewstate,
    you'll want to make sure that you place them in the viewstate, as well as
    read them (you're reading them, but I don't see where they're plaed into the
    viewstate) A good shortcut for managing your state is to store your values
    in the viewstate when you're using your Get's and Set's, as opposed to using
    private members. I've never had a problem come up with that.

    public string OnImageURL
    {
    set
    {
    Viewstate("onimageurl")= value;
    }
    get
    {
    return
    Viewstate("onimageurl")==null?"";(String)Viewstate("onimageurl");
    }
    }


    Side note, on your OnImageURL Property, try adding the following attribute:

    System.ComponentModel.EditorAttribute("System.Web.UI.Design.ImageUrlEditor,
    System.Design, Version=1.0.3300.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a",
    System.Drawing.Design.UITypeEditor,System.Drawing, Version=1.0.3300.0,
    Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)

    It'll make the URL selector show up (Makes ya look Extra Smart ;-)



    "MattC" <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to extend the image button webcontrol to allow it to have an
    > on/off status rendering a different image for each state.
    >
    > Problem is when i drag it into the design view it just says error creating
    > control.
    >
    > Here's my code so far for the control:
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > namespace UIControls
    > {
    > /// <summary>
    > /// Summary description for OnOffImageButton.
    > /// </summary>
    > [Designer("UIControls"),
    > ToolboxData("<{0}:OnOffImageButton runat=server></{0}:OnOffImageButton>")]
    > public class OnOffImageButton : System.Web.UI.WebControls.ImageButton
    > {
    > private bool status;
    > private string onimageurl;
    > private string savedimage;
    > public OnOffImageButton(){}
    > [Category("Appearance"),
    > DefaultValue(""),
    > Description("Image to be displayed before clicking and when clicking

    off")]
    > public string OnImageURL
    > {
    > set
    > {
    > onimageurl = value;
    > }
    > get
    > {
    > return onimageurl;
    > }
    > }
    >
    > public bool Status
    > {
    > get
    > {
    > return status;
    > }
    > }
    >
    > public void Switch()
    > {
    > if(status)
    > {
    > TurnOff();
    > }
    > else
    > {
    > TurnOn();
    > }
    >
    > status = !status;
    > }
    >
    > public void TurnOff()
    > {
    > savedimage = base.ImageUrl;
    > base.ImageUrl = onimageurl;
    > }
    >
    > public void TurnOn()
    > {
    > base.ImageUrl = savedimage;
    > }
    > public bool IsButtonOn()
    > {
    > return status;
    > }
    > protected override void OnPreRender(EventArgs e)
    > {
    > this.EnableViewState = true;
    >
    > base.OnPreRender (e);
    > }
    > protected override void LoadViewState(object savedState)
    > {
    > onimageurl = (String)ViewState["onimageurl"];
    > status = (bool)ViewState["status"];
    > base.LoadViewState (savedState);
    > }
    > }
    > /// <summary>
    > /// Provides a moderate level of fidelity for the CategorizedCheckBoxList
    > control in the VS.net IDE.
    > /// </summary>
    > internal class OnOffImageButtonControlDesigner :
    > System.Web.UI.Design.ControlDesigner
    > {
    > /// <summary>
    > /// Provides easy access the properties set in the IDE.
    > /// </summary>
    > protected OnOffImageButton ooib;
    > /// <summary>
    > /// Initializes the designer
    > /// </summary>
    > /// <param name="component"></param>
    > public override void Initialize(IComponent component)
    > {
    > // Make sure that this designer is attached to a OnOffImageButton
    > if(component is OnOffImageButton)
    > {
    > base.Initialize (component);
    > ooib = (OnOffImageButton)component;
    > }
    > }
    > }
    > }
    >
    >
     
    David Jessee, Apr 28, 2004
    #3
  4. MattC

    MattC Guest

    David,

    Thanks, yeah I wasn't using the SaveState to save my extended properties.
    That sorted that, how do i use the code you suggested? Like below, hmmm :s

    [Category("Appearance"),
    DefaultValue(""),
    Description("Image to display when button is clicked"),
    Browsable(true),

    System.ComponentModel.EditorAttribute("System.Web.UI.Design.ImageUrlEditor,
    System.Design, Version=1.0.3300.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a",
    System.Drawing.Design.UITypeEditor,System.Drawing, Version=1.0.3300.0,
    Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)]
    public string ButtonOnImageURL
    {
    set
    {
    onimageurl = value;
    }

    get
    {
    return onimageurl;
    }
    }

    MattC
    "David Jessee" <> wrote in message
    news:O$...
    > It looks like the error might be occurring in the LoadViewState.
    >
    > If you want to manually place and retrieve your values in the viewstate,
    > you'll want to make sure that you place them in the viewstate, as well as
    > read them (you're reading them, but I don't see where they're plaed into

    the
    > viewstate) A good shortcut for managing your state is to store your values
    > in the viewstate when you're using your Get's and Set's, as opposed to

    using
    > private members. I've never had a problem come up with that.
    >
    > public string OnImageURL
    > {
    > set
    > {
    > Viewstate("onimageurl")= value;
    > }
    > get
    > {
    > return
    > Viewstate("onimageurl")==null?"";(String)Viewstate("onimageurl");
    > }
    > }
    >
    >
    > Side note, on your OnImageURL Property, try adding the following

    attribute:
    >
    >

    System.ComponentModel.EditorAttribute("System.Web.UI.Design.ImageUrlEditor,
    > System.Design, Version=1.0.3300.0, Culture=neutral,
    > PublicKeyToken=b03f5f7f11d50a3a",
    > System.Drawing.Design.UITypeEditor,System.Drawing, Version=1.0.3300.0,
    > Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
    >
    > It'll make the URL selector show up (Makes ya look Extra Smart ;-)
    >
    >
    >
    > "MattC" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I'm trying to extend the image button webcontrol to allow it to have an
    > > on/off status rendering a different image for each state.
    > >
    > > Problem is when i drag it into the design view it just says error

    creating
    > > control.
    > >
    > > Here's my code so far for the control:
    > >
    > > using System;
    > > using System.Web.UI;
    > > using System.Web.UI.WebControls;
    > > using System.ComponentModel;
    > > namespace UIControls
    > > {
    > > /// <summary>
    > > /// Summary description for OnOffImageButton.
    > > /// </summary>
    > > [Designer("UIControls"),
    > > ToolboxData("<{0}:OnOffImageButton

    runat=server></{0}:OnOffImageButton>")]
    > > public class OnOffImageButton : System.Web.UI.WebControls.ImageButton
    > > {
    > > private bool status;
    > > private string onimageurl;
    > > private string savedimage;
    > > public OnOffImageButton(){}
    > > [Category("Appearance"),
    > > DefaultValue(""),
    > > Description("Image to be displayed before clicking and when clicking

    > off")]
    > > public string OnImageURL
    > > {
    > > set
    > > {
    > > onimageurl = value;
    > > }
    > > get
    > > {
    > > return onimageurl;
    > > }
    > > }
    > >
    > > public bool Status
    > > {
    > > get
    > > {
    > > return status;
    > > }
    > > }
    > >
    > > public void Switch()
    > > {
    > > if(status)
    > > {
    > > TurnOff();
    > > }
    > > else
    > > {
    > > TurnOn();
    > > }
    > >
    > > status = !status;
    > > }
    > >
    > > public void TurnOff()
    > > {
    > > savedimage = base.ImageUrl;
    > > base.ImageUrl = onimageurl;
    > > }
    > >
    > > public void TurnOn()
    > > {
    > > base.ImageUrl = savedimage;
    > > }
    > > public bool IsButtonOn()
    > > {
    > > return status;
    > > }
    > > protected override void OnPreRender(EventArgs e)
    > > {
    > > this.EnableViewState = true;
    > >
    > > base.OnPreRender (e);
    > > }
    > > protected override void LoadViewState(object savedState)
    > > {
    > > onimageurl = (String)ViewState["onimageurl"];
    > > status = (bool)ViewState["status"];
    > > base.LoadViewState (savedState);
    > > }
    > > }
    > > /// <summary>
    > > /// Provides a moderate level of fidelity for the

    CategorizedCheckBoxList
    > > control in the VS.net IDE.
    > > /// </summary>
    > > internal class OnOffImageButtonControlDesigner :
    > > System.Web.UI.Design.ControlDesigner
    > > {
    > > /// <summary>
    > > /// Provides easy access the properties set in the IDE.
    > > /// </summary>
    > > protected OnOffImageButton ooib;
    > > /// <summary>
    > > /// Initializes the designer
    > > /// </summary>
    > > /// <param name="component"></param>
    > > public override void Initialize(IComponent component)
    > > {
    > > // Make sure that this designer is attached to a OnOffImageButton
    > > if(component is OnOffImageButton)
    > > {
    > > base.Initialize (component);
    > > ooib = (OnOffImageButton)component;
    > > }
    > > }
    > > }
    > > }
    > >
    > >

    >
    >
     
    MattC, Apr 28, 2004
    #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. Purvi T
    Replies:
    0
    Views:
    935
    Purvi T
    Oct 19, 2004
  2. Russell
    Replies:
    3
    Views:
    6,972
    Lau Lei Cheong
    Jun 24, 2004
  3. windandwaves
    Replies:
    0
    Views:
    454
    windandwaves
    Oct 19, 2006
  4. Manuel Trunk

    Extending datagrid with new/insert button

    Manuel Trunk, Nov 17, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    162
    Manuel Trunk
    Nov 18, 2004
  5. Manuel Trunk

    DataGrid extending with new/insert button

    Manuel Trunk, Nov 17, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    117
    Wolfgang Schwedhelm
    Nov 18, 2004
Loading...

Share This Page