标签:
http://blog.csdn.net/mazhaojuan/article/details/8592015
先看一下我要实现的功能界面:
这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好。这个页面主要实现的是授课,即给老师教授的课程分配学生。此页面实现功能的步骤已在页面中有所体现,这里不再赘述,此篇文章主要介绍:我是如何通过js从前台获取数据,然后通过ajax技术向后台一般处理程序传递JSON,后台又是如何获取传来的JSON,并对JSON数据转换,然后将转换后的数据写入数据库的!
介绍这些步骤实现前,先说说自己的辛酸:絮叨辛酸,是因为为了实现这个功能,我真的用了很长时间!为了让用户看着舒服点,这个页面的实现我写了700多行的JS代码和9个一般处理程序!为了避免页面刷新影响效果,又不想使用VS自带的UpdatePanel AJAX控件,感觉这个控件会带来不少后患,所以这个页面的实现我使用了大量JS代码。从后台一般处理程序向前台传递JSON,前台解析JSON并显示在界面,这样的实现网上例子很多,非常容易实现,我也写了一篇博客来记录自己的成长,见《《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中》;而从前台向后台传递JSON用法,却让我费尽周折,最终也没找到一个完整的实例,但最后将问题分成几个步骤,然后一步步解决了!
下面分享下我的经验,希望对用到此功能的战友有所帮助,当然我这样实现有什么缺陷,还希望得到高人指点!
1.界面层添加信息列表部分html代码
- <td class="addlist">
- <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: 450px; height: 400px; overflow: scroll;">
- <table class="data-form" id="tabAddList" border="0" cellspacing="0" width="450px"
- align="center" cellpadding="0">
- <tbody>
-
- <tr>
- <th scope="row">
- 教师名:
- </th>
- <td>
- <asp:TextBox CssClass="input-normal" ID="txtAddTeacherName" runat="server"
- Enabled="False"></asp:TextBox>
- <%--隐藏控件,保存教师id--%>
- <asp:HiddenField ID="hidFieldSaveTeacherID" runat="server" />
- </td>
- </tr>
- <tr>
- <th scope="row">
- 课程名:
- </th>
- <td>
- <asp:TextBox CssClass="input-normal" ID="txtAddCourseName" runat="server"
- Enabled="False"></asp:TextBox>
- <%--隐藏控件,保存课程id--%>
- <asp:HiddenField ID="hidFieldSaveCourseID" runat="server" />
- <%--隐藏控件,保存教师、课程关系indexing--%>
- <asp:HiddenField ID="hidFieldSaveIndexing" runat="server" />
- </td>
- </tr>
- <tr>
- <th scope="row">
- 选择上课班:
- </th>
- <td>
- <asp:DropDownList ID="ddlTeachClass" CssClass="input-normal" runat="server">
- </asp:DropDownList>
- </td>
- <td>
- <%-- <asp:Button ID="btnAddTeachClass" runat="server" Text="添加一个上课班" OnClick="btnAddTeachClass_Click" />--%>
-
- <input id="btnAddTeachClass" type="button" class="btn-lit" value="确认添加" onclick="AddTeachClass()" />
- </td>
- </tr>
- <tr>
- <th scope="row">
- 已选授课人数
- </th>
- <td>
- <asp:TextBox CssClass="input-normal" ID="txtStuNum" runat="server"
- Enabled="False"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <th scope="row">
- 学生姓名列表
- </th>
- <td>
- <asp:ListBox ID="lsboxStudent" runat="server" Height="150px" Width="250px"></asp:ListBox>
- </td>
- </tr>
-
- <tr>
- <th scope="row">
- <br />
- </th>
- <td>
- <br />
- <%--<asp:Button ID="btnAddTeachCourse" class="btn-lit" runat="server" Text="确认添加"
- onclick="btnAddTeachCourse_Click" />--%>
- <input id="btnAddTeachCourse" type="button" class="btn-lit" value="确认添加" onclick="AddTeachCourse()" />
- </td>
- </tr>
- </tbody>
- </table>
-
- </div>
- </div>
- </div>
- </td>
选择完学年学期、教师、课程、学生后,点击"确认添加"按钮,执行"添加授课信息"事件!
2.通过JS获取前台授课数据,并向后台一般处理程序传递
- function AddTeachCourse() {
-
-
- var strTeacherID = $("#hidFieldSaveTeacherID").val();
-
-
-
-
- var strCourseID = $("#hidFieldSaveCourseID").val();
-
-
- var strTeachClassID = document.getElementById("ddlTeachClass");
- var TeachClassID = strTeachClassID.options[strTeachClassID.selectedIndex].value;
-
- var Indexing = $("#hidFieldSaveIndexing").val();
-
- var stuNum = $("#txtStuNum").val();
-
-
-
- var listStudent = document.getElementById("lsboxStudent");
-
- var lstStuLength = listStudent.options.length;
-
-
-
-
-
- var StuArrayObjs = [];
- for (var i = 0; i < lstStuLength; i++) {
-
- var StudentID = listStudent.options[i].value;
-
- var StuArrayObj = {};
- StuArrayObj["TeacherID"] = strTeacherID;
- StuArrayObj["CourseID"] = strCourseID;
- StuArrayObj["TeachClassID"] = TeachClassID;
- StuArrayObj["Indexing"] = Indexing;
- StuArrayObj["StuNum"] = stuNum;
- StuArrayObj["StudentID"] = StudentID;
-
- StuArrayObjs.push(StuArrayObj);
-
- }
- var TeachCourseJsonString = JSON.stringify(StuArrayObjs);
-
-
-
-
-
-
-
- $.post("../handler/AddTeachCourse.ashx", { TeachCourseJson: TeachCourseJsonString }, function (data) {
- alert(data);
- });
-
- }
以上有几个需要注意的地方:
2.1.转换JSON串使用
2.2.JS向一般处理程序传递JSON使用
- $.post("要使用的一般处理程序路径", { TeachCourseJson: TeachCourseJsonString }, function (data) {
- alert(data);
- });
3.一般处理程序解析JS传递过来的Json数据,并进行解析,写入DataTable中,然后写入数据库
- <%@ WebHandler Language="C#" Class="AddTeachCourse" %>
- using System;
- using System.Web;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data;
-
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
-
- using Newtonsoft.Json.Converters;
-
- using System.IO;
- using System.Text;
-
- using TeachSystem.BLL.TeachingBLL;
- using TeachSystem.Entity.TeachingEntity;
-
-
- public class AddTeachCourse : IHttpHandler
- {
-
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
-
-
-
-
- DataSet dsStuTeacherCourse = new DataSet("ds_StuTeacherCourse");
-
- DataTable dtAddStudentTeaherCourse = new DataTable("dt_AddStudentTeaherCourse");
-
- dtAddStudentTeaherCourse.Columns.Add(new DataColumn("StudentID", typeof(string)));
- dtAddStudentTeaherCourse.Columns.Add(new DataColumn("Indexing", typeof(int)));
- dtAddStudentTeaherCourse.Columns.Add(new DataColumn("IsAvailable", typeof(string)));
-
-
- DataTable dtAddStudentTeachClass = new DataTable("dt_AddStudentTeachClass");
-
- dtAddStudentTeachClass.Columns.Add(new DataColumn("StudentID", typeof(string)));
- dtAddStudentTeachClass.Columns.Add(new DataColumn("TeachClassID", typeof(string)));
- dtAddStudentTeachClass.Columns.Add(new DataColumn("IsAvailable", typeof(string)));
-
-
- DataTable dtAddCourseTeachClass = new DataTable("dt_AddCourseTeachClass");
-
- dtAddCourseTeachClass.Columns.Add(new DataColumn("TeachClassID", typeof(string)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("Indexing", typeof(int)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("IsAvailable", typeof(string)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("OddEven", typeof(string)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("StuNum",typeof(int)));
-
-
- string ss = context.Request.Form["TeachCourseJson"];
-
- JArray javascript = (JArray)JsonConvert.DeserializeObject(ss);
-
- for (int i = 0; i < javascript.Count; i++)
- {
-
- JObject obj = (JObject)javascript[i];
- string strTeacherID = obj["TeacherID"].ToString();
- string strCourseID = obj["CourseID"].ToString();
- string strTeachClassID = obj["TeachClassID"].ToString();
- int intIndexing = Convert.ToInt32(obj["Indexing"].ToString());
- int intStuNum = Convert.ToInt32(obj["StuNum"].ToString());
- string strStudentID = obj["StudentID"].ToString();
-
-
- DataRow drAddStudentTeaherCourse = dtAddStudentTeaherCourse.NewRow();
-
- drAddStudentTeaherCourse["StudentID"] = strStudentID;
- drAddStudentTeaherCourse["Indexing"] = intIndexing;
- drAddStudentTeaherCourse["IsAvailable"] = "是";
- dtAddStudentTeaherCourse.Rows.Add(drAddStudentTeaherCourse);
-
-
- DataRow drAddStudentTeachClass = dtAddStudentTeachClass.NewRow();
-
- drAddStudentTeachClass["StudentID"] = strStudentID;
- drAddStudentTeachClass["TeachClassID"] = strTeachClassID;
- drAddStudentTeachClass["IsAvailable"] = "是";
- dtAddStudentTeachClass.Rows.Add(drAddStudentTeachClass);
-
- }
-
-
- JObject objs = (JObject)javascript[0];
-
- string strTeachClassIDs = objs["TeachClassID"].ToString();
- int intIndexings = Convert.ToInt32(objs["Indexing"].ToString());
- int intStuNums = Convert.ToInt32(objs["StuNum"].ToString());
-
-
-
- DataRow drAddCourseTeachClass = dtAddCourseTeachClass.NewRow();
-
- drAddCourseTeachClass["TeachClassID"] = strTeachClassIDs;
- drAddCourseTeachClass["Indexing"] = intIndexings;
- drAddCourseTeachClass["IsAvailable"] = "是";
- drAddCourseTeachClass["OddEven"] = null;
- drAddCourseTeachClass["StuNum"] = intStuNums;
- dtAddCourseTeachClass.Rows.Add(drAddCourseTeachClass);
-
-
-
- dsStuTeacherCourse.Tables.Add(dtAddStudentTeaherCourse);
- dsStuTeacherCourse.Tables.Add(dtAddStudentTeachClass);
- dsStuTeacherCourse.Tables.Add(dtAddCourseTeachClass);
-
-
- Boolean flagImportStuTeacherCourse = new TeachCourseBLL().ImportStudentTeacherCourse(dsStuTeacherCourse);
-
-
-
- context.Response.Write(flagImportStuTeacherCourse);
- context.Response.End();
-
-
-
- }
-
-
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
-
- }
一般处理程序代码,需要注意几点:
3.1 获取前台传递过来的JSON字符串数组
- string ss = context.Request.Form["TeachCourseJson"];
3.2 反序列化前台传来的JSON字符才能换数组
- JArray javascript = (JArray)JsonConvert.DeserializeObject(ss);
3.3自定义Dataset,并建立各数据表,将前台传来的数据写入DataTable表
创建Dataset及数据表
-
- DataSet dsStuTeacherCourse = new DataSet("ds_StuTeacherCourse");
-
- DataTable dtAddStudentTeaherCourse = new DataTable("dt_AddStudentTeaherCourse");
-
- dtAddStudentTeaherCourse.Columns.Add(new DataColumn("StudentID", typeof(string)));
- dtAddStudentTeaherCourse.Columns.Add(new DataColumn("Indexing", typeof(int)));
- dtAddStudentTeaherCourse.Columns.Add(new DataColumn("IsAvailable", typeof(string)));
-
-
- DataTable dtAddStudentTeachClass = new DataTable("dt_AddStudentTeachClass");
-
- dtAddStudentTeachClass.Columns.Add(new DataColumn("StudentID", typeof(string)));
- dtAddStudentTeachClass.Columns.Add(new DataColumn("TeachClassID", typeof(string)));
- dtAddStudentTeachClass.Columns.Add(new DataColumn("IsAvailable", typeof(string)));
-
-
- DataTable dtAddCourseTeachClass = new DataTable("dt_AddCourseTeachClass");
-
- dtAddCourseTeachClass.Columns.Add(new DataColumn("TeachClassID", typeof(string)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("Indexing", typeof(int)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("IsAvailable", typeof(string)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("OddEven", typeof(string)));
- dtAddCourseTeachClass.Columns.Add(new DataColumn("StuNum",typeof(int)));
向数据表填充数据,例如
- DataRow drAddCourseTeachClass = dtAddCourseTeachClass.NewRow();
-
- drAddCourseTeachClass["TeachClassID"] = strTeachClassIDs;
- drAddCourseTeachClass["Indexing"] = intIndexings;
- drAddCourseTeachClass["IsAvailable"] = "是";
- drAddCourseTeachClass["OddEven"] = null;
- drAddCourseTeachClass["StuNum"] = intStuNums;
- dtAddCourseTeachClass.Rows.Add(drAddCourseTeachClass);
将各数据表存入DataSet
- dsStuTeacherCourse.Tables.Add(dtAddStudentTeaherCourse);
- dsStuTeacherCourse.Tables.Add(dtAddStudentTeachClass);
- dsStuTeacherCourse.Tables.Add(dtAddCourseTeachClass);
3.4将DataSet中各数据表写入数据库
- Boolean flagImportStuTeacherCourse = new TeachCourseBLL().ImportStudentTeacherCourse(dsStuTeacherCourse);
这篇文章仅仅介绍了添加授课功能的实现,所需要的JS文件及一般处理程序文件,导入数据库用到的BLL层类方法等没有介绍,批量导入数据功能的实现见博客《《项目经验》--简单三层使用DataTable向数据库表批量导入数据---向SqlServer一张表中导入数据》《《项目经验》--简单三层使用DataTable向数据库表批量导入数据---向SqlServer多张张表中导入数据》!
《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
标签:
原文地址:http://www.cnblogs.com/zkwarrior/p/4811384.html