views:

120

answers:

2

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!

A: 

Hey,

Well, if you want to trigger validation manually if CausesValidation is true, you can call Page_Validate client-side method to trigger the validation. I think it takes one param, the validation group, to validate...

HTH.

Brian
No, I want it to be automatic: all I've done is change the render method to add a span inside the anchor so we can style them without having as much code, surely it should be trivial to have the validators fire as they would with the base LinkButton?
Ed Woodcock
Well, you want to get .NET Reflector and look in the framework to see what is going on; there is actually more complexity needed to handle this scenario... Since the default rendering logic doesn't come into play, you have to add them back in. The LinkButton uses the GetPostBackOptions method to return a collection of options (to generate the HREF) and then adds these to the HREF attribute in the AddAttributesToRender method (which you can put in render). HTH.
Brian
+1  A: 

Problem is you're generating your own script, allow asp.net to do it for you and it should work better. Something like this will work better.

PostBackOptions options = new PostBackOptions(this);
options.PerformValidation = true;
options.RequiresJavaScriptProtocol = true;

string postback = string.IsNullOrEmpty(OnClientClick) ? this.Page.ClientScript.GetPostBackEventReference(options) : OnClientClick;
internet man
Nearly correct. You missed that you need to add the ValidationGroup property to the options, but otherwise, nicely played!
Ed Woodcock