jQuery .get in aspx(不使用单独的 HTML)
Posted
技术标签:
【中文标题】jQuery .get in aspx(不使用单独的 HTML)【英文标题】:jQuery .get in aspx (not using separate HTML) 【发布时间】:2018-08-11 00:09:33 【问题描述】:我正在 asp.net 中试验 jquery 和 json 以获得更好的想法并在项目中实现它。
在使用单独的 html 页面时,代码可以正常工作。但是当我尝试在单个 aspx 页面中实现它时,后端处理失败。
以下是预览:
下面是我的代码:
在 HTML 页面中:
<!DOCTYPE html>
<html>
<head runat="server">
<script src="Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function ()
var textBoxes = $('input[type="text"]');
textBoxes.keyup(function ()
var helpDiv = $(this).attr('id');
var textInBox = $(this).val();
$.get('jQuery_Ajax_json', HelpTextKey: textInBox , function (response)
$('#' + helpDiv + 'HelpDiv').html(response.Text);
, 'json');
);
);
</script>
</head>
<body style="font-family:Arial">
<table>
<tr>
<td>First Name</td>
<td><input id="firstName" type="text" /></td>
<td><div id="firstNameHelpDiv"></div></td>
</tr>
<tr>
<td>Last Name</td>
<td><input id="lastName" type="text" /></td>
<td><div id="lastNameHelpDiv"></div></td>
</tr>
<tr>
<td>Email</td>
<td><input id="email" type="text" /></td>
<td><div id="emailHelpDiv"></div></td>
</tr>
<tr>
<td>Income</td>
<td><input id="income" type="text" /></td>
<td><div id="incomeHelpDiv"></div></td>
</tr>
</table>
</body>
</html>
在 aspx 文件中:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="jQuery_Ajax_json.aspx.cs" Inherits="WebApplication1.jQuery_Ajax_json" %>
在 aspx.cs 文件中:
using System;
using System.Web.Script.Serialization;
namespace WebApplication1
public partial class jQuery_Ajax_json : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
JavaScriptSerializer js = new JavaScriptSerializer();
string JSONString = js.Serialize(GetHelpTextByKey(Request["HelpTextKey"]));
Response.Write(JSONString);
private HelpText GetHelpTextByKey(string key)
HelpText helpText = new HelpText();
helpText.Key = key;
helpText.Text = key.ToUpper();
return helpText;
public class HelpText
public string Key get; set;
public string Text get; set;
我现在正试图完全消除 HTML 页面,只使用 aspx 和 aspx.cs。如果您能指出正确的方向,我将不胜感激。
这是我的看法:
aspx 文件:
<!DOCTYPE html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jQuery_test.aspx.cs" Inherits="folder_manage_system.jQuery_test" %>
<html>
<head runat="server">
<title> </title>
<script src="Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function ()
var textBoxes = $('input[type="text"]');
textBoxes.keyup(function ()
var helpDiv = $(this).attr('id');
var textInBox = $(this).val();
$.get('jQuery_test', HelpTextKey: textInBox , function (response)
$('#' + helpDiv + 'HelpDiv').html(response.Text);
, 'json');
);
);
</script>
</head>
<body style="font-family:Arial">
<table>
<tr>
<td>First Name</td>
<td><input id="firstName" type="text" /></td>
<td><div id="firstNameHelpDiv"></div></td>
</tr>
<tr>
<td>Last Name</td>
<td><input id="lastName" type="text" /></td>
<td><div id="lastNameHelpDiv"></div></td>
</tr>
<tr>
<td>Email</td>
<td><input id="email" type="text" /></td>
<td><div id="emailHelpDiv"></div></td>
</tr>
<tr>
<td>Income</td>
<td><input id="income" type="text" /></td>
<td><div id="incomeHelpDiv"></div></td>
</tr>
</table>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
namespace folder_manage_system
public partial class jQuery_test : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
JavaScriptSerializer js = new JavaScriptSerializer();
string JSONString = js.Serialize(GetHelpTextByKey(Request["HelpTextKey"]));
Response.Write(JSONString);
private HelpText GetHelpTextByKey(string key)
HelpText helpText = new HelpText();
if (key != null)
helpText.Key = key;
helpText.Text = key.ToUpper();
return helpText;
public class HelpText
public string Key get; set;
public string Text get; set;
结果预览:
【问题讨论】:
【参考方案1】:将页面声明后的<!DOCTYPE html>
移动为:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jQuery_test.aspx.cs" Inherits="folder_manage_system.jQuery_test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
【讨论】:
试过了,但似乎没有解决问题。我现在添加了这个: if (Page.IsPostBack) JavaScriptSerializer js = new JavaScriptSerializer();字符串 JSONString = js.Serialize(GetHelpTextByKey(Request["HelpTextKey"]));响应.Write(JSONString);并在页面加载后摆脱了页面上打印的 json 字符串。现在连 jquery 都在 keyup 上运行,但似乎没有进入匿名函数将输出写入 helpDiv。以上是关于jQuery .get in aspx(不使用单独的 HTML)的主要内容,如果未能解决你的问题,请参考以下文章