Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Newbie:Extending an Image Button

Reply
Thread Tools

Newbie:Extending an Image Button

 
 
MattC
Guest
Posts: n/a
 
      04-27-2004
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;
}
}
}
}


 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      04-28-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
> }
> }
> }
> }
>
>



 
Reply With Quote
 
 
 
 
David Jessee
Guest
Posts: n/a
 
      04-28-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
> }
> }
> }
> }
>
>



 
Reply With Quote
 
MattC
Guest
Posts: n/a
 
      04-28-2004
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" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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;
> > }
> > }
> > }
> > }
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
image button as a back button Chicagoboy27 ASP .Net 2 10-30-2006 05:00 PM
Changing a HTML submit button that requests a Servlet, to an image button? How? James Storey via JavaKB.com Java 7 04-08-2005 06:10 PM
wx.Image: Couldn't add an image to the image list. Laszlo Zsolt Nagy Python 1 01-26-2005 09:55 PM
image button click event fires before click event of button Purvi T ASP .Net 0 10-19-2004 06:19 AM
Image button acting like submit button. Russell ASP .Net 3 06-24-2004 01:55 AM



Advertisments