标签:
http://blog.csdn.net/mazhaojuan/article/details/8599167
先看一下我要实现的功能界面:
这篇文章主要介绍:后台一般处理程序把从数据库查找的数据,转换成JSON,然后传递到前台JS文件中,JS解析JSON数据,并将数据显示在界面,主要介绍两种显示方式,显示在DropDownList控件 or 显示在动态创建的Table表中。
本文主要介绍两个地方:
1.根据学年查询学期信息的实现--JS将解析的JSON数据绑定到DropDownList框
2.显示教师信息的实现--JS将解析的JSON数据绑定到动态创建的table表中
一、首先介绍根据学年查询学期的实现过程--JS将解析的JSON数据绑定到DropDownList框
学年改变时,会引起学期的改变(这里即实现无刷新联动效果);学年、学期都存储在DropDownList列表框中。
1.添加学年的Onchange事件
学期DropDownList框的HTML代码:
2.在授课JS文件中,编写学年OnChange事件中查找学期的方法(SelectSemesterBySchYear())
注意:不要忘了在授课页面头部引入此JS文件哦!
通过此JS文件,向一般处理程序传递学年,并对传回的JSON学期信息解析,显示在学期DropDownList中。
- function SelectSemesterBySchYear() {
-
-
-
- var schoolyear = document.getElementById("ddlSchoolYear");
- if ("" != schoolyear.options[schoolyear.selectedIndex].value) {
- var getschyearvalue = schoolyear.options[schoolyear.selectedIndex].value;
- var getschyeartext = schoolyear.options[schoolyear.selectedIndex].text;
-
-
- $.ajax({
- type: "get",
- url: "../handler/QuerySemesterBySchoolYear.ashx?SchoolYear=" + escape(getschyearvalue) + "&t=" + new Date().getTime(),
- success: function (strJson) {
-
- bindSemesterDDL(strJson);
-
- },
- error: function (XmlHttpRequest, textStatus, errorThrown) {
- alert(XmlHttpRequest.responseText);
- }
- });
-
- }
-
- }
将查回的学期信息绑定到学期DropDownList框的方法
- function bindSemesterDDL(strJson) {
-
- var dataArray = eval(strJson);
- $("#ddlSemester").empty();
-
- for (var i = 0; i < dataArray.length; i++) {
-
- $("#ddlSemester").append($("<option value=‘" + dataArray[i].Semester + "‘>" + dataArray[i].Semester + "</option>"));
- }
- }
3.一般处理程序代码
一般处理程序调用BLL层方法,根据学年查询相应学期;并对查找回的DataTable做处理,向JS传递JSON学期信息
- <%@ WebHandler Language="C#" Class="QuerySemesterBySchoolYear" %>
-
- using System;
- using System.Web;
- using System.Data;
- using TeachSystem.BLL.TeachingBLL;
- using TeachSystem.Entity.TeachingEntity;
- using TeachSystem.Entity.CourseEntity;
- using System.Text;
- using System.Collections.Generic;
- using System.Reflection;
-
- public class QuerySemesterBySchoolYear : IHttpHandler {
-
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/plain";
-
- string strSchoolYear = context.Server.UrlDecode(context.Request.QueryString["SchoolYear"]);
-
-
-
- SchoolCalendarEntity enSchoolCalendar = new SchoolCalendarEntity();
- enSchoolCalendar.SchoolYear = strSchoolYear;
-
-
-
-
- DataTable dtSemesterByYear = new CourseDistributionBLL().QuerySchoolCalenderByYear(enSchoolCalendar);
-
-
-
- string strJson = DataTableToJson("SemesterJson", dtSemesterByYear);
- context.Response.Write(strJson);
- context.Response.End();
-
- }
-
-
- public string DataTableToJson(string jsonName, DataTable dt)
- {
-
- StringBuilder Json = new StringBuilder();
- Json.Append("[");
- if (dt.Rows.Count > 0)
- {
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- Json.Append("{");
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- Json.Append(dt.Columns[j].ColumnName.ToString() + ":\"" + dt.Rows[i][j].ToString() + "\"");
- if (j < dt.Columns.Count - 1)
- {
- Json.Append(",");
- }
- }
- Json.Append("}");
- if (i < dt.Rows.Count - 1)
- {
- Json.Append(",");
- }
- }
- }
- Json.Append("]");
- return Json.ToString();
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
-
- }
二、根据条件查询教师信息--JS将解析的JSON数据绑定到动态创建的table表中
1.显示教师信息的HTML代码
- <td class="teacherlist">
- <div class="block">
- <div class="h">
- <span class="icon-sprite icon-list"></span>
- <h3>
- 教师信息列表</h3>
- </div>
- <div class="tl corner">
- </div>
- <div class="tr corner">
- </div>
- <div class="bl corner">
- </div>
- <div class="br corner">
- </div>
- <div class="cnt-wp">
- <div class="cnt" style="width: 400px; height: 300px; overflow: scroll;">
- <table class="data-table history" id="mainTable" border="0" cellspacing="0" width="400px"
- align="center" cellpadding="0">
- <tbody>
- <tr>
- <th scope="col">
- 选择
- </th>
- <th scope="col">
- 教师代码
- </th>
- <th scope="col">
- 教师姓名
- </th>
- <th scope="col">
- 教师所属学院
- </th>
- <th scope="col">
- 职称
- </th>
- </tr>
- <%--添加一条模板列--%>
- <tr id="content" class="contentDisplay">
- <td id="select" class="chk">
-
- <input value=‘<%#Eval("TeacherID") %>‘ name="radTeacher" id="radTeacher" type="radio" />
-
- </td>
- <td id="TeacherCode" class="txt c">
- 00154
- </td>
- <td id="TeacherName" class="txt c">
- 木子山
- </td>
- <td id="CollegeName" class="txt c">
- 廊坊师范学院
- </td>
- <td id="TeacherTitle" class="txt c">
- 讲师
- </td>
- </tr>
- </tbody>
- </table>
-
- </div>
- </div>
- </div>
- </td>
2.给查询教师的按钮,添加OnClick事件
3.在授课JS文件中,编写根据条件查询教师的方法
前台JS文件会向一般处理程序传递查询教师的条件,一般处理查询调用BLL层的方法,查询出教师信息转换成JSON数据,然后传回JS,JS解析JSON,并将教师信息显示在动态添加的table表格中!
- function SetData() {
-
- var CollegeID = $(‘#ddlCollegeTeacher option:selected‘).val();
- var TitleID = $(‘#ddlTeacherTitle option:selected‘).val();
- var JobID = $(‘#ddlTeacherJob option:selected‘).val();
- var TeacherName = $(‘#txtTeacherName‘).val();
-
-
- var strSchoolYearID = document.getElementById("ddlSchoolYear");
-
- var strSemesterID = document.getElementById("ddlSemester");
- if ("" == strSchoolYearID.options[strSchoolYearID.selectedIndex].value) {
- alert("请选择学年!");
- } else if ("" == strSemesterID.options[strSemesterID.selectedIndex].value) {
- alert("请选择学期!");
- } else {
-
- $.ajax({
- type: "get",
-
- url: "../handler/QueryTeacherInfo.ashx?CollegeID=" + CollegeID + "&TitleID=" + TitleID + "&JobID=" + JobID + "&TeacherName=" + escape(TeacherName) + "&t=" + new Date().getTime(),
-
-
- success: function (strJson) {
-
-
- var obj = eval(strJson);
-
-
-
-
- $("#mainTable tr").eq(1).removeClass("contentDisplay");
- $("#mainTable tr").eq(1).nextAll().remove();
-
- for (var i = 0; i < obj.length; i++) {
-
- var row = $("#content").clone();
-
-
-
-
-
- row.find("#select input").bind("click", { ID: obj[i].TeacherID, Name: obj[i].TeacherName }, SelectTeacher);
- row.find("#TeacherCode").text(obj[i].TeacherCode);
- row.find("#TeacherName").text(obj[i].TeacherName);
- row.find("#CollegeName").text(obj[i].CollegeName);
- row.find("#TeacherTitle").text(obj[i].TitleName);
-
- row.appendTo("#mainTable");
- }
- $("#mainTable tr").eq(1).addClass("contentDisplay");
-
- },
-
- error: function (XmlHttpRequest, textStatus, errorThrown) {
- alert(XmlHttpRequest.responseText);
- }
- });
- }
-
- }
4.一般处理程序,执行查询教师信息的方法,向JS传递JSON教师信息
- <%@ WebHandler Language="C#" Class="QueryTeacherInfo" %>
- using System;
- using System.Web;
- using System.Data;
- using TeachSystem.BLL.TeachingBLL;
- using TeachSystem.Entity.TeachingEntity;
- using TeachSystem.Entity.PersonnalEntity;
- using TeachSystem.Entity.InstitutionEntity;
- using System.Text;
- using System.Collections.Generic;
- using System.Reflection;
- using Newtonsoft;
- using Newtonsoft.Json;
-
-
- public class QueryTeacherInfo : IHttpHandler {
-
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/plain";
-
-
- CollegeEntity enCollegeTeacher = new CollegeEntity();
-
- enCollegeTeacher.CollegeID = context.Request.QueryString["CollegeID"].ToString();
-
-
- TitlesEntity enTeacherTitle = new TitlesEntity();
-
- enTeacherTitle.TitleID = context.Request.QueryString["TitleID"].ToString();
-
-
- JobEntity enTeacherJob = new JobEntity();
-
- enTeacherJob.JobID = context.Request.QueryString["JobID"].ToString();
-
-
- TeacherEntity enTeacher = new TeacherEntity();
-
- enTeacher.TeacherName = context.Server.UrlDecode(context.Request.QueryString["TeacherName"]);
-
-
- DataTable dtTeacher = new TeachCourseBLL().QuerySimpleTeacherByGroup(enCollegeTeacher, enTeacherTitle, enTeacherJob, enTeacher);
-
-
-
-
- string strJson = DataTableToJson("TeacherJson", dtTeacher);
- context.Response.Write(strJson);
- context.Response.End();
-
- }
-
- public string DataTableToJson(string jsonName, DataTable dt)
- {
-
- StringBuilder Json = new StringBuilder();
- Json.Append("[");
- if (dt.Rows.Count > 0)
- {
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- Json.Append("{");
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- Json.Append(dt.Columns[j].ColumnName.ToString() + ":\"" + dt.Rows[i][j].ToString() + "\"");
- if (j < dt.Columns.Count - 1)
- {
- Json.Append(",");
- }
- }
- Json.Append("}");
- if (i < dt.Rows.Count - 1)
- {
- Json.Append(",");
- }
- }
- }
- Json.Append("]");
- return Json.ToString();
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
-
- }
至此,解析JSON数据后的两种显示方式就介绍完了!
《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
标签:
原文地址:http://www.cnblogs.com/zkwarrior/p/4811380.html