GridView External Paging - asp.net

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.

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

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.

Related

asp.net textbox array with events

I have the following code in the page source:
<asp:Button ID="Button1" runat="server" Text="Button" />
And in code behind page:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 9
Dim box As New TextBox()
box.ID = "textBox" + CStr(i)
box.Width = 20
box.Text = CStr(i)
box.TabIndex = CShort(i + 1)
AddHandler box.TextChanged, AddressOf ClickBox
panel1.Controls.Add(box)
Next
End Sub
Private Sub ClickBox(sender As Object, e As EventArgs)
Dim boxUsed = DirectCast(sender, TextBox)
boxUsed.BackColor = Drawing.Color.Crimson
End Sub
The problem is TextChange event for texboxes doesn't fire.
Two things. First:
box.AutoPostBack = True
Second:
Since you're creating textboxes dynamically, they will be gone between every postback, unless you re-create them in the Page_Load event.

ChekBoxList Not Noticing Selected, and not displaying value

Not picking up that something is selected, and not displaying the value.
When I debug the code and step-in:
VBCODE:
When Debugging the "li" after Each holds a value(ex286)
when I go to Item and open the box I get this:
Item = Argument not specified for parameter 'index' of 'Public ReadOnly Default Property Item(index As Integer) As System.Web.UI.WebControls.ListItem'.
Item = In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
the "li" after If holds a value(ex286), but the Selected is "FALSE" Do not know why.
After the = li is the text and the Value(286)
Another thing it only gives me the value for the first box value not the rest if I click them.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
For Each li As ListItem In CheckBoxList.Items
If li.Selected Then
Texttext.Text = li.Value
Else
Texttext.Text = "Give Up Loser!"
End If
NextEnd Sub
ASCX FILE
<asp:CheckBox ID="CheckBoxSelectAll" runat="server" Text="Select All" AutoPostBack="True" />
<asp:CheckBoxList ID="CheckBoxList" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="Name" DataValueField="Id"
RepeatColumns="3" ></asp:CheckBoxList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStuff"
DataObjectTypeName ="DataTransfer.TheData"
TypeName="BusinessDelegate.DataBusinessDelegate">
</asp:ObjectDataSource>
<asp:LinkButton ID="LinkButton" runat="server" Text="Here"></asp:LinkButton>
<asp:Label ID ="Texttext" runat="server" Text=""></asp:Label>
I have tried a few items from online but nothing worked correctly.
Get all selected values of CheckBoxList in VB.NET
ASP.NET, VB: checking which items of a CheckBoxList are selected
I am not sure what you are trying to achieve with this.
The below code might work for you.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Dim cbChecked As Boolean
For lItem = 0 To CheckBoxList.Items.Count - 1
cbChecked = CheckBoxList.GetItemChecked(lItem)
If cbChecked Then
Texttext.Text = CheckBoxList.GetItemText(lItem)
Else
Texttext.Text = "Give Up Loser!"
End If
Next
End Sub
When you run this above code, if the last check box is not checked then you will end up with
'Give Up Loser!' in the 'Texttext' text box.
You can get the number of checked boxes in the list by using the below code
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Dim checkedBoxes = CheckBoxList.CheckedItems
Dim checkedBoxesCount = checkedBoxes.Count
For Each lItems In checkedBoxes
Dim chkdCheckBoxName = lItems.ToString
Next
End Sub
Try this below code to write the values of checked boxes in the text box.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Texttext.Text = "" 'clearing the text box
Dim checkedBoxes = CheckBoxList.CheckedItems
Dim checkedBoxesCount = checkedBoxes.Count
For Each lItems In checkedBoxes
Dim chkdCheckBoxName = lItems.ToString
Texttext.Text = Texttext.Text & " | " & chkdCheckBoxName
Next
If checkedBoxesCount = 0 Then
Texttext.Text = "Give Up Loser!"
End If
End Sub
Above code is based on System.Windows.Forms.CheckedListBox

asp.net store a cookie/session for checkbox

