Sử dụng DropDownList trong DataGrid pdf

6 503 0
Sử dụng DropDownList trong DataGrid pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

Sử dụng DropDownList trong DataGrid Cập nhật: 27/11/2008 với no comments Xếp trong: Lập trình, C#, Asp.Net Bài viết này sẽ chỉ cho bạn cách sử dụng điều khiển DropDownList trong DataGrid (hoặc GridView). Bạn sẽ cần thay đổi lại một chút mã trong trường hợp GridView để có được kết quả mong muốn. Trong file DropDown.aspx cần có đoạn mã sau đây: <asp:DropDownList Bài viết này sẽ chỉ cho bạn cách sử dụng điều khiển DropDownList trong DataGrid (hoặc GridView). Bạn sẽ cần thay đổi lại một chút mã trong trường hợp GridView để có được kết quả mong muốn. Trong file DropDown.aspx cần có đoạn mã sau đây: view plain print ? 1. <asp:DropDownList id="DropDownList1" runat="server" 2. DataSource="<%# GetCategory() %>" 3. DataTextField="CategoryName" 4. DataValueField="CategoryId" 5. SelectedIndex='<%# GetCategoryID((string)DataBinder.Eval(Container.DataItem, "Cate goryName")) %>' 6. /> Ở dòng thứ hai, chúng ta thiết lập DataSource của điều khiển DropDownList với hàm GetCategory(), hàm này sẽ lấy các bản ghi Category từ cơ sở dữ liệu và trả về một DataTable. Ở dòng cuối, chúng ta thiết lập SelectedIndex cho hàm GetCategory(), hàm này sẽ lấy CategoryName hiện thời như là đối số, và trả về location (một số nguyên) của CategoryName, điều này cho phép điều khiển DropDownList hiển thị đúng tên của bản ghi hiện thời. Đoạn mã C# như sau: view plain print ? 1. using System; 2. using System.Collections; 3. using System.ComponentModel; 4. using System.Data; 5. using System.Data.OleDb; 6. using System.Configuration; 7. using System.Drawing; 8. using System.Web; 9. using System.Web.SessionState; 10. using System.Web.UI; 11. using System.Web.UI.WebControls; 12. using System.Web.UI.HtmlControls; 13. namespace Management 14. { 15. 16. public class DropDown : System.Web.UI.Page 17. { 18. protected System.Web.UI.WebControls.DataGrid ProductGrid; 19. protected DataTable _category; 20. //new a database class to get records, ProductDb is a public class 21. //containing several functions 22. protected ProductDb pdb=new ProductDb(); 23. 24. public DropDown() 25. { 26. Page.Init += new System.EventHandler(Page_Init); 27. } 28. private void Page_Load(object sender, System.EventArgs e) 29. { 30. if(!IsPostBack) 31. { 32. BindProduct(); 33. 34. } 35. } 36. private void Page_Init(object sender, EventArgs e) 37. { 38. InitializeComponent(); 39. } 40. void BindProduct() 41. { 42. //pdb.GetProduct() returns a datatable to Product's datagrid 43. ProductGrid.DataSource=pdb.GetProduct(); 44. ProductGrid.DataBind(); 45. } 46. protected void Product_Edit(object sender, DataGridCommandEventArgs e) 47. { 48. 49. BindCategory(); 50. ((DataGrid)sender).EditItemIndex=e.Item.ItemIndex; 51. BindProduct(); 52. } 53. protected void Product_Cancel(object sender, DataGridCommandEventArgs e) 54. { 55. ProductGrid.EditItemIndex=-1; 56. BindProduct(); 57. } 58. protected void Product_Update(object sender, DataGridCommandEventArgs e) 59. { 60. //get the currnet product name 61. string pname=e.Item.Cell[1].Controls[0].Text; 62. //get the current product price 63. string price=e.Item.Cell[2].Controls[0].Text; 64. //get the current categoryid 65. DropDownList ddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1"); 66. string categoryid=ddl.SelectedItem.Value; 67. //get the current productid 68. string pid=e.Item.Cell[4].Controls[0].Text; 69. //call pdb's update function 70. pdb.update(pid,pname,price,categoryid); 71. 72. ProductGrid.EditItemIndex=-1; 73. BindProduct(); 74. 75. } 76. void BindCategory() 77. { 78. //pdb.FetchCategory() returns a datatable 79. _category=pdb.FetchCategory(); 80. 81. } 82. protected DataTable GetCategory() 83. { 84. return _category; 85. } 86. protected int GetCategoryID(string cname) 87. { 88. for(int i=0;i<_category.DefaultView.Count;i++) 89. { 90. if (_category.DefaultView<img ptech.edu.vn/emoticons/emotion- 55.gif" alt="Idea" />["categoryname"].ToString()==cname) 91. { 92. return i; 93. } 94. } 95. return 0; 96. 97. } 98. 99. #region Web Form Designer generated code 100. /// 101. /// Required method for Designer support - do not modify 102. /// the contents of this method with the code editor. 103. /// 104. private void InitializeComponent() 105. { 106. this.Load += new System.EventHandler(this.Page_Load); 107. } 108. #endregion 109. } 110. } Điểm quan trọng nhất trong file C# này là ở chỗ: - Trong hàm Product_Edit(), bạn phải gọi hàm BindCategory() để thiết lập DataTable _category trước tiên, và sau đó thiết lập EditItemIndex cho DataGrid, và cuối cùng là gọi hàm BindProduct(). Điều khiển DropDownList sẽ không hiển thị gì nếu như bạn đảo lại trật tự này. Bởi vì mỗi lần bạn thiết lập EditItemIndex, DataGrid sẽ cần diễn dịch lại các bản ghi, và lúc đó điều khiển DropDownList truy xuất vào phương thức GetCategory() để nhận về nguồn dữ liệu, nếu phương thức này không trả về dữ liệu gì thì đương nhiên bạn sẽ không nhận thấy gì hết. Ghi nhớ là: trước khi thiết lập EditItemIndex của DataGrid, hãy thiết lập DataDource của điều khiển. -Trong hàm Product_Update(), bạn không truy xuất trực tiếp điều khiển DropDownList nằm trong DataGrid được, giải pháp để lấy được giá trị được chọn trong điều khiển DropDownList là phương thức FindControl(). Hàm này lấy tên của điều khiển DropDownList làm đối số của nó, và sau đó trả về điều khiển DropDownList được tìm thấy để bạn có thể sử dụng điều khiển trả về đó để lấy ra giá trị chỉ số được chọn. Hãy ghi nhớ là: sử dụng hàm FindControl() để trả về bất cứ điều khiển nào mà bạn muốn tìm trong DataGrid như Text Box, Text Area, Label, Calendar . Sử dụng DropDownList trong DataGrid Cập nhật: 27/11/2008 với no comments Xếp trong: Lập trình, C#, Asp.Net Bài viết này sẽ chỉ cho bạn cách sử dụng điều khiển DropDownList trong DataGrid. chút mã trong trường hợp GridView để có được kết quả mong muốn. Trong file DropDown.aspx cần có đoạn mã sau đây: <asp :DropDownList Bài viết này sẽ chỉ cho bạn cách sử dụng điều khiển DropDownList. để bạn có thể sử dụng điều khiển trả về đó để lấy ra giá trị chỉ số được chọn. Hãy ghi nhớ là: sử dụng hàm FindControl() để trả về bất cứ điều khiển nào mà bạn muốn tìm trong DataGrid như Text

Ngày đăng: 12/07/2014, 15:20

Tài liệu cùng người dùng

Tài liệu liên quan