標籤:
GrideView(三)---編輯
法一、(優勢:操作資料更加靈活;劣勢: 書寫較多代碼,開發效率低)
通過編輯列---添加超連結(HyperlinkField)欄位 ,來跳轉頁面實現編輯操作;
步驟: 1 、通過編輯列---添加超連結(HyperlinkField)欄位 ;
2、更改設定:外觀- Text “編輯”;資料-DataNavigateUrlFields:主索引值;
資料-DataNavigateUrlFormatString:edit.aspx?id={0} 跳轉到操作頁面 ; 並把 主索引值傳遞過去;
3、添加edit.aspx 頁面;
在頁面中添加編輯代碼:
1 private MYDataContext _context = new MYDataContext(); 2 protected void Page_Load(object sender, EventArgs e) 3 { 4 if (!IsPostBack) // 此處重點 判斷是否是第一次載入頁面,是的話進入,否則執行更新代碼; 5 { 6 string s = Request["id"];// 擷取主索引值 7 FIllcar(s);//載入資料 8 } 9 10 }11 12 private void FIllcar(string s)13 {14 var minmi = _context.Car.Where(p => p.Code == s);15 if (minmi.Count() > 0)16 {17 //載入資料18 Car data = minmi.First();19 TextBox1.Text = data.Code;20 TextBox2.Text = data.Name;21 TextBox3.Text = data.Brand;22 TextBox4.Text = data.Oil.ToString();23 TextBox5.Text = data.Price.ToString();24 }25 }26 protected void Button1_Click(object sender, EventArgs e)27 {28 // 編輯資料後更改資料庫並傳送至頁面顯示29 string code = Request["id"];30 var mimi = _context.Car.Where(p=>p.Code==code);31 Car data = mimi.First();32 data.Name = TextBox2.Text;33 data.Brand = TextBox3.Text;34 data.Oil = Convert.ToDecimal(TextBox4.Text);35 data.Price = Convert.ToDecimal(TextBox5.Text);36 _context.SubmitChanges();37 Response.Redirect("Default.aspx");38 39 40 }41 protected void Button2_Click(object sender, EventArgs e)42 {43 // 點擊取消按鈕後跳轉到顯示頁面44 Response.Redirect("Default.aspx");45 }
法二、通過GrideView 控制項內建的編輯功能實現;(優勢:開發效率高;劣勢:對於資料的操作不靈活,不顯示在頁面上的資料(或者隱藏的資料)不能顯示)
<%#Eval()%> 只能輸出資料 <%#Bind()%>//輸出資料並接受返回資料《詳細解釋 見播客 <% 看這裡! %>》
操作要點:基本上均為可視化操作:見圖;
1 視圖啟用刪除功能
2 GrideView 啟用刪除功能
GrideView(三)---編輯功能實現