linq asp.net

來源:互聯網
上載者:User

標籤:manager   phone   變數   can   als   linq   頁面   template   updating   

  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();    }

 

linq asp.net

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.