问题
I'm looking to create a GridView with the paging hidden, but paging still allowed. Then, I'd like to create my own paging buttons external the GridView and have those page through the data. Essentially, this is to create a scrollable gridview with a sticky pager at the bottom that I can customize to fit the look, feel, and functionality required in my site. Does anyone have any ideas on how to do this? I haven't found much in my searching.
回答1:
You can use a repeater with custom paging or define your own user control for this requirement.Check this article on how to do this with a repeater control. Update: This might help
Update Fixed headers, Client side pagination using jQuery
回答2:
I know this question was answered and is really old now, but I was having a lot of trouble with this and did not find the accepted answer completely helpful, mostly because I did not want to create a new control.
I found that you can do this quite easily actually, and here is how.
I created my gridview inside an updatepanel. I load the data for the grid on a button click I am not showing and then place the dataobject into the viewstate. Then I add a pagerSetting as such:
<PagerSettings Mode="NextPreviousFirstLast" Visible="false" />
Then I added 4 buttons, a label and a text box to a div that I chose to expose to the server:
<div runat="server" id="divPageControls" style="width:100%;margin-bottom:1%;margin-top:1%;display:none;">
<asp:Button runat="server" ID="btnFirst" Text="First" CssClass="ReOrderButtons" UseSubmitBehavior="false" />
<asp:Button runat="server" ID="btnPrev" Text="Previous" CssClass="ReOrderButtons" UseSubmitBehavior="false" />
<asp:Button runat="server" ID="btnNext" Text="Next" CssClass="ReOrderButtons" UseSubmitBehavior="false" />
<asp:Button runat="server" ID="btnLast" Text="Last" CssClass="ReOrderButtons" UseSubmitBehavior="false" /> <br /><br />
<asp:TextBox runat="server" ID="txtPageIndex" AutoPostBack="true" CssClass="priceQuote_TextBoxStyle" style="margin-left:20px;float:none;width:4%;text-align:center;" ></asp:TextBox> <asp:Label runat="server" ID="lblOfPages"></asp:Label>
</div>
Now Everything was in place, All I had to do was add the event handlers.
Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click
_mainView.PageIndex = 0
_mainView_PageIndexChanging(_mainView, New System.Web.UI.WebControls.GridViewPageEventArgs(_mainView.PageIndex))
End Sub
Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click
_mainView.PageIndex = _mainView.PageCount - 1
_mainView_PageIndexChanging(_mainView, New System.Web.UI.WebControls.GridViewPageEventArgs(_mainView.PageIndex))
End Sub
Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click
_mainView.PageIndex -= 1
_mainView_PageIndexChanging(_mainView, New System.Web.UI.WebControls.GridViewPageEventArgs(_mainView.PageIndex))
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
_mainView.PageIndex += 1
_mainView_PageIndexChanging(_mainView, New System.Web.UI.WebControls.GridViewPageEventArgs(_mainView.PageIndex))
End Sub
Protected Sub txtPageIndex_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPageIndex.TextChanged
If fromInternal = False Then
If txtPageIndex.Text <> "" And IsNumeric(txtPageIndex.Text) Then
If CInt(txtPageIndex.Text - 1) < _mainView.PageCount - 1 And CInt(txtPageIndex.Text - 1) >= 0 Then
_mainView.PageIndex = CInt(txtPageIndex.Text - 1)
_mainView_PageIndexChanging(_mainView, New System.Web.UI.WebControls.GridViewPageEventArgs(_mainView.PageIndex))
End If
End If
End If
End Sub
Protected Sub _mainView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles _mainView.PageIndexChanging
_mainView.PageIndex = e.NewPageIndex
_mainView.DataSource = ViewState("currProducts")
_mainView.DataBind()
End Sub
Protected Sub _mainView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles _mainView.DataBound
If _mainView.PageCount > 1 Then
divPageControls.Style("display") = "block"
If _mainView.PageIndex = 0 Then
btnFirst.Style("display") = "none"
btnPrev.Style("display") = "none"
btnLast.Style("display") = "inline"
btnNext.Style("display") = "inline"
ElseIf _mainView.PageIndex > 0 And _mainView.PageIndex < _mainView.PageCount - 1 Then
btnFirst.Style("display") = "inline"
btnPrev.Style("display") = "inline"
btnLast.Style("display") = "inline"
btnNext.Style("display") = "inline"
ElseIf _mainView.PageIndex = _mainView.PageCount - 1 Then
btnLast.Style("display") = "none"
btnNext.Style("display") = "none"
btnFirst.Style("display") = "inline"
btnPrev.Style("display") = "inline"
End If
fromInternal = True
txtPageIndex.Text = CStr(_mainView.PageIndex + 1)
lblOfPages.Text = "of " & CStr(_mainView.PageCount - 1)
End If
End Sub
After all that, when the buttons are pressed the index of the girdview's page is changed and then passed to the view's PageIndexChanging event. This is working wonderfully for me and I hope someone else can use it.
Bear in mind I did not show the async triggers for the update panel or any of the other nuances, however I don't think they are necessary for paging, unless you are using an update panel like I am.
来源:https://stackoverflow.com/questions/9534463/gridview-external-paging