C# Formview无法显示gridview';s选定索引
我正在尝试实现一个功能: 单击“编辑”时,将在formview中加载该项(然后应将其置于编辑模式) 但问题是formview总是在编辑模式下显示网格的第一行 谁能告诉我怎么修理这个吗?谢谢C# Formview无法显示gridview';s选定索引,c#,asp.net,gridview,formview,C#,Asp.net,Gridview,Formview,我正在尝试实现一个功能: 单击“编辑”时,将在formview中加载该项(然后应将其置于编辑模式) 但问题是formview总是在编辑模式下显示网格的第一行 谁能告诉我怎么修理这个吗?谢谢 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <div style="overflow-x:scroll; overflow-y:hidden; width:auto; height
<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" />
<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 />
看看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>
Hi joao,您的解决方案有效,非常感谢。此外,您可能需要更正部分答案,如PropertyName=“SelctedValue”,它应该是SelectedValue。很抱歉,没有Visual Studio参加测试。我在文本编辑器中编写了此代码。因此,出现错误。我将更正。如果可以,请限定我的答案。谢谢。如果可以,请在我的答案中投票,并将此问题标记为已解决。这有助于其他具有相同问题的用户并提高我的声誉:)谢谢你的耽搁,一旦我有足够的声誉,我会投你一票!
<SelectParameters>
<asp:ControlParameter Name="CustomerID" Type="Int32"
ControlID="GridView1" PropertyName="SelectedValue" />
</SelectParameters>