我有一个 GridView 和一个在网格中搜索的文本框,以及网格内的一个编辑按钮。所有这些在单独测试时都运行良好,但如果我尝试使用搜索按钮,然后使用编辑按钮,这不会编辑正确的行。示例:假设我的 GridView 上有 3 行:
Search:_________
Air Edit
Earth Edit
Sea Edit
如果我在文本框中输入“sea”并单击搜索按钮,则网格中只会显示“sea”记录,而按下编辑时,将显示“Air”记录而不是 sea 记录进行编辑。为什么会发生这种情况以及如何解决?
编辑:ASP代码:
<asp:TextBox ID="TextBox1" runat="server" BackColor="#D9ECFF"
style="height: 20px; width: 186px" AutoPostBack="True"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" BackColor="#0066cc"
BorderColor="#0066cc" BorderStyle="Outset" Font-Bold="True" ForeColor="White"
style=" height: 26px; width: 56px" Text="Search" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" OnRowEditing="EditRow"
OnRowCancelingEdit="CancelEditRow" DataKeyNames="AREA" DataMember="DefaultView">
<Columns>
<asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True"
SortExpression="AREA" />
<asp:TemplateField HeaderText="LEADER_USER" SortExpression="LEADER_USER">
<ItemTemplate><%#Eval("leader_user")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtleaderuser" runat="server" Text='<%#Eval("leader_user")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit"
ImageUrl="images/pencil1.png" Text="Edit" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="BtnCancel" runat="server" CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
搜索代码后面:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim miCon As New Connection
Try
SqlDataSource1.ConnectionString = miCon.GetConnectionString()
SqlDataSource1.SelectCommand = "SELECT * FROM AREA WHERE area LIKE @area"
SqlDataSource1.SelectParameters.Clear()
SqlDataSource1.SelectParameters.Add(New Parameter("area", DbType.String, "%" + TextBox1.Text + "%"))
GridView1.DataBind()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
有什么帮助吗?