如何开发一个学生成绩管理糸统(10)
2010-05-26 08:52 ScriptZhang 阅读(291) 评论(0) 编辑 收藏 举报这一节,说明一下如何添加课程信息的代码,
添加界面如下
同时我在添加信息的时候写了一个验证课号信息是否己存在,
为是方便调试和使用,我使用了Jquery1.3.2
这是手动写的验证函数
function Checkvalue() { var send = document.getElementById('TxtKid1').value; $.post("Ajax/ChkCourse.ashx", { action:"ChkKid",value: send }, uploadpage); } function uploadpage(data) { var getret = data; document.getElementById('result1').innerHTML = getret; }
在对提交数据时,我也导入了一个验证的JS脚本,你可以将其放到一个js中,就可以调用了,
具体的使用情况可以参考,我使用了在客户端进行数据验证,可以大大减少服务器压力,
http://www.cnblogs.com/jackrebel/archive/2008/10/08/1306293.html
前台的代码是这样写的
<table class="tab2" id="inserttab1" runat="server"> <tr class="alter"><th>课号:</th><td> <asp:TextBox ID="TxtKid1" runat="server" name="Card" dataType="Require" msg="必需是数字" onblur="Checkvalue();" ></asp:TextBox><span id="result1"> </span> </td></tr> <tr><th>课名:</th><td> <asp:TextBox ID="TxtName1" runat="server" name="Card" dataType="Chinese" msg="必需是中文"></asp:TextBox> </td></tr> <tr class="alter"><th>教师:</th><td> <asp:TextBox ID="TxtTeacher1" runat="server" name="Card" dataType="Chinese" msg="必需是中文"></asp:TextBox> </td></tr> <tr ><th>学期:</th><td> <asp:TextBox ID="TxtTerm1" runat="server" name="Card" dataType="Number" msg="必需是数字"></asp:TextBox> </td></tr> <tr class="alter"><th>科目类型:</th><td> <asp:DropDownList ID="TxtProf1" runat="server" Width="152px" > <asp:ListItem Value="1">公共任选课</asp:ListItem> <asp:ListItem Value="2">专业限选課</asp:ListItem> <asp:ListItem Value="3">专业必修课</asp:ListItem> </asp:DropDownList> </td></tr> <tr ><th>教时课时:</th><td> <asp:TextBox ID="TxtThours1" runat="server" name="Card" dataType="Number" msg="必需是数字"></asp:TextBox> </td></tr> <tr class="alter"><th>实验课时:</th><td> <asp:TextBox ID="TxtLhours1" runat="server" name="Card" dataType="Number" msg="必需是数字"></asp:TextBox> </td></tr> <tr><th>自学课时:</th><td> <asp:TextBox ID="TxtPhours1" runat="server" name="Card" dataType="Number" msg="必需是数字"></asp:TextBox> </td></tr> <tr class="alter"><th>操作</th><td> <asp:Button ID="Btnadd1" runat="server" Text="添加" onclick="Btnadd1_Click" OnClientClick="Validator.Validate(document.getElementById('form1'),3)" /> <asp:Button ID="Btnclear1" runat="server" Text="清空" onclick="Btnclear1_Click" /> </td></tr> </table>
点击提交后执行代码 ,
取得数据的方法
代码
//添加按钮1
protected void Btnadd1_Click(object sender, EventArgs e)
{
try
{
GetData1();
InputData();
AlertMessage.showMsg(this, "添加成功");
}
catch (Exception ex)
{
AlertMessage.showMsg(this, "输入错误"+ex.Message);
}
}
代码
//获得数据1
private void GetData1()
{
string kid1 = ((TextBox)inserttab1.FindControl("TxtKid1")).Text.Trim();
string kname1 = ((TextBox)inserttab1.FindControl("TxtName1")).Text.Trim();
string teacher1 = ((TextBox)inserttab1.FindControl("TxtTeacher1")).Text.Trim();
string term1 = ((TextBox)inserttab1.FindControl("TxtTerm1")).Text.Trim();
string prof1 = TxtProf2.SelectedValue;
int thours1 = CString.TryToParse(((TextBox)inserttab1.FindControl("TxtThours1")).Text.Trim());
int lhours1 = CString.TryToParse(((TextBox)inserttab1.FindControl("TxtLhours1")).Text.Trim());
int phours1 = CString.TryToParse(((TextBox)inserttab1.FindControl("TxtPhours1")).Text.Trim());
int id = CourseAPI.GetNextId();
inportdata(kid1, kname1, teacher1, term1, id, prof1, thours1, lhours1, phours1);
}
代码
private void inportdata(string kid,string kname,string teacher,string term,int id,string prof,int thours,int lhours,int phours)
{
GYsms.s_courseRow newCourseRow2 = Coursetab.News_courseRow();
GYsms.s_courseinfoRow newCinfoRow2 = Cinfotab.News_courseinfoRow();
newCourseRow2.kid = Convert.ToInt32(kid);
newCourseRow2.kname = kname;
newCourseRow2.teacher = teacher;
newCourseRow2.term = term;
newCinfoRow2.id = id;
newCinfoRow2.pid = Convert.ToInt32(prof);
newCinfoRow2.thours = thours;
newCinfoRow2.lhours = lhours;
newCinfoRow2.phours = phours;
Coursetab.Adds_courseRow(newCourseRow2);
Cinfotab.Adds_courseinfoRow(newCinfoRow2);
}
如果使用 是批量添加,就要执行别外一个添加方法
代码
//批量添加按钮
protected void bethadd_Click(object sender, EventArgs e)
{
try
{
GetData1();
GetData2();
InputData();
AlertMessage.showMsg(this, "添加成功");
}
catch (Exception ex)
{
AlertMessage.showMsg(this, "输入错误" + ex.Message);
}
}
GetData1()与GetData2()方法类似
我在整个类中设置全局的Table
代码
CourseBLL CourseAPI = new CourseBLL();
CourseInfoBLL CourseInfoAPI = new CourseInfoBLL();
GYsms.s_courseDataTable Coursetab = new GYsms.s_courseDataTable();
GYsms.s_courseinfoDataTable Cinfotab = new GYsms.s_courseinfoDataTable();
只要将数据添加 进行Coursetab和Cinfotab就可以直接添加了。
代码
//提交数据
private void InputData()
{
if (Coursetab.Count > 0)
{
CourseAPI.UpdateWithTransaction(Coursetab);
}
if (Cinfotab.Count > 0)
{
CourseInfoAPI.UpdateWithTransaction(Cinfotab);
}
}
在这里,还要说明一下课号的验证,我使用了Ajax验证,
添加一个ashx.
代码如下
代码
<%@ WebHandler Language="C#" Class="Ajax" %>
using System;
using System.Web;
public class Ajax : IHttpHandler
{
CourseBLL CourseAPI = new CourseBLL();
enum ChkString
{
ChkCid,
}
public string ChkValue(string action, string value)
{
string result;
int Checkid;
int tresult;
if (action == "ChkKid")
{
Checkid = Convert.ToInt32(value);
tresult = Convert.ToInt32(CourseAPI.ChkKid(Checkid));
if (tresult == 0)
{
result = "";
}
else
{
result = "<font color='red'>#课号己存在</font>";
}
}
else if (action == "ChkKname")
{
tresult = Convert.ToInt32(CourseAPI.ChkKname(value));
if (tresult == 0)
{
result = "<font color='red'>#课名不存在</font>";
}
else
{
result = "";
}
}
else
{
result = "";
}
return result;
}
public void ProcessRequest (HttpContext context)
{
string action = context.Request["action"];
string value = context.Request["value"];
context.Response.ContentType = "text/plain";
context.Response.Write(ChkValue(action,value));
}
public bool IsReusable
{
get
{
return false;
}
}
}
这样就可以使用ajax+ashx进行课号验证了,我会在下一节做一个使用jquery +ashx的介绍,也会将代码放出下载。希望对刚入门的同学有帮助。
糸列课程如下: