e.NewPageIndex is not getting the correct page value using datagrid in VB.NET 1.1
Category: visual studio editor
Question
LRAM on Fri, 24 Aug 2012 21:52:53
I have a datagrid inside the repeater. When a broker has more then 10 pages of members that are recieving a rebate and you go to pages 11 through 20 and click a page nubmer it takes you back to a page numbered 1 through 10. also it seems that the user can only view 20 pages on the run report, when click to go farther then page 20 it takes you back to page 1. tag:Datagrid Here is my code: ASPX Page: ------------------------------------------------------------------------------------ <%@ Page Language="vb" AutoEventWireup="false" ASPCompat="true" EnableViewStateMac="true" Codebehind="Rebate_Group.aspx.vb" Inherits="brokerreports.Rebate_Group" %> <LINK rel="stylesheet" href="/css/broker.css"> <div class="content"> <table border="0" width="95%" align="center"> <tr> <td class="textRed" colSpan="2"><asp:label id="lblErrorMessage" runat="server" text="There are no Broker Numbers associated with this report." visible="false"></asp:label></td> </tr> </table> <table border="0" width="95%" align="center"> <tr> <td class="h4" colSpan="2"><asp:label id="brksel" runat="server" text="Please select a Broker ID number for the report" visible="true"></asp:label></td> </tr> <tr> <td class="textRed" colSpan="2"><asp:label id="brknoid" runat="server" text="There is no active Group for your selected report. Please check the Broker ID(s) and try again." visible="false"></asp:label></td> </tr> <tr> <td width="20%" valign="top" class="text"><asp:listbox id="BrokerID" Runat="server" cssclass="text" Width="122" Height="100"></asp:listbox></td> <td> <p class="text">It is important to note the definition of small and large employer groups is unique under healthcare reform. For purposes of rebates, the definition of a small group is up to 50 average total employees in the prior calendar year (in this case 2010). This can include part-time and seasonal workers </p> <p class="text">Due to privacy restrictions, rebate amounts are not provided for groups who have terminated their contract with BCBSAZ. Please contact your BCBSAZ sales representative for more information. </p> <p class="text">The data provided in this report is current as of June 15, 2012. Any changes to contact information or the agent of record made after June 15, 2012 is not reflected. The information in this report is confidential and intended for the sole use of the recipient and any dissemination or distribution of this information is strictly prohibited. </p> </td> </tr> <tr> <td valign="top" colspan="2"> <asp:button id="btnSubmit" runat="server" CssClass="button" Text="Run Report"></asp:button> <asp:button id="btnExport" runat="server" CssClass="button" Text="Export to Excel" Visible="false"></asp:button> <asp:checkbox id="chkAddr" runat="server" CssClass="text" text="Include mailing addresses in exported file" visible="false"></asp:checkbox> </td> <!-- <td vAlign="top" align="left"><asp:button id="btnHome" runat="server" CssClass="button" Text="Return To List"></asp:button></td> --> </tr> </table> </TD></TR></TBODY> <asp:repeater id="Repeater_Grp" runat="server" visible="true" onitemdatabound="Repeater_Grp_ItemDataBound"> <ItemTemplate> <tr> <td class="h2"> <asp:Label id="BrkID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"rebateBrokerNumber") %>' visible = "true"> </asp:Label><br /> <asp:Label id="BrkName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"rebateBrokerName") %>' visible = "true"> </asp:Label></td> </tr> <tr> <td> <asp:datagrid id="DataGrid_Rpt" Width="100%" CssClass="dg" runat="server" visible="true" AllowPaging="true" PageSize="10" OnPageIndexChanged="DataGrid_Rpt_Page" AllowCustomPaging="false" AutoGenerateColumns="False" PagerStyle-AlwaysVisible="true" AllowSorting="true" OnSortCommand="DataGrid_Rpt_Sorting" showfooter="true"> <HeaderStyle CssClass="dgHeader"></HeaderStyle> <FooterStyle BackColor="#0082c5"></FooterStyle> <SelectedItemStyle cssclass="dgSelItem"></SelectedItemStyle> <AlternatingItemStyle CssClass="dgAltItem"></AlternatingItemStyle> <ItemStyle cssClass="dgItem"></ItemStyle> <PagerStyle Mode="NumericPages" HorizontalAlign="Right"></PagerStyle> <Columns> <asp:BoundColumn DataField="rebateGroupID" ReadOnly="True" HeaderText="Group Number" SortExpression="rebateGroupID"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" width="100px"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGrpName" ReadOnly="True" HeaderText="Group Name" SortExpression="rebateGrpName"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" width="100px"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupTerm" SortExpression="rebateGroupTerm" ReadOnly="True" HeaderText="Group Status"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" width="100px"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateMLRSize" SortExpression="rebateMLRSize" ReadOnly="True" HeaderText="Group Size"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" width="100px"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupAddr1" ReadOnly="True" HeaderText="Street1" visible="false"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupAddr2" ReadOnly="True" HeaderText="Street2" visible="false"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupCity" ReadOnly="True" HeaderText="City" visible="false"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupState" ReadOnly="True" HeaderText="State" visible="false"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupZip" ReadOnly="True" HeaderText="Zip code" visible="false"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateRebateAmount" ReadOnly="True" SortExpression="rebateRebateAmount" HeaderText="Rebate Amount"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" width="90px"></HeaderStyle> <ItemStyle HorizontalAlign="Right" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rebateGroupLeader" ReadOnly="True" SortExpression="rebateGroupLeader" HeaderText="GBA Name"> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" width="100px"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> </asp:BoundColumn> </Columns> </asp:datagrid> </td> </tr> </ItemTemplate> <HeaderTemplate> <table style="border-top:1px solid #C6E3F7;margin-left:40px;width:90%;margin-top:10px;"> <tr> <td> </td> </tr> </table> <table style="width:80%;margin-top:10px;margin-left:40px;"> </HeaderTemplate> <FooterTemplate> </TABLE> </FooterTemplate> </asp:repeater> <PagingTemplate> <div class="content" align="center" style="MARGIN-TOP:10px;MARGIN-LEFT:40px;WIDTH:80%"> <span class="h4"> <asp:label id="lblpages" runat="server" visible="false" text="Pages:" class="h4"></asp:label> <asp:LinkButton id="lbtnFirst" runat="server" visible="false">First</asp:LinkButton> <asp:LinkButton id="lbtnPrev" runat="server" visible="false"><<</asp:LinkButton> <asp:LinkButton id="lbtnNext" runat="server" visible="false">>></asp:LinkButton> <asp:LinkButton id="lbtnLast" runat="server" visible="false">Last</asp:LinkButton></span></div></TD></TR></TBODY></TABLE> </div></PAGINGTEMPLATE> <DIV></DIV> ----------------------------------------------------------------------------------------- VB.Net Code Behind: ----------------------------------------------------------------------------------------- Imports System.IO Imports System.Data Imports System.Text Imports BAZ.Web Imports System.Data.Common Imports BAZ.Helpers Imports System.Collections Imports System.Data.SqlClient Imports System.Web.UI.HtmlControls Imports System.Web.UI.WebControls Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.Web.UI Public Class Rebate_Group Inherits IxBCBSAZ.IxSecurePage #Region "Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents BrokerID As System.Web.UI.WebControls.ListBox Protected WithEvents Run As System.Web.UI.WebControls.Button Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents btnExport As System.Web.UI.WebControls.Button Public dt As New DataTable Protected WithEvents chkAddr As System.Web.UI.WebControls.CheckBox Protected WithEvents lblpages As System.Web.UI.WebControls.Label Protected WithEvents brksel As System.Web.UI.WebControls.Label Protected WithEvents brknoid As System.Web.UI.WebControls.Label Protected WithEvents BrkID As System.Web.UI.WebControls.Label Protected WithEvents BrkName As System.Web.UI.WebControls.Label Protected WithEvents DataGrid_Rpt As System.Web.UI.WebControls.DataGrid Protected WithEvents Repeater_Grp As System.Web.UI.WebControls.Repeater Protected WithEvents linkPrev As System.Web.UI.WebControls.HyperLink Protected WithEvents linkNext As System.Web.UI.WebControls.HyperLink Protected WithEvents lbtnPrev As System.Web.UI.WebControls.LinkButton Protected WithEvents lbtnNext As System.Web.UI.WebControls.LinkButton Protected WithEvents lbtnFirst As System.Web.UI.WebControls.LinkButton Protected WithEvents lbtnLast As System.Web.UI.WebControls.LinkButton Public Shared dg_view As New DataView 'Public Shared dg As New DataGrid 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Protected WithEvents lblErrorMessage As System.Web.UI.WebControls.Label Dim obj As New PagedDataSource Private Property SortDirection() As String Get If (ViewState("SortDirection") Is Nothing) Then ViewState("SortDirection") = String.Empty Return ViewState("SortDirection").ToString() End Get Set(ByVal value As String) ViewState("SortDirection") = value End Set End Property Private Property SortOrderBy() As String Get If (ViewState("SortOrderBy") Is Nothing) Then ViewState("SortOrderBy") = String.Empty Return ViewState("SortOrderBy").ToString() End Get Set(ByVal value As String) ViewState("SortOrderBy") = value End Set End Property Private Property SortExpression() As String Get If (ViewState("SortExpression") Is Nothing) Then ViewState("SortExpression") = String.Empty Return ViewState("SortExpression").ToString() End Get Set(ByVal value As String) ViewState("SortExpression") = value End Set End Property Public Property CurrentPage() As Integer Get ' look for current page in ViewState Dim o As Object = Me.ViewState("_CurrentPage") If o Is Nothing Then Return 0 Else ' default to showing the first page Return CInt(o) End If End Get Set(ByVal Value As Integer) Me.ViewState("_CurrentPage") = Value End Set End Property Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region #Region "Page Load event" Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Try If Not IsPostBack Then SetBrokerIDsDropDownIx() Else GenerateReport() End If Catch ex As Exception System.Logs.WebLog.LogException(ex, "Test version of broker_grouprebate_inq is throwing an unexpected exception") Response.Redirect("../Errors/UnexpectedError.aspx") End Try End Sub #End Region #Region "Logic for generating report" Private Sub GenerateReport() Dim sqlCmd As New SqlCommand Dim sqlAdp As New SqlDataAdapter Dim ds As New DataSet Dim id As String = BrokerID.SelectedValue.ToString 'Dim brokerArrayList As ArrayList = Session.Item("BrokerArray") Dim brokerArrayList As New ArrayList Try If Not (Session.Item("BrokerArray") Is Nothing) Then brokerArrayList = Session.Item("BrokerArray") End If brokerArrayList.Add("05645") brokerArrayList.Add("03345") brokerArrayList.Add("02146") brokerArrayList.Add("04146") brokerArrayList.Add("06845") brokerArrayList.Add("05845") brokerArrayList.Add("09845") brokerArrayList.Add("99999") brokerArrayList.Add("88972") brokerArrayList.Add("89806") If IsNothing(brokerArrayList.ToArray()) Or (brokerArrayList.Count = 0) Then lblErrorMessage.Visible = True Exit Sub Else lblErrorMessage.Visible = False End If brokerArrayList.Sort() If id.Equals("All") Then id = Get_ArrayList_String(brokerArrayList) End If Catch ex As Exception System.Logs.WebLog.LogException(ex, "There are no Broker Numbers associated with this report.") End Try Dim oCConn As New SqlConnection(System.Web.WebEnvironment.GetConnString("Brokers")) oCConn.Open() sqlCmd.Connection = oCConn sqlCmd.CommandText = "usp_GetRbtBrkID_Grp" sqlCmd.CommandType = CommandType.StoredProcedure sqlCmd.Parameters.Add("@BrkNums", id) sqlAdp.SelectCommand = sqlCmd sqlAdp.Fill(ds) If ds.Tables.Count > 0 Then If ds.Tables(0).Rows.Count > 0 Then brknoid.Visible = False brksel.Visible = True btnExport.Visible = True chkAddr.Visible = True If ds.Tables(0).Rows.Count > 4 Then lbtnFirst.Visible = True lbtnLast.Visible = True Else lbtnFirst.Visible = False lbtnLast.Visible = False End If Else brksel.Visible = False brknoid.Visible = True btnExport.Visible = False chkAddr.Visible = False End If End If obj.DataSource = ds.Tables(0).DefaultView obj.AllowPaging = True obj.PageSize = 4 obj.CurrentPageIndex = CurrentPage If obj.PageCount - 1 > 3 Then lbtnPrev.Visible = True lbtnNext.Visible = True lbtnFirst.Enabled = True lbtnLast.Enabled = True lbtnPrev.Enabled = True lbtnNext.Enabled = True lbtnFirst.Enabled = True lbtnLast.Enabled = True ElseIf obj.PageCount - 1 = 3 Then lbtnPrev.Visible = True lbtnNext.Visible = True lbtnPrev.Enabled = False lbtnNext.Enabled = True lbtnFirst.Enabled = True lbtnLast.Enabled = True ElseIf obj.PageCount = 2 Then lbtnPrev.Visible = True lbtnNext.Visible = True lbtnPrev.Enabled = False lbtnNext.Enabled = False lbtnFirst.Enabled = True lbtnLast.Enabled = True lblpages.Visible = True Else lbtnPrev.Visible = False lbtnNext.Visible = False lbtnPrev.Enabled = False lbtnNext.Enabled = False lbtnFirst.Enabled = False lbtnLast.Enabled = False lblpages.Visible = False lbtnFirst.Visible = False lbtnLast.Visible = False End If lbtnFirst.Enabled = Not obj.IsFirstPage lbtnLast.Enabled = Not obj.IsLastPage Repeater_Grp.DataSource = obj Repeater_Grp.DataBind() End Sub #End Region #Region "Repeater Binding" Protected Sub Repeater_Grp_ItemDataBound(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Dim old_id As String = String.Empty Dim sortexp As String = String.Empty If (e.Item.ItemType = ListItemType.Item) Or _ (e.Item.ItemType = ListItemType.AlternatingItem) Then Dim Brkid As New Label Dim Brkname As New Label Brkid = CType(e.Item.FindControl("BrkID"), Label) Brkname = CType(e.Item.FindControl("BrkName"), Label) old_id = Brkid.Text Dim new_id As String = String.Empty new_id = "Broker ID " ID = new_id + old_id CType(e.Item.FindControl("BrkID"), Label).Text = ID Dim Brk_name As String = String.Empty Brk_name = ToTitleCase(Brkname.Text) CType(e.Item.FindControl("BrkName"), Label).Text = Brk_name Dim dg As New DataGrid dg = CType(e.Item.FindControl("DataGrid_Rpt"), DataGrid) sortexp = ViewState("Sortexpression") Dim dg_view As DataView = Createdatasource(old_id, sortexp) dg_view = Createdatasource(old_id, sortexp) dg.DataSource = dg_view Dim id_brk As String = BrokerID.SelectedIndex.ToString If id_brk.Equals("0") Then dg.PageSize = 10 '5 Else dg.PageSize = 15 End If If dg_view.Table.Rows.Count > 5 Then dg.AllowPaging = True Else dg.AllowPaging = False End If dg.PagerStyle.Mode = PagerMode.NumericPages dg.DataBind() End If End Sub #End Region #Region "Datagrid Binding,Sorting,Paging" Function Createdatasource(ByVal id As String, ByVal srtexp As String) As ICollection Dim dg_sqlCmd As New SqlCommand Dim dg_sqlAdp As New SqlDataAdapter Dim dg_ds As New DataSet Dim dg_oCConn As New SqlConnection(System.Web.WebEnvironment.GetConnString("Broker")) dg_oCConn.Open() dg_sqlCmd.Connection = dg_oCConn dg_sqlCmd.CommandText = "usp_GetRbtDet" dg_sqlCmd.CommandType = CommandType.StoredProcedure dg_sqlCmd.Parameters.Add("@BrkNum", id) dg_sqlAdp.SelectCommand = dg_sqlCmd dg_sqlAdp.Fill(dg_ds) Dim source As DataView = New DataView(dg_ds.Tables(0)) Dim drv As DataRowView For Each drv In source If drv("rebateRebateAmount") = "No Rebate" Or drv("rebateRebateAmount") = "See Note" Then Else Dim amt As Decimal = drv("rebateRebateAmount") Dim deci_amt As String = amt.ToString("#0.00") ' Dim deci_amt As String = String.Format("{0: #,#.00", drv("rebateRebateAmount")) Dim new_amount As String = "$" + deci_amt drv("rebateRebateAmount") = new_amount End If Dim size As String = String.Empty If drv("rebateMLRSize") = "SG" Then size = "Small Group" drv("rebateMLRSize") = size ElseIf drv("rebateMLRSize") = "LG" Then size = "Large Group" drv("rebateMLRSize") = size Else End If Dim termind As String = String.Empty If drv("rebateGroupTerm") = "N" Then termind = "Active" drv("rebateGroupTerm") = termind ElseIf drv("rebateGroupTerm") = "Y" Then termind = "Terminated" drv("rebateGroupTerm") = termind Else End If Dim Grpname As String = drv("rebateGrpName") If Not Grpname Is String.Empty Then Grpname = ToTitleCase(Grpname) drv("rebateGrpName") = Grpname End If Dim GBAname As String = drv("rebateGroupLeader") If Not GBAname Is String.Empty Then GBAname = FormatName(GBAname) drv("rebateGroupLeader") = GBAname End If Next Dim orderby As String = String.Empty orderby = ViewState("SortOrderBy") source.Sort = orderby Return source End Function Sub DataGrid_Rpt_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles DataGrid_Rpt.PageIndexChanged Dim grid As DataGrid = DirectCast(sender, DataGrid) grid.CurrentPageIndex = e.NewPageIndex Dim id_brk As String = BrokerID.SelectedIndex.ToString If id_brk.Equals("0") Then grid.PageSize = 10 '5 Else grid.PageSize = 15 End If grid.DataBind() End Sub #End Region #Region "Fill Broker Dropdown" Private Sub SetBrokerIDsDropDownIx() Dim allIdList As String = String.Empty Dim brokerArrayList As New ArrayList Try brokerArrayList.Add("05645") brokerArrayList.Add("03345") brokerArrayList.Add("02146") brokerArrayList.Add("04146") brokerArrayList.Add("06845") brokerArrayList.Add("05845") brokerArrayList.Add("09845") brokerArrayList.Add("99999") brokerArrayList.Add("88972") brokerArrayList.Add("89806") If IsNothing(brokerArrayList.ToArray()) Or (brokerArrayList.Count = 0) Then lblErrorMessage.Visible = True Exit Sub Else lblErrorMessage.Visible = False End If brokerArrayList.Sort() 'Session.Item("BrokerArray") = brokerArrayList allIdList = Get_ArrayList_String(brokerArrayList) 'Add broker ids to dropdown BrokerID.Items.Add("All") BrokerID.SelectedIndex = 0 BrokerID.Items(0).Value = allIdList Dim ID As String For Each ID In brokerArrayList BrokerID.Items.Add(ID) Next ID Catch ex As Exception BAZ.Logs.WebLog.LogException(ex, "There are no Broker Numbers associated with this report.") End Try End Sub Private Function Get_ArrayList_String(ByVal brokerArray As ArrayList) Dim brokerArrayStr As String brokerArrayStr = String.Empty Dim brokerID As String For Each brokerID In brokerArray If brokerArrayStr.Length = 0 Then brokerArrayStr = brokerID Else brokerArrayStr = brokerArrayStr & "," & brokerID End If Next brokerID Get_ArrayList_String = brokerArrayStr End Function #End Region #Region "Report Navigation Logic" Private Sub lbtnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnPrev.Click CurrentPage -= 1 GenerateReport() End Sub Private Sub lbtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnNext.Click CurrentPage += 1 GenerateReport() End Sub Private Sub lbtnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnFirst.Click CurrentPage = 0 GenerateReport() End Sub Private Sub lbtnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnLast.Click CurrentPage = obj.PageCount - 1 GenerateReport() End Sub #End Region End Class