【问题标题】:VB.NET GridView Paging not responding to changesVB.NET GridView 分页不响应更改
【发布时间】:2015-12-04 22:20:55
【问题描述】:

我只想将 GridView 的 PageSize 设置为 20 或将 AllowPaging 设置为 True。但是,在客户端或服务器端更改 GridView 的属性并没有做任何事情——它停留在 10 行的默认分页。

我仍然可以在Page_Load() 中运行Response.Redirect(),所以我相信该页面没有出错。

我认为唯一可能导致复杂情况的是 JavaScript:我使用 jQuery 来操作 GridView 表,以便可以使用 DataTable.js 对表进行排序。

我从未使用过 VB.NET,所以任何指针都值得赞赏。也许这是一个快速解决方案,但我对 VB.NET 太无知了,无法知道问题所在。

这是aspx页面:

<%@ 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>

这里是 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

此代码仍以默认 10 行对 GridView 进行分页。我只是不想让它翻页!

【问题讨论】:

  • 所以你是说上面的代码一次显示10行,你可以查看多页数据?分页控件有吗?
  • @Steve:是的——我不想让它翻页。这么晚才回复很抱歉。您认为是什么问题?

标签: asp.net vb.net gridview


【解决方案1】:

我想通了。我的登录页面中的数据库连接有问题,所以应用程序出错了。 AllowPaging = False 现在可以使用了。这很奇怪,因为除了修改 GridView 之外,我可以做任何其他事情......哦,好吧。但这不是唯一的问题。

当您将&lt;tr&gt; 添加到客户端的GridView 时,GridView 变得愚蠢并开始分页应用程序,即使AllowPaging = False 也是如此。 在服务器端和客户端。

到目前为止,我在 VB.NET 方面的经验一直很糟糕——希望我能从现在开始更顺畅地完成这条学习曲线。

【讨论】:

  • 真的吗?我认为这很棒,尽管在我弄清楚它是如何“思考”之前,有一些事情让我感到沮丧。你习惯什么?
  • 我从事 PHP 开发已经有一段时间了,所以是的,所有这些 ASP 实践对我来说都是陌生的。
猜你喜欢
  • 1970-01-01
  • 2017-07-30
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2020-06-19
  • 1970-01-01
  • 2018-12-07
相关资源
最近更新 更多