This article was dragged on for too long. It was originally intended to be called FormView. Later, I thought the content was not entirely related to FormView, and I changed it to the current name. When ASP. net mvc is popular, is it a kind of resistance?
The first article is too basic and I am not very interested in sending it to the homepage. You can go here to see: ASP. NET: Like Web Form, or drag controls (1)
Let's get down to the truth, and the book goes back. The previous article demonstrated how to use VS to automatically generate the form to be submitted. Here we will consider a complicated situation, including the form with uploaded images and displaying the images saved in the database in WebForm.
Recall this database table:
Commodity Product
Id |
Name |
Company_id |
1 |
Steamed Stuffed bun |
3 |
2 |
Steamed dumplings |
3 |
3 |
Talent |
1 |
4 |
MVP |
1 |
5 |
Window 7 |
2 |
Since it is a product, it is no longer a normal situation to configure images for each product. For similar requirements, many people prefer to store images together and use a GUID as the file name, the GUID of the image file stored in the database. However, adding, deleting, modifying, and querying backups become very difficult, which is too troublesome! Is there a better way?
In fact, most databases use the Image field type. We can store images in the database. The expected table structure is as follows:
Id |
Name |
Company_id |
Image |
1 |
Steamed Stuffed bun |
3 |
[Bin] |
2 |
Steamed dumplings |
3 |
[Bin] |
3 |
Talent |
1 |
[Bin] |
4 |
MVP |
1 |
[Bin] |
5 |
Window 7 |
2 |
[Bin] |
Table creation:
Follow the previous method to create a FormView from the data table and set the Default Mode to Insert. The result is as follows:
Okay, we automatically generate a data insertion form, but unfortunately, the image field generates a TextBox, it is obviously unrealistic to expect users to copy the image content and input it into the text box. We want to submit the image through the upload method, so let's edit the Insert template and use the upload control.
This is the data binding method of TextBox. The Text of TextBox is bound to the image field.
Unfortunately, when we put a FileUpload here, only two Bindable attributes appear. Even if Show all properties is selected, there is no desired attribute.
Is it feasible? Of course not! We can also directly change the ASP. NET code. Find the corresponding code first:
Then bind the FileBytes attribute to the image field:
<Asp: FileUpload ID = "imageFileUpload" runat = "server" FileBytes = '<% # Bind ("image") %>'/>
Then let's run the program and see, insert a new product type as planned: Steamed Stuffed bun.
Click "run" and we get an error: the SQL variable type does not match the field.
In fact, we only need to delete the image parameter type restrictions in the Data source:
Now, the data seems to be correctly inserted.
Now, we have successfully saved images to the database, but we still need to confirm whether the images are saved. Besides, it is easy to store the images and it is difficult to display them.
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page