<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Download.aspx.cs" Inherits="App.Web.Download" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="js/jquery-1.8.1.js" type="text/javascript"></script>
<script type="text/javascript">
function DownLoadDoc(id) {
var obj = $("#" + id);//获取a标签绑定的ID值
var cmdValue = obj.attr("cmd");//获取自定义属性cmd的值
var idValue = obj.attr("key");//获取自定义属性key的值
$.ajax({
type: "post",
url: "DoTrigger.ashx",
data: { keyCmd: cmdValue, keyID: idValue },
datatype: "text",
success: function (msgUrl) {
window.open(msgUrl);//获取ashx中Response.Write输出的内容
$("#spAlert").html(msgUrl);//测试输出
}
});//ajax end
}
</script>
</head>
<body>
<!-- 数据绑定控件循环输出-->
<a id="doc1" cmd="update" key="1" οnclick="DownLoadDoc(this.id)" >下载文档-1</a>
<hr />
<span id="spAlert" style="color:red"></span>
</body>
</html>
ashx文件处理请求
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.EF;
using App.DBC;
namespace App.Web
{
/// <summary>
/// DoTrigger 的摘要说明
/// </summary>
public class DoTrigger : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
GetCmd(context);
}
public void GetCmd(HttpContext context)
{
string sql = "";
string cmd = context.Request.Form["keyCmd"];
string id = context.Request.Form["keyID"];
switch (cmd)
{
case "update":
//SQL语句
sql = "update Movie set Clicks=Clicks+1 where ID="+id;
if (DBHelperSQL.ExecuteSql(sql)>0)//执行SQL
{
//一般来说,fileUrl的值是在数据库读取的,这里为了测试,直接写出
string fileUrl = "http://www.abcdefg.com/files/doc" + id + ".txt";
context.Response.Write(fileUrl);
}
else
{
context.Response.Write("执行失败");
}
break;
case "del":
break;
default:
break;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
参考:
http://www.cnblogs.com/acles/articles/2385648.html JQuery Ajax 在asp.net中使用总结
http://q.cnblogs.com/q/34266/ 取不到值的状况