为gridview实现寻呼机控制?

时间:2010-12-02 05:40:20

标签: asp.net gridview

有没有办法通过下拉列表选择要在gridview中显示的记录/行数?

2 个答案:

答案 0 :(得分:1)

如果您的意思是根据DDL选择动态更改行数,请确保可以完成。

我建议在select操作上使用AJAX方法,该方法将向DB查询确切的行数并返回。我经常看到一个查询带回了数千行,而分页等在内存中完成。直接从数据库中获取行/页面并保留带宽的效率要高得多。

不确定这是否正是您所要求的,但希望它有所帮助。

答案 1 :(得分:0)

您还可以使用RowCreated在Codebehind中创建下拉列表。看一下下面的例子(VB.Net):

    Private Sub Yourgrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Yourgrid.RowCreated
        Select Case e.Row.RowType
            Case DataControlRowType.Pager
                Dim ddlPager As New DropDownList
                ddlPager.ID = "DdlPager"
                ddlPager.AutoPostBack = True
                ddlPager.ToolTip = "Change Pagesize"
                ddlPager.Items.Add("5")
                ddlPager.Items.Add("10")
                ddlPager.Items.Add("25")
                ddlPager.Items.Add("50")
                ddlPager.Items.Add("100")
                ddlPager.SelectedValue = "10"
                AddHandler ddlPager.SelectedIndexChanged, AddressOf Me.PageSizeChanged
                e.Row.Cells(0).ColumnSpan -= 1
                Dim td As New TableCell
                Dim span1 As New Label
                span1.Text = "Show"
                span1.Style("margin-left") = "50px"
                td.Controls.Add(span1)
                td.Controls.Add(ddlPager)
                Dim span2 As New Label
                span2.Text = "rows per page"
                td.Controls.Add(span2)
                e.Row.Cells.Add(td)
        End Select
    End Sub

    Private Sub PageSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlPager As DropDownList = DirectCast(sender, DropDownList)
        Dim newPageSize As Int32 = Int32.Parse(ddlPager.SelectedValue)
        YourGrid.PageSize = newPageSize 'change the PageSize of the Grid'
        DataBindYourGrid() 'call the function that Binds your grid to the Datasource'

        UpdYourgrid.Update() 'if you use Ajax, update the UpdatePanel of this GridView'
    End Sub

通过这种方式,您可以在每次回发时自动生成Dropdonwlist,并将其添加到Gridview的寻呼机中。代码可以重用于任何GridView。