Chihuahua Enthusiast
Chihuahua Enthusiast

Reputation: 1580

VB.NET GridView Paging not responding to changes

I simply want to set my GridView's PageSize to 20 or set AllowPaging to True. However, changing the GridView's attributes on the client side or the server side does not do anything -- it's stuck at the default paging at 10 rows.

I can still run a Response.Redirect() in Page_Load() so I believe the page is not erroring out.

The only thing I believe that could be causing complications is JavaScript: I used jQuery to manipulate the GridView Table so that I can use DataTable.js to sort the table.

I've never worked with VB.NET so any pointers are appreciated. Perhaps this is a quick fix but I am too ignorant of VB.NET to know the problem.

Here is the aspx page:

<%@ Page Title="Available Vehicles" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="VehicleList.aspx.vb" Inherits="RentVehicle.VehicleList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script>
        function ShowFull(ctrl) {
            if (ctrl.style.height == '50px')  { ctrl.style.height = '150px'; ctrl.style.width = '270px'; }
            else {ctrl.style.height = '50px'; ctrl.style.width = '90px';
        }}
</script>
<script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-32627957-1']);
    _gaq.push(['_trackPageview']);

    (function () {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

</script>    

<script type="text/javascript">
    $(document).ready(function () {
        // Fix up GridView to support THEAD tags            
        $("#<%=GridView1.ClientID%> tbody").before("<thead><tr></tr></thead>");            
        $("#<%=GridView1.ClientID%> thead tr").append($("#<%=GridView1.ClientID%> th"));
        $("#<%=GridView1.ClientID%> tbody tr:first").remove();
    });
</script>

<script type = "text/javascript">
    // now flip ADA
    $(document).ready(function() {
        $('#<%=GridView1.ClientID%>').DataTable({
            "aaSorting": [[2,'asc'], [3,'desc']]
        });
    });
</script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <h2 style="text-align:center;">Available Vehicles</h2>
    <div style="text-align:center;">
        <p>Click on a thumbnail for a larger photograph of the vehicle.</p>
    </div>
    <div style="width: 100%; height: 2px; background: #F87431; overflow: hidden; padding-left:0px"></div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="0" CellSpacing="2" OnRowDataBound="OnRowDataBound" >
        <Columns>
            <asp:templatefield>
                <itemtemplate>
                    <asp:image id="Image1" runat="server" onclick="ShowFull(this)" imageurl='<%#Eval("photo_large")%>' width="90px" Height="50px" />
                </itemtemplate>
            </asp:templatefield>

            <asp:BoundField DataField="category_name" HeaderText="Type" SortExpression="category_name" />
            <asp:TemplateField HeaderText="ADA" SortExpression="vehicle_name">
                <ItemTemplate>
                    <asp:image id="Image2" runat="server" imageurl="images/disability-symbol.png" width="25px" Height="25px" style="margin-left:5px;" visible='<%# If((Eval("category_name").ToString().Equals("ADA Mini Van") OR Eval("category_name").ToString().Equals("ADA ProMaster")), "True", "False")%>' />
                </ItemTemplate>
                <ItemStyle Width="50px" />
            </asp:TemplateField>
            <asp:BoundField DataField="vehicle_name" HeaderText="Model" SortExpression="vehicle_name" />
            <asp:BoundField DataField="internal_daily_rate" HeaderText="Internal    Per Day" SortExpression="internal_daily_rate" DataFormatString="{0:C}" HeaderStyle-HorizontalAlign="Left" ><HeaderStyle HorizontalAlign="Left"></HeaderStyle>
             <ItemStyle Width="70px" />
             </asp:BoundField>
            <asp:BoundField DataField="internal_mileage_rate" HeaderText="Internal      Per Mile" SortExpression="internal_mileage_rate" DataFormatString="{0:C}"> <ItemStyle Width="70px" />
             </asp:BoundField>
            <asp:BoundField DataField="external_daily_rate" HeaderText="External     Per Day" SortExpression="external_daily_rate" DataFormatString="{0:C}"><ItemStyle Width="70px" />
             </asp:BoundField>
            <asp:BoundField DataField="external_mileage_rate" HeaderText="External    Per Mile" SortExpression="external_mileage_rate" DataFormatString="{0:C}"><ItemStyle Width="70px" />
             </asp:BoundField>
            <asp:TemplateField HeaderText="HOV*" > <ItemTemplate>
                   <asp:CheckBox ID="checkHOV" runat="server" HeaderText="HOV*" Checked='<%# If(Eval("high_occupancy").ToString() = "Y", "True", "False")%>' visible='<%# If(Eval("high_occupancy").ToString() = "Y", "True", "False")%>'></asp:CheckBox> </ItemTemplate>
                   <ItemStyle Width="44px" />
             </asp:TemplateField>
            <asp:CommandField HeaderText="Rent Me!" ButtonType="Image" InsertVisible="False" SelectImageUrl="~/Images/carovico.gif" ShowCancelButton="False" ShowHeader="True" ShowSelectButton="True" Visible="false"/>
            <asp:TemplateField HeaderText="Rental Quantity">
                 <ItemTemplate>
                      <asp:Label ID="lblMaxNumber" runat="server" Text='<%# Eval("Max_number")%>' Visible = "false" />
                      <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList>
                 </ItemTemplate>
                <ItemStyle Width="70px" />
            </asp:TemplateField>
       </Columns>
    </asp:GridView>
    <div style="width: 100%; height: 2px; background: #F87431; overflow: hidden; padding-left:0px"></div> <br />
    <div style="float:right;padding-right:85px"> <asp:Button ID="ButtonCheckout" runat="server" Text="Check Out" /><br />
        <asp:Label ID="LabelErrorMsg" runat="server" Text="You have not chosen any vehicles." ForeColor="#FF3300" Visible="False"></asp:Label>
    </div>
    &nbsp;<p><strong>* High occupancy training required.</strong></p>
    <p><strong>Note: Similar vehicle may be substituted within requested vehicle category depending on availability.    </strong></p><br />
    Garage/Motol Pool Map: <br /><asp:Image ID="Image2" runat="server" Height="172px" ImageUrl="~/Images/motorpool.gif" Width="275px" />

</asp:Content>

And here is the aspx.vb:

Public Class VehicleList
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        If IsNothing(Session("NetID")) Then
            Response.Redirect("login.aspx")
        ElseIf IsNothing(Session("LoggedUser")) Then
            ' users with net id logged in to see the info anyway '    Response.Redirect("aboutme.aspx")  
        End If
        Dim Available_Vehicle As System.Data.DataSet = Rentalwrapper.RentalRequest.Get_Vehicle_list()
        GridView1.DataSource = Available_Vehicle
        GridView1.AllowPaging = False 'DOESNT WORK, HALP
        GridView1.DataBind()
    End If
    LabelErrorMsg.Visible = False
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If (e.Row.RowType = DataControlRowType.DataRow) Then

        'Find the DropDownList in the Row
        Dim ddlCountries As DropDownList = CType(e.Row.FindControl("dropdownlist1"), DropDownList)
        Dim LabelMaxNum As Label = CType(e.Row.FindControl("lblmaxnumber"), Label)
        Dim max_number As Int32 = CInt(LabelMaxNum.Text)

        For i As Int16 = 0 To max_number
            ddlCountries.Items.Add(i.ToString)
        Next
        ddlCountries.SelectedValue = 0
    End If
End Sub

Protected Sub ButtonCheckout_Click(sender As Object, e As EventArgs) Handles ButtonCheckout.Click
    Dim SelectedVehicle As New ArrayList()
    Dim vehicleNo As DropDownList, Quantity As Int16 = 0
    For Each row In GridView1.Rows
        vehicleNo = row.FindControl("dropdownlist1")
        Quantity = CInt(vehicleNo.SelectedValue)
        If Quantity > 0 Then
            SelectedVehicle.Add(Quantity.ToString & " x " & row.cells(2).text)   'CType(row.findcontrol("vehicle_name"), TextBox).Text)
        End If
    Next
    If SelectedVehicle.Count = 0 Then
        LabelErrorMsg.Visible = True
    Else
        Session("SelectedVehicle") = SelectedVehicle
        If IsNothing(Session("LoggedUser")) Then
            Response.Redirect("aboutme.aspx")
        Else
            Response.Redirect("RentalDetail.aspx")
        End If
    End If
End Sub
End Class

This code still pages the GridView at the default 10 rows. I just don't want it to page!

Upvotes: 0

Views: 830

Answers (1)

Chihuahua Enthusiast
Chihuahua Enthusiast

Reputation: 1580

I figured it out. There was something wrong with my database connection in my login page, so the application was erroring out. The AllowPaging = False works now. It's weird because I could do everything else but modify the GridView... Oh well. But that wasn't the only issue.

When you add a <tr> to a GridView on the client side, the GridView gets all stupid and starts paging the application, even if AllowPaging = False. on the Server side and Client side.

So far, my experience with VB.NET has been bad - hopefully I go over this learning curve smoother from here on out.

Upvotes: 1

Related Questions