linq asp.net

  1. 添加Linq to SQL类
    添加新项|Linq to SQL类,拖放SQL db中的表到dbml文件,自动创建相关的数据库相应的类。(在.cs文件中)
  2. Linq的使用
  •   添加本页面的全局变量,公用的绑定代码

 

public DataClassesDataContext lqDB = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ToString());
 protected void bind()
    {
        var result = from r in lqDB.authors
                     where r.state.Equals("CA")
                     select r;//new { r.au_id, r.au_fname, r.au_lname, r.address};
        GridView1.DataSource = result;
        GridView1.DataBind();
    }
  • PageLoad事件
    protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
    
            bind();
        }

     

  • Row_Editing事件
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.SelectedIndex = e.NewEditIndex;
            GridView1.EditIndex = e.NewEditIndex;
            bind();
    
        }

     

  • RowCancellingEdit事件
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

RowUpdating事件

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //au_id    au_lname    au_fname    phone    address    city    state    zip         contract     
//定位行对象
        authors au = lqDB.authors.First(c => c.au_id == GridView1.DataKeys[e.RowIndex].Value);
//修改行对象
        au.au_lname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        au.au_fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        au.phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        au.address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        au.city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        au.state = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        au.zip = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
        au.contract = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[1]).Checked;//模版字段有itemtemplate\edititemtemplate两个。
//提交行对象
        lqDB.SubmitChanges();
        GridView1.EditIndex = -1;
        bind();
    }

 

转载于:https://www.cnblogs.com/gnsylyj/p/6697372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值