我在學C#& ASP.NET做一個網站。我試圖通過AJAX &訪問.aspx腳本,只是返回一些HTML。C#代碼:這是如何ASP.NET AJAX腳本工作?
所以我訪問腳本http://www.mywebsite.com/test.aspx?i=199 &所有返回是:
<p>You queried: 199</p>
ASP.NET是否允許我通過CGI變量(I = 199)還是我的意思使用不同的方法?
這是你的意思在ASP.NET網站使用腳本的正確方法:
Test.aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication1.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div id="testDiv" runat="server"></div>
<!-- Must I have the html, head & body elements or can I just return a div? -->
test.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int index = url.Split("i=").Last(); // this causes an error: split has some invalid arguments
testDiv.InnerHtml = string.Format("<p>You Queried: {0}", index);
}
}
}
我使用腳本的方式是在我的主頁上有一個按鈕,當點擊發出一個AJAX請求到test.aspx & g回到p HTML元素/文本:
function getData(n)
{
$.ajax({
type: "POST",
url: "test.aspx",
data: "i="+n
}).done(function(msg) {
$("textarea:first").html(msg));
});
}
<button onclick="getData(199)"/>
僅供參考,這不是一個CGI變量。這只是一個查詢字符串。 – 2012-02-20 02:22:59