4

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" />
<div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Always" runat="server">
    <ContentTemplate>

        <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))' />

    </ContentTemplate>

</asp:UpdatePanel>

</div>

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
        NextClick()
    End If

    Label1.Text = Date.Now.ToString



End Sub

Private Sub NextClick()
    Survey.RenderPage(PlaceHolder1)
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"
        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

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

Chad
  • 1,404
  • 1
  • 18
  • 29

1 Answers1

8

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(
        navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
    Sys.Browser.name = 'WebKit';
}

Then define your ScriptManager like this:

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

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 :(

Thorarin
  • 47,289
  • 11
  • 75
  • 111
  • worked like a charm, thank you so much. This worked in Chrome too. I believe Safari and Chrome both use the same engine, so that makes sense. – Chad Aug 07 '09 at 21:44
  • That what I was hoping. Chrome uses WebKit, but it has it's own JavaScript engine. I guess it just wasn't recognized at all before this script. Possibly, you could fix that with just an `App_Browsers` file, but if it works, it works :) – Thorarin Aug 07 '09 at 21:47
  • Thanks for the post, I had the same problem in chrome version 8.0.552.237. The funny thing is that it was working without the fix above to begin with, then it just stopped working. – TheLukeMcCarthy Jan 17 '11 at 11:59
  • It may be that Chrome will not send form elements and their values if a form element is disabled or invisible. It saves bits that are sent to the server. For ASP.NET these bits are vital to trigger events. This is an assumption on my side, I'm not sure about it. – mathijsuitmegen Mar 23 '12 at 15:19