【问题标题】:Error with edit CommandName on grid view after searching搜索后在网格视图上编辑 CommandName 时出错
【发布时间】:2013-09-10 19:42:13
【问题描述】:

我有一个 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

有什么帮助吗?

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 你最初在哪里绑定你的网格,Page_Load
    • 不,除了搜索按钮点击事件之外没有“绑定”代码(参见上面的代码)。我猜它会自动发生,因为我使用数据源(参见 asp 代码)
    • 那么在您在搜索框中输入“sea”之前,您如何查看示例中的三行?
    • GridView 是链接到 的,所以我不需要使用后面的 vb 代码来获取网格绑定。
    • 必须调用.DataBind() 来执行SelectCommandSqlDataSource 部分;否则你根本看不到任何行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多