前端之家收集整理的这篇文章主要介绍了
.net ajax调用后台方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
测试-前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTetst.aspx.cs" Inherits="OnlyForTest.AjaxTetst" %>
<!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 type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOK" runat="server" Text="验证用户" />
<asp:Button ID="Button1" runat="server" Text="时间测试" OnClick="Button1_Click" />
</div>
<div>
<asp:DropDownList ClientIDMode="Static" onchange="change(this.value)" ID="DropDownList1" runat="server">
<asp:ListItem Value="0">请选择</asp:ListItem>
<asp:ListItem Value="1">测试1</asp:ListItem>
<asp:ListItem Value="2">测试2</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
<script>
function change(s)
{
$.ajax({
//要用post方式
type: "Post",//方法所在页面和方法名
url: "AjaxTetst.aspx/DropDownTest",contentType: "application/json; charset=utf-8",data: "{'ddl':'" + s + "'}",dataType: "json",success: function (data) {
//返回的数据用data.d获取内容
var json = eval("(" + data.d + ")");
alert(json.length);
for (var i = 0,l = json.length; i < l; i++) {
for (var key in json[i]) {
alert(key + ':' + json[i][key]);
}
}
},error: function (err) {
alert(err);
}
});
}
$(function () {
$("#btnOK").click(function () {
$.ajax({
//要用post方式
type: "Post",//方法所在页面和方法名
url: "AjaxTetst.aspx/SayHello",success: function (data) {
//返回的数据用data.d获取内容
//alert(data.d);
var obj = eval(data.d);
//alert(obj);
alert(obj[0].firstName);
//$.each(function () {
//})
},error: function (err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
</script>
测试-后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Collections;
namespace OnlyForTest
{
public partial class AjaxTetst : System.Web.UI.Page
{
public string year1 = "2014";
public string month1 = "03";
public string year2 = "2015";
public string month2 = "11";
string dll = "";
protected void Page_Load(object sender,EventArgs e)
{
}
[WebMethod]
public static string SayHello()
{
//return "a";
return "[{ \"firstName\": \"Brett\",\"lastName\":\"McLaughlin\",\"email\": \"aaaa\" }," +
"{ \"firstName\": \"Jason\",\"lastName\":\"Hunterwang\",\"email\": \"bbbb\"}]";
}
[WebMethod]
public static string DropDownTest(string ddl)
{
//return "a";
return "[" +
"{ \"v\": \"1\",\"t\":\"唐军\" }," +
"{ \"v\": \"2\",\"t\":\"石峰\"}" +
"]";
}
protected void Button1_Click(object sender,EventArgs e)
{
Dictionary<string,string> dic = new Dictionary<string,string>();
//ArrayList<>
string str1 = year1+"-"+month1;
string str2 = year2 + "-" + month2;
DateTime c1 = Convert.ToDateTime(Convert.ToDateTime(str1).ToString("yyyy-MM"));
DateTime c2 = Convert.ToDateTime(Convert.ToDateTime(str2).ToString("yyyy-MM"));
if (c1 > c2)
{
DateTime tmp = c1;
c1 = c2;
c2 = tmp;
}
while (c2 >= c1)
{
//MessageBox.Show(c1.Month.ToString());
//MessageBox.Show(c1.ToString("yyyy-MM"));
dic.Add(c1.ToString("yyyy-MM"),"0,0");
c1 = c1.AddMonths(1);
}
DataSet ds = DbHelpersql.Query("select *,convert(char(7),D_datatime,20) as a from d_Data");
foreach (var item in dic)
{
DataRow[] dr= ds.Tables[0].Select("a='" + item.Key+"'");
if (dr.Length > 0 && dr != null)
{
}
}
}
}
}
原文链接:https://www.f2er.com/ajax/162589.html