GridView and TemplateField Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Wood

    GridView and TemplateField Help

    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:TemplateFi eld ShowHeader="Fal se">
    <EditItemTempla te>
    <asp:LinkButt on ID="LinkButton1 " runat="server" CausesValidatio n="True"
    CommandName="Up date" Text="Update"></asp:LinkButton>
    &nbsp;<asp:Link Button ID="LinkButton2 " runat="server"
    CausesValidatio n="False"
    CommandName="Ca ncel" Text="Cancel"></asp:LinkButton>
    </EditItemTemplat e>
    <ItemTemplate >
    <asp:LinkButt on ID="LinkButton1 " runat="server"
    CausesValidatio n="False"
    CommandName="Ed it" Text="Edit"></asp:LinkButton>
    &nbsp;<asp:Link Button ID="lnkDelete" runat="server"
    CausesValidatio n="false"
    CommandName="De lete" Text="Delete"
    OnClientClick=" return confirm('Are you sure you want to delete this
    record?');" />
    </ItemTemplate>

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

    <asp:ObjectData Source ID="ObjectDataS ource1" runat="server"
    DeleteMethod="D eleteActivity" InsertMethod="I nsertActivity"
    SelectMethod="G etActivities" TypeName="SoftC ircuits.MediCor p.Activities"
    UpdateMethod="U pdateActivity">
    <DeleteParamete rs>
    <asp:Paramete r Name="activityI d" Type="Int32" />
    </DeleteParameter s>
    <UpdateParamete rs>
    ...
    </UpdateParameter s>
    <SelectParamete rs>
    ...
    </SelectParameter s>
    <InsertParamete rs>
    ...
    </InsertParameter s>
    </asp:ObjectDataS ource>

    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.Me diCorp.Activiti es does in fact get called. However,
    the activityId argument is always 0.

    Can anyone help me understand where the ObjectDataSourc e 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


  • S.M. Altaf [MVP]

    #2
    Re: GridView and TemplateField Help

    Set the GridView's DataKeyNames attribute value to activityId.

    --
    ---------------------------------------------------
    S.M. Altaf [MVP]



    "Jonathan Wood" <jwood@softcirc uits.comwrote in message
    news:Owocv$6qIH A.3616@TK2MSFTN GP06.phx.gbl...
    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:TemplateFi eld ShowHeader="Fal se">
    <EditItemTempla te>
    <asp:LinkButt on ID="LinkButton1 " runat="server"
    CausesValidatio n="True"
    CommandName="Up date" Text="Update"></asp:LinkButton>
    &nbsp;<asp:Link Button ID="LinkButton2 " runat="server"
    CausesValidatio n="False"
    CommandName="Ca ncel" Text="Cancel"></asp:LinkButton>
    </EditItemTemplat e>
    <ItemTemplate >
    <asp:LinkButt on ID="LinkButton1 " runat="server"
    CausesValidatio n="False"
    CommandName="Ed it" Text="Edit"></asp:LinkButton>
    &nbsp;<asp:Link Button ID="lnkDelete" runat="server"
    CausesValidatio n="false"
    CommandName="De lete" Text="Delete"
    OnClientClick=" return confirm('Are you sure you want to delete this
    record?');" />
    </ItemTemplate>
    >
    In addition, the GridView control is bound to an ObjectDataSourc e object
    that contains the following:
    >
    <asp:ObjectData Source ID="ObjectDataS ource1" runat="server"
    DeleteMethod="D eleteActivity" InsertMethod="I nsertActivity"
    SelectMethod="G etActivities" TypeName="SoftC ircuits.MediCor p.Activities"
    UpdateMethod="U pdateActivity">
    <DeleteParamete rs>
    <asp:Paramete r Name="activityI d" Type="Int32" />
    </DeleteParameter s>
    <UpdateParamete rs>
    ...
    </UpdateParameter s>
    <SelectParamete rs>
    ...
    </SelectParameter s>
    <InsertParamete rs>
    ...
    </InsertParameter s>
    </asp:ObjectDataS ource>
    >
    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.Me diCorp.Activiti es does in fact get
    called. However, the activityId argument is always 0.
    >
    Can anyone help me understand where the ObjectDataSourc e 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

    >

    Comment

    • Jonathan Wood

      #3
      Re: GridView and TemplateField Help

      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



      "S.M. Altaf [MVP]" <smaltaf@mNOSPA Msn.comwrote in message
      news:7F95922D-65F7-415B-9100-AEA883217C34@mi crosoft.com...
      Set the GridView's DataKeyNames attribute value to activityId.
      >
      --
      ---------------------------------------------------
      S.M. Altaf [MVP]

      >
      >
      "Jonathan Wood" <jwood@softcirc uits.comwrote in message
      news:Owocv$6qIH A.3616@TK2MSFTN GP06.phx.gbl...
      >Greetings,
      >>
      >I have a GridView control. For a couple of reasons, I ended up with a
      >TemplateFiel d to contain my Edit and Delete links, something like this:
      >>
      > <asp:TemplateFi eld ShowHeader="Fal se">
      > <EditItemTempla te>
      > <asp:LinkButt on ID="LinkButton1 " runat="server"
      >CausesValidati on="True"
      > CommandName="Up date" Text="Update"></asp:LinkButton>
      > &nbsp;<asp:Link Button ID="LinkButton2 " runat="server"
      >CausesValidati on="False"
      > CommandName="Ca ncel" Text="Cancel"></asp:LinkButton>
      > </EditItemTemplat e>
      > <ItemTemplate >
      > <asp:LinkButt on ID="LinkButton1 " runat="server"
      >CausesValidati on="False"
      > CommandName="Ed it" Text="Edit"></asp:LinkButton>
      > &nbsp;<asp:Link Button ID="lnkDelete" runat="server"
      >CausesValidati on="false"
      > CommandName="De lete" Text="Delete"
      > OnClientClick=" return confirm('Are you sure you want to delete this
      >record?');" />
      > </ItemTemplate>
      >>
      >In addition, the GridView control is bound to an ObjectDataSourc e object
      >that contains the following:
      >>
      ><asp:ObjectDat aSource ID="ObjectDataS ource1" runat="server"
      > DeleteMethod="D eleteActivity" InsertMethod="I nsertActivity"
      > SelectMethod="G etActivities" TypeName="SoftC ircuits.MediCor p.Activities"
      > UpdateMethod="U pdateActivity">
      > <DeleteParamete rs>
      > <asp:Paramete r Name="activityI d" Type="Int32" />
      > </DeleteParameter s>
      > <UpdateParamete rs>
      > ...
      > </UpdateParameter s>
      > <SelectParamete rs>
      > ...
      > </SelectParameter s>
      > <InsertParamete rs>
      > ...
      > </InsertParameter s>
      ></asp:ObjectDataS ource>
      >>
      >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
      >DeleteActivi ty method of SoftCircuits.Me diCorp.Activiti es does in fact
      >get called. However, the activityId argument is always 0.
      >>
      >Can anyone help me understand where the ObjectDataSourc e 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
      >>

      Comment

      • S.M. Altaf [MVP]

        #4
        Re: GridView and TemplateField Help

        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.Reflecti on 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 ObjectDataSourc e being
        used with any arbitrary argument name given to the Delete method.

        --
        ---------------------------------------------------
        S.M. Altaf [MVP]



        "Jonathan Wood" <jwood@softcirc uits.comwrote in message
        news:OrGJRK8qIH A.4952@TK2MSFTN GP05.phx.gbl...
        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

        >
        >
        "S.M. Altaf [MVP]" <smaltaf@mNOSPA Msn.comwrote in message
        news:7F95922D-65F7-415B-9100-AEA883217C34@mi crosoft.com...
        >Set the GridView's DataKeyNames attribute value to activityId.
        >>
        >--
        >---------------------------------------------------
        >S.M. Altaf [MVP]
        >http://www.mendhak.com/
        >>
        >>
        >"Jonathan Wood" <jwood@softcirc uits.comwrote in message
        >news:Owocv$6qI HA.3616@TK2MSFT NGP06.phx.gbl.. .
        >>Greetings,
        >>>
        >>I have a GridView control. For a couple of reasons, I ended up with a
        >>TemplateFie ld to contain my Edit and Delete links, something like this:
        >>>
        >> <asp:TemplateFi eld ShowHeader="Fal se">
        >> <EditItemTempla te>
        >> <asp:LinkButt on ID="LinkButton1 " runat="server"
        >>CausesValidat ion="True"
        >> CommandName="Up date" Text="Update"></asp:LinkButton>
        >> &nbsp;<asp:Link Button ID="LinkButton2 " runat="server"
        >>CausesValidat ion="False"
        >> CommandName="Ca ncel" Text="Cancel"></asp:LinkButton>
        >> </EditItemTemplat e>
        >> <ItemTemplate >
        >> <asp:LinkButt on ID="LinkButton1 " runat="server"
        >>CausesValidat ion="False"
        >> CommandName="Ed it" Text="Edit"></asp:LinkButton>
        >> &nbsp;<asp:Link Button ID="lnkDelete" runat="server"
        >>CausesValidat ion="false"
        >> CommandName="De lete" Text="Delete"
        >> OnClientClick=" return confirm('Are you sure you want to delete this
        >>record?');" />
        >> </ItemTemplate>
        >>>
        >>In addition, the GridView control is bound to an ObjectDataSourc e object
        >>that contains the following:
        >>>
        >><asp:ObjectDa taSource ID="ObjectDataS ource1" runat="server"
        >> DeleteMethod="D eleteActivity" InsertMethod="I nsertActivity"
        >> SelectMethod="G etActivities"
        >>TypeName="Sof tCircuits.MediC orp.Activities"
        >> UpdateMethod="U pdateActivity">
        >> <DeleteParamete rs>
        >> <asp:Paramete r Name="activityI d" Type="Int32" />
        >> </DeleteParameter s>
        >> <UpdateParamete rs>
        >> ...
        >> </UpdateParameter s>
        >> <SelectParamete rs>
        >> ...
        >> </SelectParameter s>
        >> <InsertParamete rs>
        >> ...
        >> </InsertParameter s>
        >></asp:ObjectDataS ource>
        >>>
        >>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
        >>DeleteActivit y method of SoftCircuits.Me diCorp.Activiti es does in fact
        >>get called. However, the activityId argument is always 0.
        >>>
        >>Can anyone help me understand where the ObjectDataSourc e 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
        >>SoftCircuit s Programming
        >>http://www.softcircuits.com
        >>>
        >

        Comment

        • Jonathan Wood

          #5
          Re: GridView and TemplateField Help

          Yeah, it appears that, no matter what, the objectdatasourc e eventually is
          looking for an ID argument in the method.

          If I change the method argument name to xID, and also change the
          ObjectDataSourc e'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 uncharacteristi c 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



          "S.M. Altaf [MVP]" <smaltaf@mNOSPA Msn.comwrote in message
          news:986F012A-E2FC-4A13-9050-238145D75E39@mi crosoft.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.Reflecti on 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 ObjectDataSourc e being
          used with any arbitrary argument name given to the Delete method.
          >
          --
          ---------------------------------------------------
          S.M. Altaf [MVP]

          >
          >
          "Jonathan Wood" <jwood@softcirc uits.comwrote in message
          news:OrGJRK8qIH A.4952@TK2MSFTN GP05.phx.gbl...
          >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]" <smaltaf@mNOSPA Msn.comwrote in message
          >news:7F95922 D-65F7-415B-9100-AEA883217C34@mi crosoft.com...
          >>Set the GridView's DataKeyNames attribute value to activityId.
          >>>
          >>--
          >>---------------------------------------------------
          >>S.M. Altaf [MVP]
          >>http://www.mendhak.com/
          >>>
          >>>
          >>"Jonathan Wood" <jwood@softcirc uits.comwrote in message
          >>news:Owocv$6q IHA.3616@TK2MSF TNGP06.phx.gbl. ..
          >>>Greetings,
          >>>>
          >>>I have a GridView control. For a couple of reasons, I ended up with a
          >>>TemplateFiel d to contain my Edit and Delete links, something like this:
          >>>>
          >>> <asp:TemplateFi eld ShowHeader="Fal se">
          >>> <EditItemTempla te>
          >>> <asp:LinkButt on ID="LinkButton1 " runat="server"
          >>>CausesValida tion="True"
          >>> CommandName="Up date" Text="Update"></asp:LinkButton>
          >>> &nbsp;<asp:Link Button ID="LinkButton2 " runat="server"
          >>>CausesValida tion="False"
          >>> CommandName="Ca ncel" Text="Cancel"></asp:LinkButton>
          >>> </EditItemTemplat e>
          >>> <ItemTemplate >
          >>> <asp:LinkButt on ID="LinkButton1 " runat="server"
          >>>CausesValida tion="False"
          >>> CommandName="Ed it" Text="Edit"></asp:LinkButton>
          >>> &nbsp;<asp:Link Button ID="lnkDelete" runat="server"
          >>>CausesValida tion="false"
          >>> CommandName="De lete" Text="Delete"
          >>> OnClientClick=" return confirm('Are you sure you want to delete
          >>>this record?');" />
          >>> </ItemTemplate>
          >>>>
          >>>In addition, the GridView control is bound to an ObjectDataSourc e
          >>>object that contains the following:
          >>>>
          >>><asp:ObjectD ataSource ID="ObjectDataS ource1" runat="server"
          >>> DeleteMethod="D eleteActivity" InsertMethod="I nsertActivity"
          >>> SelectMethod="G etActivities"
          >>>TypeName="So ftCircuits.Medi Corp.Activities "
          >>> UpdateMethod="U pdateActivity">
          >>> <DeleteParamete rs>
          >>> <asp:Paramete r Name="activityI d" Type="Int32" />
          >>> </DeleteParameter s>
          >>> <UpdateParamete rs>
          >>> ...
          >>> </UpdateParameter s>
          >>> <SelectParamete rs>
          >>> ...
          >>> </SelectParameter s>
          >>> <InsertParamete rs>
          >>> ...
          >>> </InsertParameter s>
          >>></asp:ObjectDataS ource>
          >>>>
          >>>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
          >>>DeleteActivi ty method of SoftCircuits.Me diCorp.Activiti es does in fact
          >>>get called. However, the activityId argument is always 0.
          >>>>
          >>>Can anyone help me understand where the ObjectDataSourc e 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
          >>>SoftCircui ts Programming
          >>>http://www.softcircuits.com
          >>>>
          >>

          Comment

          Working...