This is a repost of a reproducible problem/bug with GridView with dynamic
SQL and binding. Is there a better ASP.NET newsgroup I should post to where
MS techs or MVPs take an interest in such problems?
Thanks.
Here is an example of what I believe is a bug in ASP.NET 2.0 GridView paging
without postbacks (or at least not documented how to fix it). Once the
GridView is displayed, clicking on any of the paging tools causes the
GridView to completely disappear! This happens when either DataSets or
DataTables are used (it doesn't work at all for DataReaders because they
don't support paging apparently).
If you remove the EnableSortingAn dPageCallbacks parameter from the script
below and add a PageIndexChangi ng sub, the paging works fine except there is
a postback for every page. I would prefer to NOT have a postback and instead
use the the AJAX-like callbacks when EnableSortingAn dPageCallbacks is
enabled.
Protected Sub AuthorsGridView _PageIndexChang ing(ByVal sender As Object,
ByVal e As System.Web.UI.W ebControls.Grid ViewPageEventAr gs) Handles
AuthorsGridView .PageIndexChang ing
AuthorsGridView .PageIndex = e.NewPageIndex
bindDataToGridV iew()
End Sub
So, how can you use GridView with callback paging without postbacks? What's
missing from the code and why shouldn't the code work? And where is the
documentation for avoiding this problem?
Thanks for any help.
<%@ Page language="VB" %>
<script runat="server">
Private Sub bindDataToGridV iew()
'This example uses Microsoft SQL Server and connects
Dim strSQL As String = "SELECT [au_fname], [au_lname], [city] FROM
[authors]"
Dim con As New
System.Data.Sql Client.SqlConne ction("server=l ocalhost;databa se=pubs;integra ted
security=SSPI")
Dim cmd = New System.Data.Sql Client.SqlComma nd(strSQL, con)
Dim ad As New System.Data.Sql Client.SqlDataA dapter(cmd)
Dim ds As New Data.DataSet
'Dim dt As New Data.DataTable
ad.Fill(ds)
'ad.Fill(dt)
AuthorsGridView .DataSource = ds
'AuthorsGridVie w.DataSource = dt
AuthorsGridView .DataBind()
End Sub
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
bindDataToGridV iew()
End If
End Sub
</script>
<html>
<body>
<form id="Form1" runat="server">
<h3>Disappearin g GridView Example</h3>
<asp:gridview id="AuthorsGrid View"
autogeneratecol umns="False"
AllowPaging="Tr ue"
EnableSortingAn dPagingCallback s="True"
PageSize="3"
runat="server" CellPadding="4" ForeColor="#333 333" GridLines="None ">
<Columns>
<asp:BoundFie ld DataField="au_f name" HeaderText="Fir st Name">
</asp:BoundField>
<asp:BoundFie ld DataField="au_l name" HeaderText="Las t Name">
</asp:BoundField>
<asp:BoundFie ld DataField="City " HeaderText="Cit y">
</asp:BoundField>
</Columns>
<RowStyle BackColor="#E3E AEB" />
<PagerStyle BackColor="#666 666" ForeColor="Whit e"
HorizontalAlign ="Center" />
<HeaderStyle BackColor="#1C5 E55" Font-Bold="True"
ForeColor="Whit e" />
<AlternatingRow Style BackColor="Whit e" />
</asp:gridview>
</form>
</body>
</html>