download.microsoft.com/download/f/d/1/...
e=arial size=-1 color=blue>
« back to results for ""
Below is a cache of http://download.microsoft.com/download/f/d/1/fd129d3f-1fa0-4c4c-ad61-e9ae0d6ab4fe/DataAccessDemo6Transcript.doc. It's a snapshot of the page taken as our search engine crawled the Web.
The web site itself may have changed. You can check the current page or check for previous versions at the Internet Archive.
Yahoo! is not affiliated with the authors of this page or responsible for its content.
Lets close out this session on data access in ASP.NET
by looking at a final example, a page named EditMasterDetail.aspx.
This is basically the same page that you saw in the previous demo, one
that uses a grid view and a details view to create a master detail view
of the employees table of the Northwind database. This one throws
in editing capabilities to demonstrate two-way data binding.
As before, I can select
a record by clicking this Select button here. But theres some
additional things that I can do. For example, I can click a Delete
button to delete one of these records. Notice that when I click
that Delete button a message box pops up asking for confirmation.
Thats not a feature that is built into the grid view, thats a feature
that I have added. I added that because youre asked to include
a feature very much like this in the application that you will be building.
In just a few moments Ill show you what I had to do to get that confirmation
box to appear, and youll see that it wasnt much.
Ill click Cancel because
I dont care to delete that record. Now, lets edit one of these
records. Grid view has editing support built into it. Doesnt
take any code to make this happen, although you can add code if youd
like. Now, if I want to change Stevens name to Mike like this,
I can make the change, click Update, and the change gets propagated
back to the database courtesy of the grid view control and the SQL data
source that its using to do its data binding.
Now, in addition, if you
scroll down, notice that the details view has some editing capabilities
of its own. For example, I can tell it to include an Edit link.
If I click that link then the details view clicks into an editable view
like this. If I click the Delete link then it will delete that
record from the database. If I click the New link it shows me
a blank input form allowing me to add a new record.
Significantly again, I
havent written any code to make this happen. Youre seeing a
very good demonstration here of the capabilities built into the details
view control. Now, lets take a quick look at the source here
to see what it is that allows this two-way data binding to work.
Theres several key elements that I need you to see.
Here is that source view.
First, heres the SQL data source control that provides content to the
grid view at the top of the page. In a previous demo, the SelectCommand
property of that SQL data source was initialized, but the UpdateCommand
and DeleteCommand properties were not. The first step in doing
two-way data binding is to initialize the InsertCommand, UpdateCommand,
and DeleteCommand properties of the data source control. Now,
I didnt initialize InsertCommand here, because Im not allowing this
grid view control to do inserts. Because I am allowing it to do
Updates and Deletes, I must define those properties. Property
values tell the SQL data source exactly what command to execute.
An UpdateCommand, for example, or a DeleteCommand if the control is
asked to perform an update on the database or a delete on the database.
Now, in almost all cases
when you update or delete, the commands that you use will require parameters,
hence, the UpdateParameters element that you see right here and the
DeleteParameters element that are inside the SQL data source control.
These simply map the parameters being passed by name, parameters like
@LastName, being passed, in this case, to the UpdateCommand to the corresponding
fields in the grid view that we are binding to.
Lets go down to the second
SQL data source. This is the one that serves the details view
control. Im allowing the details view to do Inserts, Updates,
and Deletes, which is why Ive initialized all three of those properties
in that control, InsertCommand, UpdateCommand, and DeleteCommand.
In addition, you can see
the InsertParameters, UpdateParameters, and DeleteParameters, which,
again, are telling the SQL data source where to get the values for the
input parameters input to the commands from. Again, these asp:parameter
elements just map those parameters being defined in the commands back
to the equivalently named fields and the control that the data source
is bound to.
I had to make some small
changes to the grid view and details view itself to support two-way
data binding. For example, in the grid view tag if I scroll out
here, youll see that I now have properties being initialized.
Properties named AutoGenerateEditButton and AutoGenerateDeleteButton.
Setting both of those to True causes the Edit and Delete buttons that
you saw in the data grid, or in the grid view on the page to appear.
Same with the details view
control. Here is the details view tag declaring that control.
If youll scroll out some, youll notice that Ive set its AutoInsertButton
proper- AutoGenerateInsertButton property to True, its AutoDelete-
AutoGenerateDeleteButton, I should say, property to True. I
should find, yes, an AutoGenerateEditButton property set to True also.
Thats why you saw links at the bottom of the details view allowing
us to edit, to delete, and to insert.
Its also very important
when youre using controls, like grid views and details views to do
two-way data binding that you specify the primary key or keys, being
used to do the inserts, updates, and deletes with the DataKeyNames property.
For example, right here Ive set DataKeyNames equal to EmployeeID.
That is the primary key in the table that Im getting this data from
and putting the data back to. If you look inside the details view
tag, youll see that Ive also defined that primary key there.
Thats important, because I am allowing editing to occur from inside
those controls.
That is a quick look at
what it takes to do two-way data binding. I think youll agree
that its not much, especially if youve done two-way data binding before
in Version 1.0 of ASP.NET where quite a bit more code was required to
do this.
There is one other thing
I want to mention to you. Lets go back to the page. Im
going to run it more- one more time. Im going to select one of
these records. Actually, there are two things I want to mention.
Before I do this one let me show you something else. I promised
that I would show you how it is that Im causing that confirmation dialogue
to appear when the user clicks one of the Delete links. As I said,
the grid view doesnt have the ability on its own to display that message
box, but it doesnt take much code to add that.
Notice in my grid view
tag here, I have an attribute that reads OnRowCreated = GridView1_RowCreated.
Row created is an event that the grid view fires for each and every
row that it renders out. If my grid view has a hundred rows, then
each time the page is requested the RowCreated event will fire 100 times.
With this attribute Im telling the grid view that each time it creates
a row I want this method called. If we look at that method youll
see how there Im adding in some Client-side script to that row, such
that when the button is clicked, the Delete button, that is, my code
will be activated.
Here is that method here.
Remember, this is called for each and every row that the grid view creates.
I first look at the row type to make sure that its a data row and not,
say, a header row or a footer row. Ultimately then, I reach into
the leftmost cell of the row that was just rendered out, pull out a
reference to control number 2, which happens to be that link that reads
Delete. I attach a DHTML onclick attribute to it. That
means that when a Delete link is clicked, this little bit of Java script
right here is going to execute, and it is the confirm function that
causes the message box to pop up.
With that, notice Im returning
the value that return confirmed- that confirm returned. If confirm
returns False indicating that the user does not want to delete that
record, then this function returns False, and the browser itself suppresses
the post-back that would otherwise have occurre