Gridview:搜索后编辑

时间:2013-09-10 20:59:37

标签: asp.net vb.net gridview data-binding sqldatasource

我的搜索按钮链接到GridvieW,每行都有一个编辑按钮。当我按下搜索按钮时,数据会发生变化并发生数据绑定()。 之后,如果我尝试使用编辑按钮,则会显示另一行进行编辑,而不是选定的一行(最大问题)。两个按钮在单独测试时效果很好,但不是一个接一个。我解决了从编辑按钮事件中删除GridView1.DataBind(),但之后需要2次点击才能显示编辑模板(另一个问题)。

编辑:我想问题出现在搜索按钮中。你能提供一个不依赖于sqldatasource的好搜索代码吗?

'Where data is loaded into GV
Dim SqlDataSource1 As New SqlDataSource

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    SqlDataSource1.SelectCommand = "SELECT * FROM [TABLE]"
    SqlDataSource1.ConnectionString = "Conn String"

    If Not IsPostBack Then   

        Dim conn As New SqlConnection
    conn.ConnectionString = con.GetConnectionString
    Dim cmd As New SqlCommand()
    cmd.CommandText = "SELECT [AREA], [LEADER_USER] FROM [AREA]"
    cmd.CommandType = CommandType.Text
    cmd.Connection = conn
    conn.Open()
    Dim adpt As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    adpt.Fill(ds)
    GridView1.DataSource = ds
    GridView1.DataBind()
    conn.Close()
    End If

End Sub

'Search button
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    Try
        SqlDataSource1.SelectCommand = "SELECT * FROM TABLE WHERE id LIKE @id"
        SqlDataSource1.SelectParameters.Clear()
        SqlDataSource1.SelectParameters.Add(New Parameter("id", DbType.String, "%" + txtSearch.Text + "%"))
        GridView1.DataSource = SqlDataSource1
        GridView1.DataBind()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

'Edit button
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)

    GridView1.EditIndex = e.NewEditIndex
    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()

End Sub

标记:

<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>

请帮帮我。如何将这两个gridview功能编码为一起工作?这是我知道如何做到这一点的唯一方法,但任何想法对我来说都可以。如果你是C#家伙,你可以使用C#到VB转换器: http://www.developerfusion.com/tools/convert/csharp-to-vb/

2 个答案:

答案 0 :(得分:1)

试试这个:

  fill_grid()
        {
// populate your grid
           SqlCommand cmd = new SqlCommand();

       cmd.CommandText = " your select statement  ";


       cmd.CommandType = CommandType.Text;
       cmd.Connection = this.sqlConnection1;
       this.yourConnection .Open();
       SqlDataAdapter adpt = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       adpt.Fill(ds);
       yourGrid.DataSource = ds;
       yourGrid.DataBind();

       this.sqlConnection1.Close();
    }

然后挂钩搜索按钮的按钮点击事件,如下所示:

 yourButton_Click ((object sender, EventArgs e)
       {
           GridViewRow clickedRow = ((Button)sender).NamingContainer as GridViewRow;

    //Then find your parameter from your textbox in the clicked row 

           TextBox yourbox  = (TextBox)clickedRow.FindControl("your_box");

           //Here is where you would all your search logic whatever that is 

       }

然后,您仍然可以独立于搜索按钮单击事件使用row_updating事件。

答案 1 :(得分:0)

你可以通过if命令在page_load中添加Where语句并删除btnSearch_Click函数中的where。像这样

if (txtSearch.Text.length()>0)
  SqlDataSource1.SelectCommand += "WHERE id LIKE @id"

然后它正常工作