搜索后在gridview中编辑CommandName时出错

时间:2013-09-06 16:22:29

标签: asp.net vb.net gridview edit templatefield

我有一个带有文本框的GridView,它可以搜索到网格,还有一个网格内的编辑按钮。 所有这些在单独测试时都能很好地工作,但如果我尝试使用搜索按钮,然后使用编辑按钮,则不会编辑右侧行。示例:假设我的GridView上有3行:

Search:_________
Air    Edit 
Earth  Edit 
Sea    Edit 

如果我在文本框中写“海”并单击搜索按钮,则只会在网格中显示“海”记录,按编辑时,将显示“空气”记录以编辑而不是海记录。为什么会发生这种情况以及如何解决?

已编辑: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

任何帮助?

1 个答案:

答案 0 :(得分:1)

在没有看到所有代码的情况下,我的猜测是您的编辑按钮正在回发到服务器并且网格的初始绑定逻辑再次执行,这没有考虑在搜索框中键入的值;因此,第一行将再次Air,就像您执行搜索之前一样。

要停止此操作,请在Page_Load事件中添加以下内容:

Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
    ' Put logic here to bind the grid only when page is first loaded

    End If
End Sub