GrideView(三)---編輯功能實現

來源:互聯網
上載者:User

標籤:

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(三)---編輯功能實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.