Custom control doesn't fire validation

Posted by Ed Woodcock on Stack Overflow See other posts from Stack Overflow or by Ed Woodcock
Published on 2010-06-04T10:22:54Z Indexed on 2010/06/15 17:02 UTC
Read the original article Hit count: 299

Filed under:
|
|

I've written a custom ASP.net control that descends from LinkButton and overrides the Render() method. I'm using it to replace ImageButtons in the site I'm working on so we don't have to have an image for each button.

This control works fine, does the required post-backs etc., however it doesn't fire the validators in its validation group. This is obviously an issue.

The code for the control is (condensed) as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class CustomButton : LinkButton
{
    public string SpanCssClass { get; set; }
    protected override void Render(HtmlTextWriter writer)
    {
        if (!Visible)
        {
            return;
        }

        writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
        writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
        string postback = string.IsNullOrEmpty(OnClientClick) ? "javascript:__doPostBack('" + UniqueID + "','');" : OnClientClick;
        writer.AddAttribute(HtmlTextWriterAttribute.Href, postback);
        writer.RenderBeginTag(HtmlTextWriterTag.A);

        writer.AddAttribute(HtmlTextWriterAttribute.Class, SpanCssClass);
        writer.RenderBeginTag(HtmlTextWriterTag.Span);

        writer.Write(Text);

        writer.RenderEndTag();
        writer.RenderEndTag();
    }
}

Does anyone know why this would not be causing the validators to fire?

I was under the impression that leaving all the other methods in LinkButton un-overridden would leave all the other functionality the same!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET