我在使用gridview和fromview做后台程序时,用了gridview的排序功能,排序之后记录与原来的记录不一样,可我点击更新的时候,还是调用的是原来的那个记录,不是现在的记录,
我想肯定是ID号弄错了,但就是不知道怎么把后来排序之后的ID号对应起来,这样更新,才能达到自己想要的效果!
这是代码 这是部分代码,我估计解决问题看这些就够了吧
Code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<strong><span style="color: #3333ff">GridView+FormView 示範資料 新增/修改/刪除<br />
</span></strong>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" EmptyDataText="沒有資料錄可顯示。"
ForeColor="#333333" GridLines="None" PageSize="5" AllowSorting="True">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField ShowHeader="False">
<HeaderTemplate>
<asp:Button ID="btnInsert" runat="server" CausesValidation="False" CommandName="Insert"
Text="新增"></asp:Button>
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="編輯"></asp:Button>
<asp:Button ID="btnDelete" runat="server" CausesValidation="False" CommandName="Delete"
Text="刪除"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="titleName" HeaderText="titleName" SortExpression="titleName" />
<asp:BoundField DataField="createTime" HeaderText="createTime" SortExpression="createTime" />
<asp:BoundField DataField="creator" HeaderText="creator" SortExpression="creator" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:FormView ID="FormView1" runat="server" CellPadding="4" DataKeyNames="ID"
DataSourceID="SqlDataSource1" DefaultMode="Edit" ForeColor="#333333" Visible="False">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<EditItemTemplate>
<table style="width: 400px">
<tr>
<td style="width: 100px; text-align: left">
ID</td>
<td style="width: 392px; text-align: left">
<asp:Label ID="EmployeeIDLabel1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
<asp:TextBox ID="txtEmployeeID" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px; text-align: left">
titleName</td>
<td style="width: 392px; text-align: left">
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("titleName") %>'></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px; text-align: left">
createTime</td>
<td style="width: 392px; text-align: left">
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("createTime") %>'></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px; text-align: left">
creator</td>
<td style="width: 392px; text-align: left">
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("creator") %>'></asp:TextBox></td>
</tr>
</table>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="False" CommandName="Insert"
Text="新增"></asp:LinkButton>
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="更新"></asp:LinkButton>
<asp:LinkButton ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消"></asp:LinkButton>
</EditItemTemplate>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"
InsertCommand="INSERT INTO [firstLevelTitle] VALUES (@titleName,@createTime,@creator)"
ProviderName="<%$ ConnectionStrings:NorthwindConnectionString1.ProviderName %>"
SelectCommand="SELECT * FROM firstLevelTitle"
UpdateCommand="UPDATE firstLevelTitle set titleName=@titleName,createTime=@createTime,creator=@creator WHERE [ID] = @ID" OldValuesParameterFormatString="{0}">
<UpdateParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
</div>
</form>
</body>
</html>