Control Adapter For Button

Last post 07-14-2007 1:04 PM by neilredfern. 2 replies.

Sort Posts:

  • Control Adapter For Button

    07-14-2007, 6:32 AM
    • Member
      63 point Member
    • neilredfern
    • Member since 09-10-2002, 6:53 AM
    • Posts 46

    Hi,

    I'm trying to ensure that when every a asp:button is placed on a page it renders both an onclick and onkeypress JavaScript event with the same signiture (due to my site currently failing the http://webxact.watchfire.com/ due to the lack of the onkeypress event).  So does any one have any idea of a way to do this using Control Adapter's or another solution?

    Thanks

    Neil
     

  • Re: Control Adapter For Button

    07-14-2007, 11:57 AM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    What you are trying to do is similar to an earlier posting wherein someone wanted to build a very simple adapter for <asp:Image> to slightly tweak the attributes that are rendered by the ASP.NET without the framework. To accomplish that, I suggested some code in this posting http://forums.asp.net/p/1058023/1592877.aspx. Look about midway down that thread for the entry made at 01-02-2007, 10:20 AM.

    Obviously you don't want to have an adapter for Image. You want one for Button. But the concepts are exactly the same. I imagine that you'll find it pretty easy to simply use Button in most places in that example code where it uses Image.

    The Image adapter uses a regular expression to massage the HTML that is normally rendered by the framework. You may want to use that technique, too. Or, you may find that in your case you can do some other sort of string manipulation to add the JavaScript event handlers to the <input> tag before having the adapter spit it out.

    However you do it... if you get it done, please consider posting your results here so others, in the future, can use them too. Thanks. Best of luck and best regards,

    Russ Helfand
    Groovybits.com
  • Re: Control Adapter For Button

    07-14-2007, 1:04 PM
    Answer
    • Member
      63 point Member
    • neilredfern
    • Member since 09-10-2002, 6:53 AM
    • Posts 46

     Russ,

    Thanks for the pointer that works great please find my code below:

     

    using System.IO;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls.Adapters;
    
    namespace CSSFriendly
    {
        public class ButtonControlAdapter : WebControlAdapter
        {
            protected override void BeginRender(HtmlTextWriter writer)
            {
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                base.RenderBeginTag(hw);
                hw.Flush();
                hw.Close();
                string origTag = sw.ToString();
                Match match =
                    Regex.Match(origTag, "\\s*onclick=[\"][^\"]*[\"]", 
                                  RegexOptions.Multiline | RegexOptions.IgnoreCase);
    
                if(match.Success)
                {
                    string temp = match.Value.Replace("onclick", "onkeypress");
    
                    origTag = origTag.Insert(match.Index, temp);
                }
                            
                writer.Write(origTag);
            }
    
            protected override void RenderEndTag(HtmlTextWriter writer)
            {
            }
    
    
            protected override void EndRender(HtmlTextWriter writer)
            {
    
            }
    
            protected override void RenderBeginTag(HtmlTextWriter writer)
            {
    
            }
        }
    }

      I suppect that there may be a better (more efficent) way of doing the string manlipulation and the Regex works on the assumpt that the onclick event is always surrounded by double quotes.

     

    Thanks

    Neil 

Page 1 of 1 (3 items)