有搜索结果时如何更改gridview分页

时间:2016-06-14 09:49:37

标签: c# asp.net gridview

我有gridview和一些用户可以搜索的文本框。 当用户搜索我更新gridview但更改页面时我再次绑定gridview,我丢失了搜索结果。 如何在不再调用DataBind的情况下更改gridview分页?

我用它进行分页:

   protected void grv_Data_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Data.PageIndex = e.NewPageIndex;
        TicketDataBinding();
    }

我的搜索方法是:

  protected void btn_search_Click(object sender, EventArgs e){
        using (var ticket = new BLL.Ticket())
        {
            grv_Data.DataSource = tit.SelectList( fromDate, toDate);
            grv_Data.DataBind();
        }}
抱歉,我忘记了asp.net webform,我不知道这是怎么回事?

2 个答案:

答案 0 :(得分:0)

我已经包含了两个将数据绑定到GridView的示例,其中分页仍然适用于过滤数据。我希望它可以帮助你。

1.您可以在.ASPX中完全执行此操作:

<form id="form1" runat="server">
    <asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox>
    <asp:Button runat="server" Text="Search" />
    <asp:GridView ID="GridView1" runat="server" DataSourceID="sqlDS" AllowPaging="true" PageSize="5" AutoGenerateColumns="true">
        <Columns>
            <asp:BoundField DataField="EmailType" />
            <asp:BoundField DataField="EmailAddress" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="sqlDS" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:conn %>" 
        SelectCommand="SELECT EmailType, EmailAddress FROM EmailNotifications WHERE EmailAddress LIKE @EmailAddressParam + '%'">
        <SelectParameters>
            <asp:ControlParameter ControlID="txtEmailAddress" DbType="String" Name="EmailAddressParam" DefaultValue="%" />
        </SelectParameters>
    </asp:SqlDataSource>
</form>

2.或代码背后:

public partial class PagingAndSearchingInGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GetData();
    }

    private void GetData()
    {
        string emailAddress = txtEmailAddress.Text;
        DataTable table = !Page.IsPostBack ? GetEmails() : GetEmails(emailAddress);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

    private DataTable GetEmails(string emailAddress = "%")
    {
        var table = new DataTable();

        string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        using (var connection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand("SELECT EmailType, EmailAddress FROM EmailNotifications WHERE EmailAddress LIKE @EmailAddressParam + '%'", connection))
            {
                command.Parameters.AddWithValue("@EmailAddressParam", emailAddress);
                using (var a = new SqlDataAdapter(command))
                {
                    connection.Open();
                    a.Fill(table);
                    connection.Close();
                }
            }
        }
        return table;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.GetData();
    }
}

答案 1 :(得分:0)

当pageIndex更改了事件时,您可以检查是否已单击搜索按钮。单击搜索按钮时,您可以将隐藏字段值设置为1或类似的值。

div.box p{
    display:none;
}