I have a checkbox and I want to achieve that if I checked the checkbox, after reload the page, it's still checked (I don't have a value stored in the database).
The checkbox is
<asp:CheckBox ID="cb1" runat="server" />
The event about checkbox change it
Protected Sub cb1_CheckedChanged(sender As Object, e As EventArgs) Handles cb1.CheckedChanged
Dim SE_cb1 As Boolean = Nothing
SE_cb1 = If(cb1.Checked = True, 1, 0)
Session("SE_cb1") = SE_cb1
End Sub
The page load event is
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Session("SE_cb1") Is Nothing Then cb1.Checked = If(Session("SE_cb1") = 1, True, False)
end sub
I wonder why is that after I reload/redirect the page, the checked checkbox still dropped the check?
Thanks for any advice!
OK, when i change the load event to prerender, it seems OK now. Thanks for the help!
Handles Me.Load--> Handles Me.PreRender

Datapager always marks page 1

I'm facing a problem with two Datapagers associated with a Listview. This is my page:
<asp:DataPager ID="PagerTop" runat="server" PagedControlID="pp" QueryStringField="page" PageSize="30"></asp:DataPager>
<asp:ListView ID="pp" runat="server" ItemPlaceholderID="holder" EnableViewState="true" OnPagePropertiesChanging="PagePropertiesChanging">
...
</asp:ListView>
<asp:DataPager ID="PagerBottom" runat="server" PagedControlID="pp" QueryStringField="page" PageSize="30"></asp:DataPager>
and the code behind is as follows
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim MyData As List(Of Product)
MyData = ResultsManager.GetResults()
pp.DataSource = MyData
pp.DataBind()
End Sub
Private Sub PagePropertiesChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs)
PagerTop.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
PagerBottom.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
pp.DataBind()
End Sub
When I change the page, the listview is updated and shows the results corresponding with the indicated page, and PagerTop shows the selected page. That's how they are supposed to work. But PagerBottom is stucked on page 1 and never changes!
It doesn't matter which pager I click, PagerTop always works and PagerBottom always stays at page 1.
I'm running out of brilliant ideas... what can be wrong here?
a very good example is given here
http://www.c-sharpcorner.com/uploadfile/nipuntomar/datapager-in-Asp-Net-3-5/
just place another data pager with different id without any change. i have tried and worked fine for me.

ASP User Control Issue

I am attempting to construct my own date picker using code from several sources.
Why won't the calendar hide when visible?
myDate.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="myDate.ascx.vb"
Inherits="Website.myDate" %>
<asp:TextBox ID="dateText" runat="server" > </asp:TextBox>
<asp:Button ID="dateBtn" runat="server" UseSubmitBehavior="false" Text="x" />
<asp:Calendar ID="dateCal" runat="server" ></asp:Calendar>
myDate.ascx.vb
Partial Public Class myDate
Inherits System.Web.UI.UserControl
Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
dateText.Text = dateCal.SelectedDate ' Update text box'
dateCal.Visible = False ' Hide calendar'
End Sub
Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
End Sub
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dateCal.Visible = False ' Hide calendar on load'
End Sub
Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
End Sub
End Class
It won't hide when visible because Page_Load runs every time the page is loaded including postbacks (button clicks etc).
So you need to use the IsPostBack property to set the visiblity in Page_load:
if Not Page.IsPostBack then
dateCal.Visible = False ' Hide calendar on load'
end if
Sometimes getting the initialization right in page_load with IsPostBack can lead to a mass of strange conditions so use it wisely.
I think I'd be inclined to set the property in the ascx file rather than in Page_load.
First of all, if you want to toggle controls between page postbacks, you need to use ViewState. Check, is your page uses ViewState.
EnableViewState for the page must be set to true in your case.
Also, check your Page_load function.
On every loading of the page you are hiding your calendar
Page_load is calling every time before any button or calendar even rises.
So, you are changing visibility to true and then using changed visibility value in events:
dateCal.Visible = Not dateCal.Visible
That's making calendar always be visible when you are clicking on dateBtn
To make it clear to you i will order events in your code in the order they are calling:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dateCal.Visible = False ' Hide calendar on load'
End Sub
Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
dateText.Text = dateCal.SelectedDate ' Update text box'
dateCal.Visible = False ' Hide calendar'
End Sub
Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
End Sub
Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
End Sub
Now you may see that every time when the page is loading, the page_load event is calling and hides the calendar.
You should either set Visible="false" in the ascx file for the calendar. Or call
dateCal.Visible = False
only when its first loading of the page (!IsPostback property)
so, in C# it will be
protected void Page_Load(object sender, EventArgs args) {
if (!IsPostback)
dateCal.Visible = false;
}

Resources