views:

24

answers:

2

We're trying to build a simple asp control for some clients where they can just drop in a single block -

i.e.

<captcha:CaptchaControl ID="CaptchaControl1" 
runat="server"
Server="http://localhost:51947/"
/>

and have it render the control. The catch is that I can't get this to include custom validation. Right now I'm using the RenderContents function to display the layout of the control itself as well as hook it up the to Javascript. The problem is that I don't know how to get custom validation to fire when used as part of a control.

protected override void RenderContents(HtmlTextWriter output)
{

output.Write(@"
<script type=""text/javascript"" src=""http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js""&gt;&lt;/script&gt;   
<link rel=""stylesheet"" type=""text/css"" href=""/Layout/CaptchaLayout.css"" />
//etc
<asp:Textbox id=""text1"" runat=""server"" text=""""></asp:Textbox>
<asp:CustomValidator id=""CustomValidator2"" runat=""server"" 
   ControlToValidate = ""text1""
   ErrorMessage = ""You must enter at least 8 characters!""
   ClientValidationFunction=""validateLength"" >
</asp:CustomValidator>"

            );

    }

Any suggestions for a better way to do this?

A: 

Oogh, I would definitely not recommend your approach. It's very brittle and difficult to maintain, and depending on how your control is used, I'm not even sure that you can output more asp tags and have them processed properly.

Why don't you just inherit your custom control from Panel, and then in the Init or Load event handlers, add the textbox and custom validator to it? Roughly:

public class MyControl : Panel
{
   public MyControl()
   {
   }

   protected override void OnInit(EventArgs e)
   {
    ScriptManager.RegisterScript( ... Google script, CSS, etc. ... );


    TextBox txt = new TextBox();
    txt.ID = "text1";
    this.Controls.Add(txt);

    CustomValidator vld = new CustomValidator();
    vld.ControlToValidatre = "text1";
    vld.ID = "validator1";
    this.Controls.Add(vld);
   }
}
womp
Thanks womp - that seems to make sense so far. My only follow up is that I have a lot of html formatting in the controls. How do I combine that with this method of adding TextBox objects?i.e. if I just rendered out a bunch of divs before, how do I stick this new text box in that html.
Brian
Well, an ASP.Net panel control renders as a <div>. Essentially the code above means that your main control will render as a div, and any controls it contains will be inside that. You just have to think about what your html represents, and add the appropriate controls to your Controls collection to duplicate how it's rendering.
womp
A: 

Your CustomValidator doesn't work because ASP.NET has no idea it's there. You are basically just dumping that output to the response... ASP.NET is not interpreting it.

It seems to me that this is a perfect situation for a User Control rather than a Custom Control. Just drop that output string in its own .ASCX file.

Bryan