I am learning C# & ASP.NET to make a website. I am attempting to access a .aspx script through AJAX & just get some HTML back.
So I access the script http://www.mywebsite.com/test.aspx?i=199 & all it returns is:
<p>You queried: 199</p>
Does ASP.NET allow me to pass CGI variables(i=199) or am I meant to use a different method?
Is this the correct way you are meant to use scripts in a ASP.NET websites:
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);
}
}
}
The way I use the script is to have a button on my home page that when click makes an AJAX request to test.aspx & gets back the p HTML element/text:
function getData( n )
{
$.ajax({
type: "POST",
url: "test.aspx",
data: "i="+n
}).done(function( msg ) {
$("textarea:first").html(msg) );
});
}
<button onclick="getData(199)"/>
This isn’t specific to asp.net.
You need to look at the request object, specifically the Query String collection. These concepts are common to many web server side languages/frameworks. Through personal experience Ive used it in classic asp, asp.net and php.
To get the value from the query string: