Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In ASP.NET websites that include data entry forms it's useful to be able to validate form entry. When user's type values into data entry forms, you can use ASP.NET validator controls that are bound to the data entry form controls. You can then set custom rules on these validator controls and cause error messages to be displayed without extensive programming. For the demonstrations in this chapter, I'll use a website that's part of the exercise files. If you have access to the exercise files, select File > Open Web site. Go to the Validation folder under Ch09Validation and open it. Then open the file TitleInsert.aspx.
In the starting version of this page, there is a Details View control that's bound to an SqlDataSource. There are four fields that are being accepted in the form right now, the Title, Price, Publication Date, and Notes. I will run the page in the browser and I'll type some values into the controls and show you what can go wrong. I'll click into the Title and type any value, 'A title,' and then I'll type a nonnumeric value in as the price, just xyz. And I'll do the same thing for the PubDate and I'll click the Insert link. And in response I get a server error.
The key error here is at the top. There was an attempt to take a string and format it as a number, and because I, as the user didn't type-in a numeric value, the page fails. So to prevent that issue from happening, we need to capture the problem before the data is sent to the server. In web environments, validation is handled in two possible ways either with client-side validation or server-side validation. In server-side validation, data is sent to the server and then logic is applied to the data at the server level, and then if there is a problem, an error message is sent back to the webpage where its refreshed.
In client-side validation, the validation logic is handled entirely by the client. That's the kind of validation that's used in ASP.NET. Before you can apply validators in a DetailsView control, you first have to replace any instances of the BoundField component with Template fields. A Template field allows you to apply your own specific controls including textboxes, pull-down lists, and most importantly here the validator controls. I will prepare the form for validation like this. I'll select the DetailsView control, then I'll open the Task list and select Edit Fields. I'm going to validate two controls, the Price and the Publication Date. First, I'll click on the Price and the Selected fields list and I'll delete it. And then I'll do the same thing for the PubDate.
Now I'll add two Template fields. I'll go to the Available field list at the top. I'll choose TemplateField and click Add. For the first template field, I'll then go to HeaderText and put in the old HeaderText for the Price field of Price. Then I'll click on the item again and click the Up Arrow a couple of times to move it back up into place. Then I'll do the same thing for the PubDate. I'll add a new TemplateField, I'll set its HeaderText to match the old one and then I'll move it back up into place. So that my selected fields are in the same order, then I'll click OK.
You will see in the DetailsView control presented in Design View that there are no longer textboxes in the data entry form. I have to replace those now with explicit templating. So the next step is to go into Source View and start to code. I will click on the Source button and then press Alt+Shift+Enter to go into full screen, and then I'll locate my new TemplateFields. Here is the first one. Notice that the TemplateField is created with a pair of tags. Place the cursor between the tags and press Enter a couple of times to make some space. Within the TemplateField, I'll declare an EditItemTemplate as a pair of tags. This is the template that will be presented when the Details View control is in Edit mode.
Now within the EditItemTemplate, I'll declare an asp: TextBox. I'll give it an ID matching the ID of the old BoundField, and also matching the name of the database column on the server, price, I'll put in the essential runat = "server" attribute. And then just in case this form is going to be used to present existing data, I'll add a Text attribute and I'll use this binding expression. I'll start off with the less than and a percent and then within the tokens of pound sign and then I call to the Bind function. And within the Bind function I'll pass in the name of the database column, price. Finally, at the end of the textbox, I'll close the tag.
Now I'll save and run the page again. I'll select Debug > Start Without Debugging and when the data entry form opens, I'll see my new textbox which is a part of the editing template. Now I'll go back to the code and do the same thing for the Publication Date. I'll select the EditItemTemplate that I created for the price and then I'll copy that to the Clipboard. The code will be almost exactly the same for the PubDate. I'll click into the tags for the TemplateField for the PubDate, then I'll go to the new version of the TextBox control and I'll set both the ID and the database column name in the Bind function to the original name of the column PubDate.
Now I'll save and fully test the page. I'll press Ctrl+S to save and then run the page, and then I'll type-in some values. The title will be My New Book, I'll type-in a numeric value this time, a 15, a well-formed date of 3/1/2009 and Some Notes. Then I'll click the Insert link. You will see that the form is simply refreshed with new blank values. Now I'll go to the Database Explorer, I'll go back to Visual Web Developer and exit Full Screen. Go to the Database Explorer, open the Tables list, right-click on the Titles table and select Show Table Data, scroll down to the bottom of the data and show that my new record was successfully inserted.
So all this is in preparation for validation. Before you can apply validator controls to a data entry form that's presented within a Details View component, you must modify your Details View to use the TemplateField instead of the BoundField for any control that you want to validate.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97355 Viewers
61 Video lessons · 84610 Viewers
71 Video lessons · 68810 Viewers
56 Video lessons · 101280 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.