PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSASPNETFormViewUpload/ReadMe.txt

#
Plain Text | 217 lines | 143 code | 74 blank | 0 comment | 0 complexity | 8f3962a1a05c5226294b4f240d9fcaaf MD5 | raw file
  1. ========================================================================
  2. ASP.NET APPLICATION : CSASPNETFormViewUpload Project Overview
  3. ========================================================================
  4. /////////////////////////////////////////////////////////////////////////////
  5. Use:
  6. This CSASPNETFormViewUpload sample demonstrates how to display and upload
  7. images in an ASP.NET FormView control and how to implement Insert, Edit,
  8. Update, Delete and Paging functions in the control.
  9. This project includes two pages: Default and Image.
  10. Default populates a FormView control with data from a SQL Server database
  11. and provides UI for data manipulation.
  12. Image is used to retrieve the image from a SQL Server database and display
  13. it in the Web page.
  14. /////////////////////////////////////////////////////////////////////////////
  15. Creation:
  16. Step1. Create a C# ASP.NET Web Application named CSASPNETFormViewUpload in
  17. Visual Studio 2008 / Visual Web Developer.
  18. Step2. Drag a FormView control into the Default page.
  19. (1) Rename the FormView to fvPerson.
  20. (2) In the Source view, copy and paste the markup of following three
  21. templates from the sample:
  22. ItemTemplate: render the particular record displayed in the FormView.
  23. EditItemTemplate: customize the editing interface for the FormView.
  24. InsertItemTemplate: customize the inserting interface for the FormView.
  25. Related references:
  26. ASP.NET: Using the FormView's Templates
  27. MSDN: FormView Class
  28. MSDN: Image Class
  29. Step3. Copy the following methods from the sample,and paste them in the
  30. code-behind of Default page:
  31. Page_Load Method:
  32. Initialize underlying objects, when the Page is accessed
  33. for the first time.
  34. BindFormView Method:
  35. Bind the FormView control with data from a SQL Server table.
  36. Related reference:
  37. MSDN: using Statement (C# Reference)
  38. Step4. Navigate to the Property panel of fvPerson and then switch to Event.
  39. Double-click on the following events to generate the Event handlers.
  40. After that, fill the generated methods with the sample code.
  41. (1) ModeChanging Event: Occurs when the FormView control switches
  42. between edit, insert, and read-only mode, but before the mode changes.
  43. In this event, we need to switch FormView control to the new mode and
  44. then rebind the FormView control to show data in new mode.
  45. ////////////////////////////////////////////////////////////////
  46. fvPerson.ChangeMode(e.NewMode);
  47. BindFormView();
  48. ////////////////////////////////////////////////////////////////
  49. Related reference:
  50. MSDN: FormView.ModeChanging
  51. (2) PageIndexChanging Event: Occurs when a pager button within the
  52. control is clicked.
  53. In order to show data in the new page, we need to set the index of new
  54. page and then rebind the FormView control.
  55. ////////////////////////////////////////////////////////////////
  56. fvPerson.PageIndex = e.NewPageIndex;
  57. BindFormView();
  58. ////////////////////////////////////////////////////////////////
  59. Related reference:
  60. MSDN: FormView.PageIndexChanging Event
  61. (3) ItemInserting Event: Occurs when an Insert button within a FormView
  62. control is clicked, but before the insert operation.
  63. After clicking Insert button, we need to get the first name, last name
  64. and specified image file (bytes) from the InsertItemTemplate of the
  65. FormView control.
  66. ////////////////////////////////////////////////////////////////
  67. string strLastName = ((TextBox)fvPerson.Row.FindControl("tbLastName")).Text;
  68. string strFirstName = ((TextBox)fvPerson.Row.FindControl("tbFirstName")).Text;
  69. cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = strLastName;
  70. cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = strFirstName;
  71. FileUpload uploadPicture = (FileUpload)fvPerson.FindControl("uploadPicture");
  72. if (uploadPicture.HasFile)
  73. {
  74. cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
  75. }
  76. else
  77. {
  78. cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
  79. }
  80. ////////////////////////////////////////////////////////////////
  81. Related reference:
  82. MSDN: FormView.ItemInserting Event
  83. (4) ItemUpdating Event: Occurs when an Update button within a FormView
  84. control is clicked, but before the update operation.
  85. After clicking Update button, we need to get and pass the person ID,
  86. first name, last name and specified image file (bytes) from the
  87. EditItemTemplate of the FormView control.
  88. ////////////////////////////////////////////////////////////////
  89. string strPersonID = ((Label)fvPerson.Row.FindControl("lblPersonID")).Text;
  90. ////////////////////////////////////////////////////////////////
  91. Related reference:
  92. MSDN: FormView.ItemUpdating Event
  93. (5) ItemDeletingEvent: Occurs when a Delete button within a FormView
  94. control is clicked, but before the delete operation.
  95. We get the PersonID from the ItemTemplate of the FormView control and
  96. then delete the person record in the database based on the PersonID.
  97. ////////////////////////////////////////////////////////////////
  98. string strPersonID = ((Label)fvPerson.Row.FindControl("lblPersonID")).Text;
  99. ////////////////////////////////////////////////////////////////
  100. Related reference:
  101. MSDN: FormView.ItemDeleting Event
  102. Step5. Create a new Web page named Image in the project. Copy the Page_Load
  103. method from the sample, and paste it in code-behind of Image page:
  104. In this page, we retrieve the image data from the database, convert it to a
  105. bytes array and then write the array to the HTTP output stream
  106. to display the image.
  107. ////////////////////////////////////////////////////////////////
  108. Byte[] bytes = (byte[])cmd.ExecuteScalar();
  109. if (bytes != null)
  110. {
  111. Response.ContentType = "image/jpeg";
  112. Response.BinaryWrite(bytes);
  113. Response.End();
  114. }
  115. ////////////////////////////////////////////////////////////////
  116. Related references:
  117. MSDN: Request Object
  118. MSDN: Response Object
  119. /////////////////////////////////////////////////////////////////////////////
  120. References:
  121. ASP.NET: Using the FormView's Templates
  122. http://www.asp.net/learn/data-access/tutorial-14-cs.aspx
  123. MSDN: Image Class
  124. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.aspx
  125. MSDN: FormView Class
  126. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.aspx
  127. MSDN: using Statement (C# Reference)
  128. http://msdn.microsoft.com/en-us/library/yh598w02.aspx
  129. MSDN: FormView.ModeChanging
  130. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.modechanging.aspx
  131. MSDN: FormView.PageIndexChanging Event
  132. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.pageindexchanging.aspx
  133. MSDN: FormView.ItemInserting Event
  134. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserting.aspx
  135. MSDN: FormView.ItemUpdating Event
  136. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemupdating.aspx
  137. MSDN: FormView.ItemDeleting Event
  138. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemdeleting.aspx
  139. MSDN: Request Object
  140. http://msdn.microsoft.com/en-us/library/ms524948.aspx
  141. MSDN: Response Object
  142. http://msdn.microsoft.com/en-us/library/ms525405.aspx
  143. /////////////////////////////////////////////////////////////////////////////