Rather then creating a Button I'd like to have an ImageButton. But TreeNode does not inherit from WebControl, so I don't know how to retrieve the ImageUrl from an image located in the dll as resource.
Something like
button.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(typeof(WebCustomControl1), "MyCompany.Web.Resources.edit.gif");
is not possible. Any suggestions? thx a lot
With the solution below you got to use CommandTreeView instead of TreeView as the control on the page.
public class CommandTreeView : TreeView
{
protected override TreeNode CreateNode()
{
//return base.CreateNode();
return new CommandTreeNode();
}
}
namespace MyCompany.Web.UI
{
[Flags]
public enum CommandButtonSet
{
None = 0,
Edit = 1,
Delete = 2,
MoveUp = 4,
MoveDown = 8
}
public class CommandTreeNode : TreeNode
{
private CommandButtonSet _commandButtonSet = CommandButtonSet.None;
private List _buttons = new List();
public CommandTreeNode()
{
}
public CommandTreeNode(String text, String value)
: base(text, value)
{
}
public CommandButtonSet CommandButtonSet
{
get { return _commandButtonSet; }
set { _commandButtonSet = value; }
}
protected override void RenderPreText(System.Web.UI.HtmlTextWriter writer)
{
base.RenderPreText(writer);
if ( (CommandButtonSet & CommandButtonSet.Edit) == CommandButtonSet.Edit )
_buttons.Add(GetButton("edit", Value, "edit.gif", "Edit"));
if ( (CommandButtonSet & CommandButtonSet.Delete) == CommandButtonSet.Delete )
_buttons.Add(GetButton("delete", Value, "delete.gif", "Delete"));
if ( (CommandButtonSet & CommandButtonSet.MoveDown) == CommandButtonSet.MoveDown )
_buttons.Add(GetButton("moveUp", Value, "moveUp.gif", "Move Up"));
if ( (CommandButtonSet & CommandButtonSet.MoveUp) == CommandButtonSet.MoveUp )
_buttons.Add(GetButton("moveDown", Value, "moveDown.gif", "Move Down"));
foreach ( WebControl control in _buttons )
{
control.RenderControl(writer);
writer.Write(" ");
}
}
private WebControl GetButton(string commandName, string commandArgument, string img, string alternateText)
{
Button button = new Button();
button.CommandName = commandName;
button.CommandArgument = commandArgument;
button.Text = alternateText;
return button;
}
protected override object SaveViewState()
{
Object[] state = new Object[2];
state[0] = base.SaveViewState();
state[1] = CommandButtonSet;
return state;
}
protected override void LoadViewState(object state)
{
if ( state != null )
{
Object[] savedState = (Object[])state;
CommandButtonSet = (CommandButtonSet)savedState[1];
base.LoadViewState(savedState[0]);
}
}
}
}