当前位置:Gxlcms >
数据库问题 >
《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
时间:2021-07-01 10:21:17
帮助过:2人阅读
<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获取前台授课数据,并向后台一般处理程序传递
[javascript] view plaincopyprint?
- 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串使用
[javascript] view plaincopyprint?
- JSON.stringify()
2.2.JS向一般处理程序传递JSON使用
[javascript] view plaincopyprint?
- $.post("要使用的一般处理程序路径", { TeachCourseJson: TeachCourseJsonString }, function (data) {
- alert(data);
- });
3.一般处理程序解析JS传递过来的Json数据,并进行解析,写入DataTable中,然后写入数据库
[csharp] view plaincopyprint?
- <%@ 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);
-
-
-