找不到变量__doPostBack

3
  • 我正在编写我的第一个asp.net网站
  • 以便在iPhone Safari上运行。

我的default.aspx正文中的代码:

<div id="pnlLogin" class="panel" selected="true" >
    <h2>Login Details</h2>
    <form ID="fLogin" runat="server" class="panel" selected="true" > 
        <fieldset>
            <div class="row">
                <label>Name</label>
                <asp:TextBox id="txtUserName" runat="server" placeholder="Your username" />
            </div>
            <div class="row">
                <label>Password</label>
                <asp:TextBox id="txtPassword" textmode="Password" runat="server" placeholder="Your password" />
            </div>
        </fieldset>
        <asp:LinkButton id="btnLogin" class="whiteButton" text="Log me in!" runat="server" onclick="Login_Clicked" />
    </form> 
</div>

在 default.aspx 的后端 .cs 文件中:
    protected void Login_Clicked(object sender, EventArgs e)
    {
        var username = txtUserName.Text;
        var password = txtPassword.Text;

        if (username == "masi" && password == "pass")
        {
            Response.Redirect("ControlPanel.aspx");
        }
    }

来自桌面Safari的完整页面源代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head><title>
    Cover Plus
</title><meta id="viewport" name="viewport" content="width=device-width, user-scalable=0, initial-scale=1.0" /><link href="iui/iui.css" rel="stylesheet" type="text/css" /><link title="default" href="iui/t/default/default-theme.css" rel="stylesheet" type="text/css" />
        <script type="application/x-javascript" src="iui/iui.js"></script>


        <link rel="apple-touch-icon" href="img/touch-icon-iphone.png" /><link rel="apple-touch-icon" sizes="72x72" href="img/touch-icon-ipad.png" /><link rel="apple-touch-icon" sizes="114x114" href="img/touch-icon-iphone4.png" /><link rel="apple-touch-startup-image" href="img/startup.png" />
    <script type="text/javascript">
        function login()
        {
            var isVerified = Verify();
            if (isVerified) {
                ident.setAttribute("placeholder", "valid");
            }
        }
    </script>
</head>
    <body>


<div class="toolbar">
    <h1 id="pageTitle">Login</h1>
</div>

<div id="pnlLogin" class="panel" selected="true" >
    <h2>Login Details</h2>
    <form method="post" action="Default.aspx" id="fLogin" class="panel" selected="true">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTY2NzcyMzEwM2Rk+CbfIXzzsip63MXaBjBxcQhbraDzpmAHkc6FH4cZIiE=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['fLogin'];
if (!theForm) {
    theForm = document.fLogin;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBALC/9DxDQKMrfuqAgKOsPfSCQKRnIq9D4GV4RtvQeooslP0bkLhJVOPoPu6Gt6b0rGrW9P8fPbK" />
</div> 
        <fieldset>
            <div class="row">
                <label>Name</label>
                <input name="ctl00$MainContent$txtUserName" type="text" id="MainContent_txtUserName" placeholder="Your username" />
            </div>
            <div class="row">
                <label>Password</label>
                <input name="ctl00$MainContent$txtPassword" type="password" id="MainContent_txtPassword" placeholder="Your password" />
            </div>
        </fieldset>
        <a id="MainContent_btnLogin" class="whiteButton" href="javascript:__doPostBack(&#39;ctl00$MainContent$btnLogin&#39;,&#39;&#39;)">Log me in!</a>
    </form> 
</div>

</body>
</html>
问题:

在桌面版的Safari/Chrome上运行良好,我键入masi/pass并按下按钮,然后跳转到新页面,但在iPhone上,它会在控制台中给我一个Javascript错误,什么也不做。

它说: Javascript:错误 未定义 引用错误:找不到变量:__doPostBack

我完全不知道该怎么办。

更新-解决方案:

我随机删除了头部中几乎无效的javascript代码,现在它可以正常工作了。


你有多少个表单?ASP .Net WebForms 只能使用一个表单。同时确保你没有嵌套的表单。 - Adrian Iftode
据我所知,每个aspx页面只有一个表单,因此在default.aspx上只有一个表单。 - sprocket12
有可能您正在使用包含此函数的母版页,就像源代码中所示,因此您需要手动将其添加到您的页面中。 - Vishal Suthar
大家请看我的更新,问题已经解决了。如果有人知道为什么它不喜欢头部的JavaScript,请告诉我。 - sprocket12
Verify函数是做什么的?这可能是问题产生的地方。 - Alex Morales
1个回答

3
问题在于ASP.net默认处理未知浏览器的方式...例如iPhone。尽管假设未知浏览器可以使用JavaScript很好,但您可以在web.config或machine.config的<browsers>部分指定浏览器具有的功能。
请查看http://slingfive.com/pages/code/browserCaps/以获取更新的asp.net browsercaps配置文件。
以下是匹配基于GECKO的浏览器(Netscape 6+,Mozilla/Firefox等)的示例:
参考:this question and answer.
<case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">
                    browser=Gecko
                    <filter>
                            <case match="(Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">
                                    type=${type}
                            </case>
                            <case> <!-- plain Mozilla if no VendorProductToken found -->
                                    type=Mozilla
                            </case>
                    </filter>
                    frames=true
                    tables=true
                    cookies=true
                    javascript=true
                    javaapplets=true
                    ecmascriptversion=1.5
                    w3cdomversion=1.0
                    css1=true
                    css2=true
                    xml=true
                    tagwriter=System.Web.UI.HtmlTextWriter
                    <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
                            version=${version}
                            majorversion=0${major}
                            minorversion=0${minor}
                            <case match="^b" with="${letters}">
                                    beta=true
                            </case>
                    </case>
            </case>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接