GridView & DetailsView - Wrong Record Being Sent Over

Discussion in 'ASP .Net' started by jeffmagill@gmail.com, May 10, 2006.

  1. Guest

    Hi Everybody,

    I'm really hoping that someone can help me out because I have spent too
    much time on this project already!

    Basically, I have a DetailsView that handles all the Edits for a
    GridView - each row has an edit button and clicking on this should
    trigger that record to be loaded into the DetailsView for editing. The
    problem is that the record never gets loaded into the DetailsView.
    Instead, the DetailsView always and only contains the first record of
    the table. My guess is that something is wrong with the
    CommandName='Select' column in the GridView, but I'm hopeless.

    Both objects share the same asp:ObjectDataSource,
    AcronymObjectDataSource, which pulls from a TableAdapter which accesses
    a SQL database.

    Here's the relevant code that I can think of with some nice 'X'
    dividers to separate the objects. Obviously they arent in the original
    aspx file but this is basically how the aspx file appears.

    By the way, the DB is
    id int, (primary key)
    term varchar 20,
    definition varchar 500,
    featured bit

    Thanks in advance!!!

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    <asp:GridView ID="AllAcronymsGridView" runat="server"
    AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
    DataKeyNames="id" DataSourceID="AcronymObjectDataSource" PageSize="33">
    <Columns>
    <asp:BoundField DataField="term" HeaderText="Term"
    SortExpression="term" />
    <asp:BoundField DataField="definition" HeaderText="Definition"
    SortExpression="definition" />
    <asp:CheckBoxField DataField="featured" HeaderText="Featured"
    SortExpression="featured" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="btnSelect" runat="server"
    CausesValidation="False" CommandName="Select" Text="Edit"
    OnClick="btnSelect_Click"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="btnDelete" runat="server"
    CausesValidation="False" CommandName="Delete" Text="Delete"
    OnClientClick="return confirm('Are you sure you want to delete this
    record?');"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="id" HeaderText="id"
    InsertVisible="False" ReadOnly="True" SortExpression="id" />
    </Columns>
    <PagerSettings Position="TopAndBottom" />
    </asp:GridView>

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateRows="False" DataKeyNames="id"
    DataSourceID="AcronymObjectDataSource" Height="50px" Width="100%"
    DefaultMode="Edit" CellPadding="5" GridLines="None">

    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Top"
    Width="100px" />
    <Fields>
    <asp:TemplateField HeaderText="Term" SortExpression="term">
    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Top"
    Width="100px" />
    <ItemTemplate>
    <asp:Label ID="TermLabel" runat="server" Text='<%#
    Bind("term") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:textbox ID="term_tb" runat="server" Text='<%#
    Bind("term") %>' />
    <asp:RequiredFieldValidator ID="valTermTextBox"
    ControlToValidate="term_tb" runat="server" Display="dynamic" Text="<br
    />Term is a required field"
    CssClass="error"></asp:RequiredFieldValidator>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:textbox ID="term_tb" runat="server" Text='<%#
    Bind("term") %>' />
    <asp:RequiredFieldValidator ID="valTermTextBox"
    ControlToValidate="term_tb" runat="server" Display="dynamic" Text="<br
    />Term is a required field"
    CssClass="error"></asp:RequiredFieldValidator>
    </InsertItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Name:" SortExpression="term">
    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Top"
    Width="100px" />

    <ItemTemplate>
    <asp:Label ID="DefLabel" runat="server" Text='<%#
    Bind("definition") %>'></asp:Label>
    </ItemTemplate>

    <EditItemTemplate>
    <asp:TextBox ID="def_tb" runat="server" Text='<%#
    Bind("definition") %>'></asp:TextBox><asp:RequiredFieldValidator
    ID="valDefTextBox" ControlToValidate="def_tb" runat="server"
    Display="dynamic" Text="<br />Definition is a required field"
    CssClass="error"></asp:RequiredFieldValidator>
    </EditItemTemplate>

    <InsertItemTemplate>
    <asp:TextBox ID="def_tb" runat="server" Text='<%#
    Bind("definition") %>'></asp:TextBox><asp:RequiredFieldValidator
    ID="valDefTextBox" ControlToValidate="def_tb" runat="server"
    Display="dynamic" Text="<br />Definition is a required field"
    CssClass="error"></asp:RequiredFieldValidator>
    </InsertItemTemplate>
    </asp:TemplateField>

    <asp:CheckBoxField DataField="featured"
    HeaderText="Featured:" SortExpression="featured" />

    <asp:TemplateField ShowHeader="False">
    <EditItemTemplate>
    <asp:Button ID="UpdateButton" runat="server"
    CausesValidation="True" CommandName="Update" Text="Update This
    Record"></asp:Button>
    <asp:Button ID="CancelUpdateButton" runat="server"
    CausesValidation="False" CommandName="Cancel" Text="Cancel"
    OnClick="CancelUpdateButton_Click"></asp:Button>
    </EditItemTemplate>

    <InsertItemTemplate>
    <asp:Button ID="InsertButton" runat="server"
    CausesValidation="True" CommandName="Insert" Text="Insert New
    Record"></asp:Button>
    <asp:Button ID="CancelInsertButton" runat="server"
    CausesValidation="False" CommandName="Cancel" Text="Cancel"
    OnClick="CancelInsertButton_Click"></asp:Button>
    </InsertItemTemplate>
    </asp:TemplateField>

    </Fields>
    </asp:DetailsView>

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    <asp:ObjectDataSource ID="AcronymObjectDataSource" runat="server"
    DeleteMethod="Delete" InsertMethod="Insert"
    OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetAllAcronyms"
    TypeName="AcronymDataTableAdapters.acronymsTableAdapter"
    UpdateMethod="Update">
    <DeleteParameters>
    <asp:parameter Name="Original_id" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="term" Type="String" />
    <asp:parameter Name="definition" Type="String" />
    <asp:parameter Name="featured" Type="Boolean" />
    <asp:parameter Name="Original_id" Type="Int32" />
    <asp:parameter Name="id" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="term" Type="String" />
    <asp:parameter Name="definition" Type="String" />
    <asp:parameter Name="featured" Type="Boolean" />
    </InsertParameters>
    </asp:ObjectDataSource>
     
    , May 10, 2006
    #1
    1. Advertising

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. Teemu Keiski
    Replies:
    3
    Views:
    2,083
    Jacob Yang [MSFT]
    Aug 18, 2003
  2. Giorgio
    Replies:
    1
    Views:
    3,490
    purkka
    Mar 15, 2006
  3. Frits van Soldt

    How to insert a new record in detailsview when there are no record

    Frits van Soldt, Nov 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    746
    Frits van Soldt
    Nov 17, 2005
  4. Feege
    Replies:
    0
    Views:
    460
    Feege
    Dec 20, 2005
  5. gr8beer

    Gridview - wrong record displayed when editing

    gr8beer, Sep 14, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    112
    gr8beer
    Sep 14, 2006
Loading...

Share This Page