views:

730

answers:

1

I'm dynamically creating validation controls and adding them to an update panel. However the client side validation never fires.

Here is the aspx file:

<div>
    <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
    <Triggers >
        <asp:AsyncPostBackTrigger ControlID ="Button1" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

<asp:Button ID="Button1" runat="server" Text="Button"  CausesValidation="true"/>


</div>

Here is the code behind:

Dim Survey As New Survey
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Survey.RenderPage(PlaceHolder1)
End Sub

Here is the class that creates the validation control:

Public Class Survey

    Public Sub RenderPage(ByVal PlaceHolder As PlaceHolder)

        Dim textbox As New TextBox
        textbox.ID = "testing"
        PlaceHolder.Controls.Add(textbox)

        Dim val As New RequiredFieldValidator
        val.ControlToValidate = textbox.ID
        val.Text = "required"
        val.EnableClientScript = True
        PlaceHolder.Controls.Add(val)

    End Sub
End Class

When you hit next, client side validation never fires. What's really weird is that when you wrap the button inside another update panel, the validation fires (in IE and Firefox, but not in Chrome or Safari).

Anyone have any ideas what's going on? I know that the first versions of Asp.net AJAX didnt support the validation controls, but everything is up to date on my end.

A: 

I see there 2 problems

  1. When update panel causes async post back to server it cannot create tree of controls with your dynamic controls - so check that you call RenderPage from Page_Load for ScriptManager.IsInAsyncPostBack == true
  2. There is issue of usage validators under update panel - the scripts must be loaded before update panel works. I can propose you to allocate fictive RequiredFieldValidator under UpdatePanel. Set them Display=none (but not Visible=false !!!) or place to nonexistence ValidationGroup. This allows to render JScript into you page.
Dewfy
Thanks for the response. After messing around for a while, I can get it to work in IE and Firefox but not Chrome or Safari, so I have posted a separate question. http://stackoverflow.com/questions/1247032/asp-net-dynamic-validators-dont-work-in-chrome-or-safari
Chad