GridView and TemplateField Help

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

Greetings,

I have a GridView control. For a couple of reasons, I ended up with a
TemplateField to contain my Edit and Delete links, something like this:

<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
&nbsp;<asp:LinkButton ID="LinkButton2" runat="server"
CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
&nbsp;<asp:LinkButton ID="lnkDelete" runat="server"
CausesValidation="false"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this
record?');" />
</ItemTemplate>

In addition, the GridView control is bound to an ObjectDataSource object
that contains the following:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DeleteMethod="DeleteActivity" InsertMethod="InsertActivity"
SelectMethod="GetActivities" TypeName="SoftCircuits.MediCorp.Activities"
UpdateMethod="UpdateActivity">
<DeleteParameters>
<asp:Parameter Name="activityId" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
...
</UpdateParameters>
<SelectParameters>
...
</SelectParameters>
<InsertParameters>
...
</InsertParameters>
</asp:ObjectDataSource>

I'm trying to understand how some of this information hooks up. When I click
the delete link (and click OK in the confirmation), the DeleteActivity
method of SoftCircuits.MediCorp.Activities does in fact get called. However,
the activityId argument is always 0.

Can anyone help me understand where the ObjectDataSource should be getting
this argument value from such that it would represent the id of the record
on the row for which the Delete link was clicked?

The activityId represents the primary key of the table being displayed.

Thanks.
 
Thanks. I seemed to be able to get this working, but I'm still not fully
clear on how this is setup.

The underlying name of the table's primary key is ID. If I set the
DataKeyNames property to ID, then I get the error "could not find a
non-generic method 'DeleteActivity' that has parameters: activityId, ID."

So I then change the delete parameter name to just ID. And I then get the
error "could not find a non-generic method 'DeleteActivity' that has
parameters: ID."

Finally, I change the name of the method's argument to ID and it does work.

But so did the DataKeyNames property specify the name of the database's
primary key, the delete parameter name, the method's argument name, or all
three? If all three, as appears to be the case, is there no way to have the
method argument name be different from the actual name of the table's
primary key column?

Just trying to understand this better.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
 
The DataKeyNames property of the GridView tells it which fields in the data
that it's dealing with are the primary key fields. I had assumed that yours
was activityID, but it appears that yours is ID.

As for the second question, I can't really tell why changing the method
argument name made any difference at all, because it shouldn't. Do you
continue to get errors if you change the method argument name to something
else? If yes, then the only slightly reasonable explanation that I can
think of is the usage of System.Reflection by the GridView to invoke the
Delete method, wherein it specifies the argument value as well as name,
based upon what it has been passed in the DataKeyNames property.

Having said that, I have still seen examples of the ObjectDataSource being
used with any arbitrary argument name given to the Delete method.
 
Yeah, it appears that, no matter what, the objectdatasource eventually is
looking for an ID argument in the method.

If I change the method argument name to xID, and also change the
ObjectDataSource's delete parameter name to xID, then I get the error that
the method could not be found that takes the arguments xID, ID.

This seems uncharacteristic of .NET and something I'm having trouble feeling
comfortable with. Again, it is working. But it's still not quite sitting
right with me.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
 
Back
Top