GridView & DetailsView - Wrong Record Being Sent Over

J

jeffmagill

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top