Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding Validator Control at runtime in a Custom Control

5 views
Skip to first unread message

Wim Hollebrandse

unread,
Feb 20, 2002, 5:53:22 AM2/20/02
to
Hi,

I have a class which inherits from the TextBox class and I am creating a
requiredfield validator for this input control at runtime. Client side
validation seems to work fine, but when I force server side validation,
nothing happens, it simply posts the form and doesn't display the error
message. I think this might have to do with Viewstate stuff, and also, the
fact that it creates the new control all over again (in the class def.),
when the form is being submitted.

Any ideas?

Here's a simplified example (the control class definition) of what I am
trying to do:

// --- Required Field Textbox
public class Required : TextBox
{
protected RequiredFieldValidator reqCtl;
public string InvalidMessage;

protected override void OnPreRender(System.EventArgs e) {
reqCtl = new RequiredFieldValidator();
reqCtl.ControlToValidate = this.ID;
reqCtl.ErrorMessage = this.InvalidMessage;
//reqCtl.EnableClientScript = false;
//this.Parent.Controls.Add(reqCtl);
Controls.Add(reqCtl);
}

protected override void Render(HtmlTextWriter w)
{
base.Render(w);
reqCtl.RenderControl(w);
}
}
//--

Any ideas of how to get server side validation to work in this example, are
welcome.

Thanks!
Wim


Daniel Cazzulino

unread,
Feb 21, 2002, 12:46:37 PM2/21/02
to
Try using OnInit override instead of OnPreRender.
OnPreRender event happens after LoadViewState and LoadPostData, and that may
be the problem.
OnInit is the first event in the chain.

HTH,
Daniel


"Wim Hollebrandse" <wim.hollebrandseATvi.net> wrote in message
news:utRxFxfuBHA.1608@tkmsftngp04...

Wim Hollebrandse

unread,
Feb 21, 2002, 1:04:36 PM2/21/02
to
Fantastic Daniel. That did the trick indeed.

Thanks!
Wim


"Daniel Cazzulino" <d_k...@yahoo.com> wrote in message
news:urZDW$vuBHA.2324@tkmsftngp02...

Daniel Cazzulino

unread,
Feb 21, 2002, 1:33:39 PM2/21/02
to
I have a printed graph of the event sequence in my pocket, just to remember
always the order:

OnInit
-- LoadViewState
---- LoadPostData
------ OnLoad
-------- RaisePostDataChangedEvent
---------- RaisePostBackEvent
------------ OnPreRender
-------------- SaveViewState
---------------- Render
------------------ Dispose

Daniel

"Wim Hollebrandse" <wim.hollebrandseATvi.net> wrote in message

news:OyVRuGwuBHA.2280@tkmsftngp05...

Wim Hollebrandse

unread,
Feb 22, 2002, 4:15:55 AM2/22/02
to
I was being rather silly. I enabled tracing and saw the whole chain of
events, but yet didn't think of moving the code into the OnInit.

Thanks,
Wim

"Daniel Cazzulino" <d_k...@yahoo.com> wrote in message

news:O3ILqZwuBHA.1860@tkmsftngp04...

mudasi...@gmail.com

unread,
Dec 16, 2004, 12:57:08 PM12/16/04
to
Hi Wim,
How do we link the TextBox to this validator control class in runtime
?
Could you please tell.

Thanks
_
Muds.

Elmer Carías

unread,
Dec 17, 2004, 4:14:26 PM12/17/04
to
Dim aspPage As New String("<asp:CompareValidator Type="Currency"
Operator="DataTypeCheck" ErrorMessage="Please use a Currency value"
runat="server" ControlToValidate="Text_Box_Test" />")
Dim aspControls As New Control
aspControls = Page.ParseControl(aspPage)
Page.Controls.Add(aspControls)

Where Text_Box_Test is the ID of control to validate.

I hope this code helps you.

Atte. Elmer Carías
El Salvador
elmer_...@hotmail.com

<mudasi...@gmail.com> wrote in message
news:1103219827....@z14g2000cwz.googlegroups.com...

0 new messages