Newbie:Extending an Image Button

M

MattC

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;
}
}
}
}
 
V

Victor Garcia Aprea [MVP]

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
 
D

David Jessee

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 said:
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;
}
}
}
}
 
M

MattC

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 said:
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 said:
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> said:
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;
}
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top