Rendered Control, problem med event-raising.
Hej Eksperter,Jeg er ved at lave en control, der nedarver fra WebControl.
Jeg har fået den til at rendere fint ud som den nu skal. Nu sidder jeg og bakser med at få den til at lave PostBack.
Jeg har ladet klassen implementere
IPostBackEventHandler,
har erklæret et event
public event EventHandler Click,
har implemeneteret metoderne
public void RaisePostBackEvent(string args) og
protected virtual void OnClick(EventArgs e)
Hvorfor er det, at der ikke sker et postback når jeg tester kontrollen i en side?
Den samlede kode er her:
[ToolboxData("<{0}:RichLinkButton runat=\"server\"> </{0}:RichLinkButton>"), DefaultProperty("Text")]
public class RichLinkButton : WebControl, IPostBackEventHandler
{
public event EventHandler Click;
[Bindable(false), Browsable(true), DefaultValue("RichLinkButton")]
public string Text
{
get
{
if (ViewState["text"] != null)
{
return ViewState["text"].ToString();
}
else { return string.Empty; }
}
set { ViewState["text"] = value; }
}
[Bindable(false), Browsable(true), DefaultValue("steel")]
public string Theme
{
get
{
if (ViewState["theme"] != null)
{
return ViewState["theme"].ToString();
}
else { return string.Empty; }
}
set {ViewState["theme"] = value; }
}
protected override void Render(HtmlTextWriter writer)
{
string path = "/App_Themes/" + Theme + "/button";
string id = this.ClientID;
writer.BeginRender();
//Tilføj attributter
writer.AddAttribute(HtmlTextWriterAttribute.Id, base.ID);
writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, "", true));
writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, "Verdana");
writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "10px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");
writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, "none");
writer.AddStyleAttribute(HtmlTextWriterStyle.Color, GetLinkStyle(false));
writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "hand");
//Tilføj javascript onmouseover, onmouseout
writer.AddAttribute("onmouseover", "this.style.color='" + GetLinkStyle(true) + "';document.getElementById('midt_" + id + "').style.backgroundImage='url(" + path + "/midt_h.gif)'; getElementById('start_" + id + "').src='" + path + "/start_h.gif'; getElementById('slut_" + id + "').src='" + path + "/slut_h.gif';");
writer.AddAttribute("onmouseout", "this.style.color='" + GetLinkStyle(false) + "';document.getElementById('midt_" + id + "').style.backgroundImage='url(" + path + "/midt.gif)'; getElementById('start_" + id + "').src='" + path + "/start.gif'; getElementById('slut_" + id + "').src='" + path + "/slut.gif';");
writer.RenderBeginTag(HtmlTextWriterTag.A);
RenderContents(writer);
//Afslut <a>
writer.RenderEndTag();
writer.EndRender();
writer.Flush();
}
protected override void RenderContents(HtmlTextWriter writer)
{
string path = "/App_Themes/" + Theme + "/button";
string id = this.ClientID;
//Skriv tabellen inden i a tagget
writer.Write("<TABLE border=0 cellspacing=0 cellpadding=0><TR>");
//Første celle
writer.Write("<TD><img id='start_" + id + "' src='" + path + "/start.gif'></TD>");
//Midterste celle - den med teksten
writer.Write("<TD id=midt_" + id + " valign=middle align=right width=92 height=17 style='background-image: url(" + path + "/midt.gif); padding-right:5px' noWrap>" + Text + "</TD>");
//Sidste celle
writer.Write("<TD><img id='slut_" + id + "' src='" + path + "/slut.gif'></TD></TR></TABLE>");
}
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
private string GetLinkStyle(bool hover)
{
string style = "";
if (Theme != string.Empty)
{
if (hover)
{
switch (Theme)
{
default: style = "black";
break;
case "graphite": style = "black";
break;
}
}
else
{
switch (Theme)
{
default: style = "black";
break;
case "graphite": style = "white";
break;
}
}
}
return style;
}
public void RaisePostBackEvent(string args)
{
OnClick(EventArgs.Empty);
}
}
}
PS: Når jeg set på det mark-up, som bliver genereret ligner det til forveksling en linkbutton (hvilket er meningen), men som sagt sker der ikke noget postback.