views:

230

answers:

2

Hi,

i want to create a validatior via code behind.But although i create it,it doesnt fire when postback occurs.I craete validator in Page's Onload event.What can be reason?Here is my code.

Public Sub Validate(ByVal p_Page As Page, ByRef p_AssignedControl As System.Web.UI.WebControls.WebControl, ByVal p_Message As String)

    Dim v_VCALLOUT As New AjaxControlToolkit.ValidatorCalloutExtender

    Dim r_VALIDATOR As New System.Web.UI.WebControls.RequiredFieldValidator
    Dim placeHolder As PlaceHolder = p_Page.FindControl("Vld")


    With r_VALIDATOR

        .ID = "r_" & p_AssignedControl.ID
        .ControlToValidate = p_AssignedControl.ID
        .Display = ValidatorDisplay.None
        .ErrorMessage = p_Message
        .Font.Name = "Verdana"
        .Font.Size = FontSize.XSmall

    End With

    With v_VCALLOUT

        .ID = "v_" & r_VALIDATOR.ID
        '.CssClass = "highlight"
        .HighlightCssClass = "highlight"
        .WarningIconImageUrl = "~/images/warning.gif"
        .CloseImageUrl = "~/images/close.gif"
        .TargetControlID = r_VALIDATOR.ID
        .Enabled = True

    End With

    placeHolder.Controls.Add(v_VCALLOUT)
    placeHolder.Controls.Add(r_VALIDATOR)


End Sub
A: 

First thought: (from MSDN)

None The validation message is never displayed inline.

Do you have a Validation Summary?

Second thought:

try to create your validators in the Page_Init event.

Arthur
i tried second,but it doesnt work.I didnt understand "None The validation message is never displayed inline".When i use these controls in aspx,it works.Why not in code behind?I think this is about page events but i couldnt guess where.
Alexander
What does Public Sub Validate(ByVal p_Page As Page, ByRef p_AssignedControl As System.Web.UI.WebControls.WebControl, ByVal p_Message As String) mean?
Arthur
because i declare this sub in a module and then call it in page.I dont wanan repeat same code in my pages.
Alexander
OK - I see. Pls. post also the calling code in your page. Your Sub looks fine.
Arthur
i call it on_load or on_init so.mdlUtilities.Validate(Me, Me.txtEntry, "Please Enter Something")
Alexander
What's with p_AssignedControl? Is it also created dynamically?
Arthur
No,it is in aspx.Forexample on page,when i want to use validator for a control,i send this control ID as parameter.
Alexander
A: 

I found reason.I call Validate Sub if page.IsPostpact=False.If i call Validate Sub in every postback,it works great.

Alexander