Asp.net Dynamic Validators don't work in Chrome or Safari

馋奶兔 提交于 2019-12-21 02:41:11

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/1247032/asp-net-dynamic-validators-dont-work-in-chrome-or-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!