【发布时间】:2021-06-21 09:13:36
【问题描述】:
情况:
我有一个主要工作的 aspx 页面,其中包含一个 asp:GridView 和一个 asp:TextBox,使用 asp:RangeValidator 来管理 gridview 的页面大小。我还有两个 asp:LinkButton 来分别增加和减少大小。在代码隐藏中,我使用数据表作为 gridview 的数据源。
在第一次将多行填充到 gridview 之后,我可以更新 rowcount 文本框,按 Enter 键会导致 gridview 显示新的页面大小(通过 pageLoad 事件)。
问题:
当我单击任一链接按钮来调整行数文本框值,然后更新 gridview 的页面大小时,gridview 之后显示为空白......就像没有可用的数据一样。
通过单击链接按钮更新行计数并且网格视图显示为空白后,我可以单击行计数文本框并按 Enter,现在显示网格视图,其页面大小与行计数文本框值匹配。这表明 gridview 数据仍然存在并且没有丢失。
研究
通过调试,我可以看到 pageload 事件在链接按钮单击事件之前触发,但我不明白为什么在链接按钮单击执行后 gridview 不显示。在代码隐藏中,在加载页面和链接按钮单击事件设置页面大小后重新绑定网格视图。
我需要什么帮助
我不确定如何纠正此行为,以便链接按钮单击事件正确更新并显示网格视图以匹配请求的页面大小。
我不想破坏页面生命周期。任何正常的 aspx 生命周期建议将不胜感激!
HTML sn-p
注意:我删除了一些 html 内容和所有 css 样式,以使相关的 html 元素更易于阅读。
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SearchForEmployee.aspx.vb" Inherits="EmployeeSearch.SearchForEmployee1" EnableSessionState="True" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
...
</head>
<body>
<form id="form1" runat="server">
...
<label>Row Count: <asp:TextBox ID="RowCount" runat="server" ClientIDMode="Static" Text="7" Width="50px"></asp:TextBox></label>
<asp:LinkButton ID="BtnRowCountUp" runat="server" ClientIDMode ="Static"><i class="fa fa-plus"></i></asp:LinkButton>
<asp:LinkButton ID="BtnRowCountDown" runat="server" ClientIDMode ="Static"><i class="fa fa-minus"></i></asp:LinkButton>
<br /><asp:RangeValidator ID="RowCountValidator" Type="Integer" MinimumValue="5" MaximumValue="20" ControlToValidate="RowCount" runat="server"></asp:RangeValidator>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="OnPageIndexChanging" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" ClientIDMode="Static" PageSize="7">
...
</form>
</body>
</html>
CodeBehind sn-p
Public Class SearchForEmployee1
Private dt As DataTable
Private cRowCountMin As Integer = 5
Private cRowCountMax As Integer = 20
Private cRowCountInit As Integer = 7
Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
If Not Session("dt") Is Nothing Then
dt = DirectCast(Session("dt"), DataTable)
End If
...
If Not Me.IsPostBack Then
Me.RowCountValidator.MinimumValue = cRowCountMin.ToString
Me.RowCountValidator.MaximumValue = cRowCountMax.ToString
Me.RowCount.Text = cRowCountInit.ToString
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
...
End If
Me.GridView1.PageSize = CInt(Me.RowCount.Text)
Me.GridView1.DataBind()
End Sub
Private Sub BtnRowCountUp_Click(sender As Object, e As EventArgs) Handles BtnRowCountUp.Click
Dim iRowCount As Integer = CInt(Me.RowCount.Text)
If iRowCount < cRowCountMax Then
iRowCount += 1
Me.RowCount.Text = iRowCount.ToString
Me.GridView1.PageSize = iRowCount
Me.GridView1.DataBind()
End If
End Sub
Private Sub BtnRowCountDown_Click(sender As Object, e As EventArgs) Handles BtnRowCountDown.Click
Dim iRowCount As Integer = CInt(Me.RowCount.Text)
If iRowCount > cRowCountMin Then
iRowCount -= 1
Me.RowCount.Text = iRowCount.ToString
Me.GridView1.PageSize = iRowCount
Me.GridView1.DataBind()
End If
End Sub
Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
...
'
' Fill datatable
'
...
Session("dt") = dt
End Sub
...
End Class
【问题讨论】:
标签: html asp.net vb.net button gridview