SQLDataSourceで正常に動作するグリッドビューがあります。編集、削除ボタンは完全に機能します。
ただし、レコードを検索してそのレコードを編集しようとすると、グリッドビューは編集モードで最初の行を開きます。
何を間違えたのかわかりません。
これが私のコードです
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customer_id"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." AllowPaging="True"
AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal"
PageSize="5" Width="873px" onrowediting="GridView1_RowEditing" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" ></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Customer_id" HeaderText="Customer_id" ReadOnly="True"
SortExpression="Customer_id" InsertVisible="False" />
<asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name"
SortExpression="Customer_Name" />
<asp:BoundField DataField="Customer_Type" HeaderText="Customer_Type" SortExpression="Customer_Type" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"
Font-Bold="True" Font-Italic="True" Font-Overline="True" Font-Size="Large" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
そして、コードビハインドは次のようになります。
protected void Page_Load(object sender, EventArgs e)
{
string vs = (string)ViewState["buttonClicked"];
string isEditing = (string)ViewState["isEditing"];
if (IsPostBack)
{
if (vs == "False")
{
RadioButtonListChanged();
GridView1.DataBind();
}
else if (vs == "True")
{
btnSearch_Click(sender, e);
GridView1.DataBind();
}
}
}
protected void RadioButton1_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonListChanged();
}
private void RadioButtonListChanged()
{
ViewState["buttonClicked"] = "False";
string sqlString;
if (RadioButton1.SelectedItem.Text != "All")
{
sqlString = "Select * from customers where status='True' and customer_type = '" + RadioButton1.SelectedValue.ToString() + "' order by customer_name";
}
else
{
sqlString = "Select * from customers where status='True' order by customer_name";
}
SqlDataSource1.SelectCommand = sqlString;
SqlDataSource1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
ViewState["buttonClicked"] = "True";
string sqlString;
sqlString = "Select * from customers where status='True' and customer_name like '%" + txtCustomerName.Text + "%' order by customer_type, customer_name";
SqlDataSource1.SelectCommand = sqlString;
SqlDataSource1.DataBind();
}