ASPxGridView和Linq常用总结(2)
实现目标:使用js、Linq和ASPxGridView实现增删改数据
1、查询(点击grd,赋值text)
//cs后台
引用using System.Web.Script.Serialization;
protected void Page_Load(object sender, EventArgs e)
{
//if (Session["LoginUser"] == null)
//{
// Response.Write("<script>top.window.location.href('../login.aspx')</script>");
//}
Ajax.Utility.RegisterTypeForAjax(typeof(Driver));//注册Ajax
}
//js页面可以使用此方法
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public string GetTableByKey(int iCode)
{
Tb_Table objTable = linqDataSource.Tb_Table .SingleOrDefault(p => p.ID == iCode);
JavaScriptSerializer jscvt = new JavaScriptSerializer();
string jsonobjTable = jscvt.Serialize(objTable );
return jsonobjTable ;
}
//js页面
function getQueryTable() {
var sId = grdView.GetRowValues(grdView.GetFocusedRowIndex(), 'ID;Name', QueryTable);
}
function QueryTable(values) {
var key = values[0];
if (key != null) {
var objTable= 后台页面.GetTableByKey(key);
if (objTable.value != "null") {
objTable = eval('(' + objTable.value + ')');
txt_DriverName.SetText(objTable.DriverName);
txt_DriverName.SetIsValid(1);
dte_DriverlicenseDateB.SetText(objTable.DriverlicenseDateB);
}
}
else {
btnEdit.SetEnabled(false);
btnDelete.SetEnabled(false);
}
}
//源
//grd
<ClientSideEvents FocusedRowChanged="function(s, e) { getQueryTable(); }"/>
2、删除一行
//js页面
function getDeleteDriver() {
var sDriverId = grdDriver.GetRowValues(grdDriver.GetFocusedRowIndex(), 'DriverID;DriverName',
DeleteDriver);
}
//删除菜单
function DeleteDriver(values) {
var key = values[0];
if (key != null) {
var r = confirm('您确定要删除此司机信息吗?');
if (r == true) {
var r_Del = Driver.DeleteDriver(key);
if (r_Del.value != "成功") {
alert(r_Del.value);
}
else {
grdDriver.PerformCallback("");
}
}
}
else {
alert('请选中您要删除的模块!');
}
}
//cs页面
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public string DeleteDriver(string iCode)
{
try
{
Tb_Driver objDriver = linqData.Tb_Driver.SingleOrDefault(p => p.DriverID == Convert.ToInt32
(iCode));
if (objDriver != null)
{
linqData.Tb_Driver.DeleteOnSubmit(objDriver);
linqData.SubmitChanges();
return "成功";
}
else
{
return "删除失败,模块信息不存在!";
}
}
catch (System.Exception ex)
{
return ex.Message;
}
}
//源
//删除按钮
<ClientSideEvents click="function(s, e) { getDeleteDriver(); }"/>
3、新增、修改和保存
//cs页面
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public string AddDriver(string sDriverName,string sSFZH, dFristLicenseDate)
{
try
{
Tb_Driver objDriver = new Tb_Driver();
objDriver.DriverName = sDriverName;
objDriver.SFZH = sSFZH;
if (dFristLicenseDate!="")
{
objDriver.FristLicenseDate = Convert.ToDateTime(dFristLicenseDate);
}
linqData.Tb_Driver.InsertOnSubmit(objDriver);
linqData.SubmitChanges();
return "成功";
}
catch (System.Exception ex)
{
return ex.Message;
}
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public string EidtDriver(int iDriverID, string sDriverName,string dFristLicenseDate)
{
try
{
Tb_Driver objDriver = linqData.Tb_Driver.SingleOrDefault(p => p.DriverID == iDriverID);
if (objDriver != null)
{
objDriver.DriverName = sDriverName;
objDriver.SFZH = sSFZH;
if (dFristLicenseDate != "")
{
objDriver.FristLicenseDate = Convert.ToDateTime(dFristLicenseDate);
}
linqData.SubmitChanges();
return "成功";
}
else
{
return "编辑失败,模块信息不存在!";
}
}
catch (System.Exception ex)
{
return ex.Message;
}
}
//js页面
function getSaveDriver() {
var sDriverId = grdDriver.GetRowValues(grdDriver.GetFocusedRowIndex(), 'DriverID;DriverName', SaveDriver);
}
function SaveDriver(values) {
var keyID = values[0];
//执行客户端合法性验证
var sDriverName = txt_DriverName.GetText();
var sSFZH=txt_SFZH.GetText();
if (sDriverLicenseFileNo == "") {
txt_DriverLicenseFileNo.Validate();
return;
}
var key = roundEdit.GetHeaderText();
var sResult;
if (key == "司机信息---编辑") {
sResult = Driver.EidtDriver(values[0], sDriverName, sSFZH, dFristLicenseDate);
}
if (key == "司机信息---新增") {
sResult=Driver.AddDriver(sDriverName, sSFZH, dFristLicenseDate);
}
if (sResult.value != "成功") {
alert(sResult.value);
return;
}
else {
grdDriver.PerformCallback("");
if (key == "司机信息---编辑") {
setPageState(0);
}
if (key == "司机信息---新增") {
setPageState(2);
}
}
}
//源
//保存按钮
<ClientSideEvents click="function(s, e) { getSaveDriver(); }"/>
注意:所有页面按钮的AutoPostBack="False"
摘自 SYZ_YUMEIZHOU_YY