using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DAL;
public partial
class LINQ_DLINQ_SP : System.Web.UI.Page

{
// 实例化一个NorthwindDataContext(DataContext)
// 在对象关系设计器(Object Relational Designer)中拖进来存储过程,同时NorthwindDataContext类中就会自动生成调用相应存储过程的相应方法

NorthwindDataContext _ctx =
new NorthwindDataContext();
protected
void Page_Load(
object sender, EventArgs e)

{
if (!Page.IsPostBack)

{

BindCategory();

}

}
private
void BindCategory()

{

var categories = _ctx.GetCategory(
null);

gvCategory.DataSource = categories;

gvCategory.DataBind();

}
protected
void btnAdd_Click(
object sender, EventArgs e)

{
// categoryId - 用于获取存储过程的输出值(output)
int? categoryId =
null;
// rtn - 用于获取存储过程的返回值(return)
int rtn = _ctx.AddCategory(txtCategoryName.Text, txtDescription.Text,
ref categoryId);

Page.ClientScript.RegisterStartupScript(
this.GetType(),
"js",
string.Format(
"alert('output:{0},return:{1}')", categoryId.ToString(), rtn.ToString()),
true);

gvCategory.EditIndex = -1;

BindCategory();

}
protected
void gvCategory_SelectedIndexChanged(
object sender, EventArgs e)

{

var products = _ctx.GetProduct((
int)gvCategory.SelectedValue);

dvProduct.DataSource = products;

dvProduct.DataBind();

}
protected
void gvCategory_RowDeleting(
object sender, GridViewDeleteEventArgs e)

{
// rtn - 用于获取存储过程的返回值(return)
int rtn = _ctx.DeleteCategory((
int)gvCategory.DataKeys[e.RowIndex].Value);

Page.ClientScript.RegisterStartupScript(
this.GetType(),
"js",
string.Format(
"alert('return:{0}')", rtn.ToString()),
true);

gvCategory.EditIndex = -1;

BindCategory();

}
protected
void gvCategory_RowUpdating(
object sender, GridViewUpdateEventArgs e)

{
// rtn - 用于获取存储过程的返回值(return)
int rtn = _ctx.UpdateCategory(

(
int)gvCategory.DataKeys[e.RowIndex].Value,

((TextBox)gvCategory.Rows[e.RowIndex].Cells[2].Controls[0]).Text,

((TextBox)gvCategory.Rows[e.RowIndex].Cells[3].Controls[0]).Text);

Page.ClientScript.RegisterStartupScript(
this.GetType(),
"js",
string.Format(
"alert('return:{0}')", rtn.ToString()),
true);

gvCategory.EditIndex = -1;

BindCategory();

}
protected
void gvCategory_RowEditing(
object sender, GridViewEditEventArgs e)

{

gvCategory.EditIndex = e.NewEditIndex;

BindCategory();

}
protected
void gvCategory_RowCancelingEdit(
object sender, GridViewCancelEditEventArgs e)

{

gvCategory.EditIndex = -1;

BindCategory();

}

}