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