GridView and TemplateField Help

Discussion in 'ASP .Net' started by Jonathan Wood, May 1, 2008.

  1. 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.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
     
    Jonathan Wood, May 1, 2008
    #1
    1. Advertisements

  2. Set the GridView's DataKeyNames attribute value to activityId.

    --
    ---------------------------------------------------
    S.M. Altaf [MVP]
    http://www.mendhak.com/


    "Jonathan Wood" <> wrote in message
    news:Owocv$...
    > 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.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
     
    S.M. Altaf [MVP], May 1, 2008
    #2
    1. Advertisements

  3. 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


    "S.M. Altaf [MVP]" <> wrote in message
    news:...
    > Set the GridView's DataKeyNames attribute value to activityId.
    >
    > --
    > ---------------------------------------------------
    > S.M. Altaf [MVP]
    > http://www.mendhak.com/
    >
    >
    > "Jonathan Wood" <> wrote in message
    > news:Owocv$...
    >> 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.
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>
     
    Jonathan Wood, May 1, 2008
    #3
  4. 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.

    --
    ---------------------------------------------------
    S.M. Altaf [MVP]
    http://www.mendhak.com/


    "Jonathan Wood" <> wrote in message
    news:...
    > 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
    >
    >
    > "S.M. Altaf [MVP]" <> wrote in message
    > news:...
    >> Set the GridView's DataKeyNames attribute value to activityId.
    >>
    >> --
    >> ---------------------------------------------------
    >> S.M. Altaf [MVP]
    >> http://www.mendhak.com/
    >>
    >>
    >> "Jonathan Wood" <> wrote in message
    >> news:Owocv$...
    >>> 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.
    >>>
    >>> --
    >>> Jonathan Wood
    >>> SoftCircuits Programming
    >>> http://www.softcircuits.com
    >>>

    >
     
    S.M. Altaf [MVP], May 1, 2008
    #4
  5. 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


    "S.M. Altaf [MVP]" <> wrote in message
    news:...
    > 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.
    >
    > --
    > ---------------------------------------------------
    > S.M. Altaf [MVP]
    > http://www.mendhak.com/
    >
    >
    > "Jonathan Wood" <> wrote in message
    > news:...
    >> 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
    >>
    >>
    >> "S.M. Altaf [MVP]" <> wrote in message
    >> news:...
    >>> Set the GridView's DataKeyNames attribute value to activityId.
    >>>
    >>> --
    >>> ---------------------------------------------------
    >>> S.M. Altaf [MVP]
    >>> http://www.mendhak.com/
    >>>
    >>>
    >>> "Jonathan Wood" <> wrote in message
    >>> news:Owocv$...
    >>>> 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.
    >>>>
    >>>> --
    >>>> Jonathan Wood
    >>>> SoftCircuits Programming
    >>>> http://www.softcircuits.com
    >>>>

    >>
     
    Jonathan Wood, May 1, 2008
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lupina
    Replies:
    3
    Views:
    27,286
    =?Utf-8?B?RWx0b24gVw==?=
    Mar 1, 2006
  2. Replies:
    2
    Views:
    16,079
  3. sloan
    Replies:
    2
    Views:
    4,972
    aryan_subhash
    Oct 17, 2007
  4. mesut
    Replies:
    0
    Views:
    643
    mesut
    Jun 29, 2007
  5. Rafia Tapia
    Replies:
    1
    Views:
    491
    Masudur
    Jul 29, 2007
Loading...

Share This Page