Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />
<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?

like image 477
Chad Avatar asked Feb 28 '23 10:02

Chad


1 Answers

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

like image 178
Thorarin Avatar answered Mar 11 '23 19:03

Thorarin