Asp.net动态验证器无法在Chrome或Safari中使用

时间:2009-08-07 21:10:10

标签: asp.net asp.net-ajax validation-controls

好的,我正在动态创建Asp.net验证控件并将它们插入更新面板。验证适用于IE和Firefox,但不适用于Chrome或Safari。

这是aspx文件。不要问为什么我没有使用按钮服务器控件...

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

这是背后的代码:

 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

这是班级:

    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

有没有人对如何在Chrome和Safari中使用它有任何想法?

1 个答案:

答案 0 :(得分:8)

默认情况下,ASP.NET AJAX与Safari不兼容。它有几个JavaScript hacks,使它可以与不再需要的Safari 1.x一起使用。不幸的是,这打破了Safari 3的AJAX。但是,有一个解决方案。

创建Safari3AjaxHack.js,如下所示:

// 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';
}

然后像这样定义ScriptManager:

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

我不确定Chrome。到目前为止,我还没有遇到ASP.NET AJAX问题。微软至少在.NET 3.5 SP1中没有解决这个问题是非常愚蠢的,但你能做些什么:(