R – Asp.net Dynamic Validators don’t work in Chrome or Safari


Ok, I'm dynamically creating Asp.net validation controls and inserting them into an update panel. The validation works in IE and Firefox, but not in Chrome or Safari.

Here is the aspx file. Don't ask why I'm not using a button server control…

 <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Always" runat="server">

        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <input id="Button1" type="button" value="submit" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "btnNext", true, "", "", false, true))' />




Here is the code behind:

 Dim Survey As New Survey

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Request("__EVENTARGUMENT") = "btnNext" Then
    End If

    Label1.Text = Date.Now.ToString

End Sub

Private Sub NextClick()
End Sub

And here is the class:

    Public Class Survey

    Public Sub RenderPage(ByVal PlaceHolder As PlaceHolder)

        Dim textbox As New TextBox
        textbox.ID = "testing"

        Dim val As New RequiredFieldValidator
        val.ControlToValidate = textbox.ID
        val.Text = "required"
        val.EnableClientScript = True

    End Sub
End Class

Does anyone have any ideas on how to get this to work in Chrome and Safari?

Best Solution

ASP.NET AJAX doesn't play well with Safari by default. It has several JavaScript hacks in it to make it work with Safari 1.x that are no longer needed. Unfortunately, this breaks AJAX for Safari 3. But, there is a solution.

Create a Safari3AjaxHack.js, like this:

// Safari 3 AJAX "issue". It no longer needs JavaScript hacks that are still implemented
// http://forums.asp.net/p/1252014/2392110.aspx

Sys.Browser.WebKit = {}; //Safari 3 is considered WebKit
if (navigator.userAgent.indexOf('WebKit/') > -1) {
    Sys.Browser.agent = Sys.Browser.WebKit;
    Sys.Browser.version = parseFloat(
    Sys.Browser.name = 'WebKit';

Then define your ScriptManager like this:

<asp:ScriptManager runat="server" ID="ScriptManager1">
        <asp:ScriptReference Path="~/Scripts/Safari3AjaxHack.js" />

I'm not sure about Chrome. I haven't had ASP.NET AJAX problems with it so far. It's pretty silly that Microsoft didn't fix this in .NET 3.5 SP1 at least, but what can you do :(

Related Question