Formview 无法显示来自 gridview 的选定索引的数据

Formview cannot show data from gridview's selected index

我正在尝试实现一个功能: 当您单击编辑时,您将在表单视图中加载该项目(然后应将其置于编辑模式。

但问题是 formview 在编辑模式下总是显示网格的第一行。

谁能告诉我如何解决这个问题?谢谢。

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div style="overflow-x:scroll; overflow-y:hidden; width:auto; height:auto">
<asp:GridView ID="GridView1" runat="server" DataKeyNames="CustomerID,ModifyBy" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource_gridView" CellPadding="4" ForeColor="#333333" GridLines="None" Height="325px" HorizontalAlign="Center" Width="809px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" InsertVisible="False" ReadOnly="True" />
        <asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" />
        <asp:BoundField DataField="CustomerTitle" HeaderText="CustomerTitle" SortExpression="CustomerTitle" />
        <asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" />
        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
        <asp:BoundField DataField="StateID" HeaderText="StateID" SortExpression="StateID" />
        <asp:BoundField DataField="CountryID" HeaderText="CountryID" SortExpression="CountryID" />
        <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:BoundField DataField="ModifyBy" HeaderText="ModifyBy" SortExpression="ModifyBy" />
        <asp:CheckBoxField DataField="IsActive" HeaderText="IsActive" SortExpression="IsActive" ReadOnly="true" />
    </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource_gridView" runat="server" ConnectionString="<%$ ConnectionStrings:SummerBreezeConnectionString %>" SelectCommand="SELECT CustomerName, CustomerTitle, Address1, City, StateID, CountryID, PostalCode, Email, CustomerID, ModifyBy, IsActive FROM Customer" UpdateCommand="adn_Customer_UPDATE" UpdateCommandType="StoredProcedure" DeleteCommand="adn_Customer_DELETE" DeleteCommandType="StoredProcedure" InsertCommand="adn_Customer_INSERT" InsertCommandType="StoredProcedure">
    <DeleteParameters>
        <asp:Parameter Name="CustomerID" Type="Int32" />
        <asp:Parameter Name="ModifyBy" Type="String" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="CustomerName" Type="String" />
        <asp:Parameter Name="CustomerTitle" Type="String" />
        <asp:Parameter Name="Address1" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="StateID" Type="Int32" />
        <asp:Parameter Name="PostalCode" Type="String" />
        <asp:Parameter Name="CountryID" Type="Int32" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="CreateBy" Type="String" />
        <asp:Parameter Direction="InputOutput" Name="CustomerID" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="CustomerID" Type="Int32" />
        <asp:Parameter Name="CustomerName" Type="String" />
        <asp:Parameter Name="CustomerTitle" Type="String" />
        <asp:Parameter Name="Address1" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="StateID" Type="Int32" />
        <asp:Parameter Name="PostalCode" Type="String" />
        <asp:Parameter Name="CountryID" Type="Int32" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="ModifyBy" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource_formView" ForeColor="Black" GridLines="Vertical" Height="237px" OnItemUpdated="FormView1_ItemUpdated" Width="342px">
    <EditItemTemplate>
        CustomerName:
        <asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
        <br />
        CustomerTitle:
        <asp:TextBox ID="CustomerTitleTextBox" runat="server" Text='<%# Bind("CustomerTitle") %>' />
        <br />
        Address1:
        <asp:TextBox ID="Address1TextBox" runat="server" Text='<%# Bind("Address1") %>' />
        <br />
        City:
        <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
        <br />
        StateID:
        <asp:TextBox ID="StateIDTextBox" runat="server" Text='<%# Bind("StateID") %>' />
        <br />
        CountryID:
        <asp:TextBox ID="CountryIDTextBox" runat="server" Text='<%# Bind("CountryID") %>' />
        <br />
        PostalCode:
        <asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# Bind("PostalCode") %>' />
        <br />
        Email:
        <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
        <br />
        ModifyBy:
        <asp:TextBox ID="ModifyByTextBox" runat="server" Text='<%# Bind("ModifyBy") %>' />
        <br />
        CustomerID:
        <asp:Label ID="CustomerIDLabel1" runat="server" Text='<%# Eval("CustomerID") %>' />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource_formView" runat="server" ConnectionString="<%$ ConnectionStrings:SummerBreezeConnectionString %>" DeleteCommand="adn_Customer_DELETE" DeleteCommandType="StoredProcedure" InsertCommand="adn_Customer_INSERT" InsertCommandType="StoredProcedure" SelectCommand="SELECT CustomerName, CustomerTitle, Address1, City, StateID, CountryID, PostalCode, Email, IsActive, ModifyBy, CustomerID FROM Customer" UpdateCommand="adn_Customer_UPDATE" UpdateCommandType="StoredProcedure">
    <DeleteParameters>
        <asp:Parameter Name="CustomerID" Type="Int32" />
        <asp:Parameter Name="ModifyBy" Type="String" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="CustomerName" Type="String" />
        <asp:Parameter Name="CustomerTitle" Type="String" />
        <asp:Parameter Name="Address1" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="StateID" Type="Int32" />
        <asp:Parameter Name="PostalCode" Type="String" />
        <asp:Parameter Name="CountryID" Type="Int32" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="CreateBy" Type="String" />
        <asp:Parameter Direction="InputOutput" Name="CustomerID" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="CustomerID" Type="Int32" />
        <asp:Parameter Name="CustomerName" Type="String" />
        <asp:Parameter Name="CustomerTitle" Type="String" />
        <asp:Parameter Name="Address1" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="StateID" Type="Int32" />
        <asp:Parameter Name="PostalCode" Type="String" />
        <asp:Parameter Name="CountryID" Type="Int32" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="ModifyBy" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
<br />

背后的代码

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        FormView1.ChangeMode(FormViewMode.Edit);
        FormView1.DataBind();
    }

查看来自 FormView 数据源的 SelectCommand 。他没有where子句。

您需要指定一个子句来过滤客户端数据。尝试将 sql 替换为

SELECT CustomerName, CustomerTitle, Address1, City, StateID, CountryID, PostalCode, 
Email, IsActive, ModifyBy, CustomerID FROM Customer where CostumerID = @CostumerID

在关闭之前输入 SelectCommand 中使用的参数

<SelectParameters>
    <asp:ControlParameter Name="CustomerID" Type="Int32"
     ControlID="GridView1" PropertyName="SelectedValue" />
</SelectParameters>