这是我将 JQuery mobile 与 asp.net webform 一起使用的成果。下面是我在 Default.aspx 页面中使用的代码。 这是一个非常简单的代码
下面是aspx页面的完整代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title> Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js">
</script>
</head>
<body>
<form id="form1" runat="server">
<!-- Start of first page -->
<div data-role="page" id="foo">
<div data-role="header">
<h1>Mobile Login</h1>
</div><!-- /header -->
<div data-role="content">
<table width="100%">
<tr width="100%">
<td width="30%" align="right" valign="middle">
<asp:Literal ID="lblUserName" runat="server" Text="User Name"></asp:Literal>
</td>
<td width="50%">
<input type="text" maxlength="20" id="UserName" style="width:50%;" runat="server" />
</td>
</tr>
<tr width="100%">
<td width="50%" align="right">
<asp:Literal ID="Literal1" runat="server" Text="Password"></asp:Literal>
</td>
<td width="50%">
<input type="text" maxlength="20" id="Password" style="width:50%;" runat="server" />
</td>
</tr>
<tr width="100%">
<td width="50%" align="right">
</td>
<td width="50%">
<table width="100%">
<tr>
<td width="30%">
<asp:Button ID="btnLogin" runat="server" Text="Login"
OnClick="btnLogin_Click" />
</td>
<td width="30%">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="ui-btn-active"
OnClick="btnCancel_Click" />
</td>
<td width="40%">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div><!-- /content -->
<div data-role="footer">
<h4>
@ All right reserved.
</h4>
</div><!-- /footer -->
</div><!-- /page -->
</form>
</body>
</html>
现在在服务器端
protected void btnLogin_Click(object sender, EventArgs e)
{
Response.Redirect("Home.aspx",true);
}
protected void btnCancel_Click(object sender, EventArgs e)
{
UserName.Value = "";
Password.Value = "";
}
但是当我单击登录/取消按钮时,除了网址发生更改之外,没有任何反应
至
http://localhost:2278/Mobile/Default.aspx#/Mobile/Default.aspx
我的代码有什么问题吗?我无法从 JQuery Mobile 中的 ASP.NET 服务器控件访问服务器端功能吗?我知道在 MVC 中可以做得更好,但在这种情况下这对我来说不是一个选择。
请帮忙
最佳答案
这是因为.net默认将type="submit"
添加到Button控件中。您需要将其设置为 false,我也将 CausesValidation
设置为 false,如下所示:
<asp:Button ID="btnLogin" runat="server" Text="Login"
OnClick="btnLogin_Click" CausesValidation="False" UseSubmitBehavior="False" />
但是,平心而论,我不会仅使用服务器端事件来通过单击按钮导航。您可以通过更简单的方式实现这一点:
<a href="Home.aspx" data-role="button">Login</a>
..当然,假设您在离开之前无需执行任何其他服务器端操作。
关于JQuery Mobile 出现 ASP.NET Web 表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6648515/