Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Websites with data entry systems can also use update forms. An Update Form queries the database on the server, filtering the data to retrieve just a single record, presents that data in a form, in this case presented with the DetailsView control, and then when the user has filled in the new data and clicked the appropriate link, the new data is sent back to the server in an update statement. You can also integrate update forms with the rest of the data entry system by linking to the form from a list page. For this demonstration I'll be working with the file AuthorUpdate.aspx. It begins where another video left off using a DetailsView control, which is currently configured for inserting data.
I'll start by clicking on the control and opening its smart tasks list. Now I'll disable Inserting and then instead I'll select the option Enable Editing. Then with the controls still selected I'll press F4 to go to the Properties view. In the Properties view, which I configured as a floating panel, I'll go to the DefaultMode property and change it from Insert to Edit. Notice that the default appearance of the form changes. Now it shows the primary key, but as a read only value only and also displays links at the bottom for updating and canceling the data. The next step is to modify the SqlDataSource component so that it retrieves only one record from the server-side database and filters for that record based on a primary key value.
I'll close the Properties view and then look at the file in Source view. I am going to go down to the declaration of the of the SqlDataSource component. I'll press Alt+Shift+Enter to go into Full Screen mode and then I'll take a look at the code for the Select command. The Select command is the command that retrieves data from the database on the server. The default select command that's created when you drag a table into a page, selects all the data from the table. In this case all of the data is being retrieved from the author's table.
I'll place the cursor at the end of the SQL statement and add a filtering expression Where and then the name of the column au_id wrapped in brackets. And then in equals operator and the name of a parameter starting with the @ character and then au_id. So I have created a filtering clause as part of the select statement. Now I need to tell the SqlDataSource object where to get the value for that parameter. To do that I'll place the cursor after the begin tag and before the existing parameters. In fact, I can delete some of these parameters. The Delete and Insert parameters won't be used in this page at any point. I'll be using the UpdateParameter. So I'll leave those in place. Then above the UpdateParameters, I'll create a new set of parameters called SelectParameters.
I will type in the less than character and then choose select parameters from the list, then put in the closing tag character and you will see Visual Web Developer completes the tag with an end tag. I'll press Enter a couple of times. Next, I need to indicate where the value of that parameter will come from. I'll start with asp: and then I'll select a QueryStringParamter. This means that I'll be getting the value from a parameter passed in as part of the URL of the page. I'll select asp: QueryStringParamter. Then I'll set the name attribute to match the name of the attribute in my SQL statement. Name = "au_id".
Next, I need to tell the command what the name of the QueryStringParamter will be what's passed in with the URL. So I'll add the following property. QueryStringField = au_id. Now the only reason I know that's the name of the parameter is because I'm going to be creating it in the last step of the exercise. And finally, I add the Type attribute, which is used to indicate what the implied data type of the value is. And I'll put in Int32 matching the data type that's used in other parameters for the same column. And then I'll close the tag with slash and the greater than.
So I have modified the select command to include the Where clause, I have used a parameter for the filter value and then I have filled in the parameter from a value passed in with the URL, known here as the QueryStringParamter. I'll save and test the page. When I first run the page, it shows up with no data. That's because when I run the page, the URL does not include the QueryStringParamter that I'm requesting. I'll place the cursor in the web browsers URL field and then at the end of the page, I'll add a question mark and then the QueryParamter au_id and I'll pass in a value of 2, meaning that I want to see the values for author number 2.
And you will see that when the form comes up, it shows the details for that record. I'll click up here again and I'll change it to a value of Author ID number 3 and I'll see another record show up. Now I'll go take a look at Author ID number 1. Marjorie Green and I'm going to change Marjorie Green to Marjorie Brown and click Update. Then I'll go through the author list until I find the record and show that it's been successfully updated. There is one more thing to know about how this page is working. In the pages starting stage it had a code file that already had an event handler function called DetailView_ ItemUpdate. The code for this function checks the affected rows property of the event object for a value of 1 and if it equals 1, it redirects back to the author's page. This matches the kind of code that was used in the Insert operation that I previously demonstrated. And back in the page, if I look at the DetailsView control I'll see that there is an OnItemUpdate at the event handler that's calling that function.
So the structure of the DetailsView control used for updating and the one that's used for inserting are pretty much the same. They are both using the event handlers for the updated or the inserted events depending on which operation is being used and they are both using the ItemCommand event to react to a click on the Cancel button. So that's how you create the form. In the final step, which I'll show in the next video, I'll show you how to get to this Form page from the List page and pass the QueryString property when the user clicks on a link or a button.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98677 Viewers
61 Video lessons · 85992 Viewers
71 Video lessons · 69843 Viewers
56 Video lessons · 102133 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.