Hi David,
Your code looks great. Thank you for sharing your code.
From your another post, it seems you're experiencing rendering errors at
design-time. The reason of this is because the default designer associated
with Hyperlink control (or other descendants that are inherited from
hyperlink that don't have specified other designers) will use the Render
method to render the html for design-time. Since we don't have HttpContext
at design-time, the code throws exceptions and the designer will notify you
that it cannot render the control at design-time.
Also, I think displaying the 16x16 image at design time would be
sufficient. This can be done by creating a custom designer for your control.
By the way, you don't have to manually construct the html in Render(). From
your code, it seems you're wrapping an image tag inside a hyperlink tag,
which can be represented a hyperlink control and an image control.
I've put together following code for your reference. Note that I've written
several methods as "protected virtual" which might be useful when your user
wants to inherit from your control and provide custom implementations for
those methods.
namespace net.windward.webcontrols
{
[AspNetHostingPermission(SecurityAction.Demand, Level =
AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level =
AspNetHostingPermissionLevel.Minimal),
DefaultProperty("File"),
ToolboxData("<{0}:HelpLink runat=\"server\"/>"),
Designer(typeof(HelpLinkDesigner))
]
public class HelpLink : HyperLink
{
private const string bitmap = "images/help.gif";
private const string bitmapPixels = "16";
private const string helpDir = "help/";
private const string resourceFile = "windward";
private const string resourceName = "Help_Tooltip";
public virtual string File
{
get
{
string s = (string)ViewState["File"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["File"] = value;
}
}
public virtual string Bookmark
{
get
{
string s = (string)ViewState["Bookmark"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["Bookmark"] = value;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
Controls.Add(GetHelpImage());
}
protected virtual Control GetHelpImage()
{
Image img = new Image();
string text = GetHelpTooltip();
img.AlternateText = text;
img.Width = Unit.Pixel(16);
img.Height = Unit.Pixel(16);
img.Attributes.Add("title", text);
img.BorderWidth = Unit.Pixel(0);
return img;
}
public virtual string GetDesignTimeHtml()
{
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
GetHelpImage().RenderControl(writer);
return sw.ToString();
}
protected virtual string GetHelpFile()
{
if (Context == null)
{
return "about:blank";
}
// find the correct htm file
string locale = CultureInfo.CurrentUICulture.IetfLanguageTag;
string root = Context.Request.PhysicalApplicationPath + helpDir
+ File;
string filename = root + "-" + locale + ".htm";
while ((locale.Length > 0) &&
(!System.IO.File.Exists(filename)))
{
int pos = locale.LastIndexOf('-');
if (pos > 0)
{
locale = locale.Substring(0, pos);
filename = root + "-" + locale + ".htm";
}
else
{
locale = string.Empty;
filename = root + ".htm";
}
}
// now we need the application path (for the url)
filename = File + (locale.Length > 0 ? "-" + locale : "") +
".htm";
return filename;
}
protected virtual string GetHelpTooltip()
{
return HttpContext.GetGlobalResourceObject(resourceFile,
resourceName) as string;
}
protected override void OnPreRender(EventArgs e)
{
this.Attributes.Add("title", GetHelpTooltip());
this.NavigateUrl = GetHelpFile();
this.Target = "_blank";
base.OnPreRender(e);
}
}
public class HelpLinkDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
HelpLink hl = (HelpLink)this.Component;
return hl.GetDesignTimeHtml();
}
public override bool AllowResize
{
get
{
return false;
}
}
}
}
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.