473,245 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

Disappearing GridView - Newsgroup???

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 EnableSortingAndPageCallbacks parameter from the script
below and add a PageIndexChanging 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 EnableSortingAndPageCallbacks is
enabled.
Protected Sub AuthorsGridView_PageIndexChanging(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles
AuthorsGridView.PageIndexChanging

AuthorsGridView.PageIndex = e.NewPageIndex

bindDataToGridView()

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 bindDataToGridView()
'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.SqlClient.SqlConnection("server=localh ost;database=pubs;integrated
security=SSPI")
Dim cmd = New System.Data.SqlClient.SqlCommand(strSQL, con)
Dim ad As New System.Data.SqlClient.SqlDataAdapter(cmd)

Dim ds As New Data.DataSet
'Dim dt As New Data.DataTable

ad.Fill(ds)
'ad.Fill(dt)

AuthorsGridView.DataSource = ds
'AuthorsGridView.DataSource = dt
AuthorsGridView.DataBind()
End Sub

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
bindDataToGridView()
End If
End Sub

</script>

<html>
<body>
<form id="Form1" runat="server">

<h3>Disappearing GridView Example</h3>

<asp:gridview id="AuthorsGridView"
autogeneratecolumns="False"
AllowPaging="True"
EnableSortingAndPagingCallbacks="True"
PageSize="3"
runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="au_fname" HeaderText="First Name">
</asp:BoundField>
<asp:BoundField DataField="au_lname" HeaderText="Last Name">
</asp:BoundField>
<asp:BoundField DataField="City" HeaderText="City">
</asp:BoundField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:gridview>
</form>
</body>
</html>


Apr 16 '07 #1
4 2697

Don Miller wrote:
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?
Hi,
The GridView disappers because it is empty. If you want to use a
callback to change the page set GridView's datasource in the page load
event handler. You don't need to process PageIndexChanging event, it
is not raised.

<asp:GridView ID="AuthorsGridView" runat="server" AllowPaging="True"
EnableSortingAndPagingCallbacks="True" PageSize="3">
......
</asp:GridView>

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
bindDataToGridView()
End If

If Not IsCallback Then
......
AuthorsGridView.DataSource = ds
'AuthorsGridView.DataBind() Don't bind here!
End If
End Sub

Regards

Apr 16 '07 #2
Thanks very much for the reply. What I'm really trying to do is to
dynamically change the GridView by changing (on command) the SELECT
statement for the GridView. Initially, the GridView is empty until a command
is chosen (with a hyperlink button). In the actual application the GridView
is only one of several on a page that are hidden by changing Views. I would
like to do this (AJAX paging) with a SQL datasource by dynamically changing
the SELECT statement but I run into the same disappearing GridView when I
tried that (see example below)

I appreciate your help and I obviously don't understand how these things
work but I've already spent hours of reading and trial and error and seem no
closer. Thanks again.

<%@ Page language="VB" %>

<script runat="server">

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
GridViewSQLDataSource.SelectCommand = "SELECT [au_fname],
[au_lname], [city] FROM [authors] WHERE [city] = 'Oakland'"
End Sub

Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
GridViewSQLDataSource.SelectCommand = "SELECT [au_fname],
[au_lname], [city] FROM [authors]"
End Sub
</script>

<html>
<body>
<form id="Form1" runat="server">

<h3>Disappearing GridViews</h3>

<asp:LinkButton ID="LinkButton1" runat="server"
OnClick="LinkButton1_Click">Oakland Only</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server"
OnClick="LinkButton2_Click">All Cities</asp:LinkButton>

<asp:gridview id="AuthorsGridView"
autogeneratecolumns="False"
AllowPaging="True"
EnableSortingAndPagingCallbacks="True"
PageSize="3"
DataSourceID = "GridViewSQLDataSource"
runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="au_fname" HeaderText="First Name">
</asp:BoundField>
<asp:BoundField DataField="au_lname" HeaderText="Last Name">
</asp:BoundField>
<asp:BoundField DataField="City" HeaderText="City">
</asp:BoundField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:gridview>

<asp:SqlDataSource ID="GridViewSQLDataSource"
ConnectionString="server=localhost;database=pubs;i ntegrated
security=SSPI"
runat="server">
</asp:SqlDataSource>

</form>
</body>
</html>
Apr 16 '07 #3

Don Miller wrote:
Thanks very much for the reply. What I'm really trying to do is to
dynamically change the GridView by changing (on command) the SELECT
statement for the GridView. Initially, the GridView is empty until a command
is chosen (with a hyperlink button). In the actual application the GridView
is only one of several on a page that are hidden by changing Views. I would
like to do this (AJAX paging) with a SQL datasource by dynamically changing
the SELECT statement but I run into the same disappearing GridView when I
tried that (see example below)

Save the chosen select command text in the ViewState and assign it to
GridViewSQLDataSource.SelectCommand in the page load event handler.
Example:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If (IsCallback) Then
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
End If
End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.SelectCommand = "SELECT .... "
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
End Sub

Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.SelectCommand = "SELECT ...."
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
End Sub

Private Property SelectCommand() As String
Get
Return ViewState("SelectCommand").ToString()
End Get
Set(ByVal value As String)
ViewState("SelectCommand") = value
End Set
End Property

Regards

Apr 17 '07 #4
Thank you so much! I'm still learning and quite sure I wouldn't have come up
that solution using ViewState or even know where to place the code in the
Event cascade (onLoad, onCallback, GridView init/load, etc.).

It still seems like a bug to me that the SELECT command for the DataSource
has to be reset again with a Callback. It is set for the GridView each time
a link button was pressed and it seems like that should be enough. Why
should a dynamic SELECT statement assigned to a DataSource get trashed on a
Callback? This solution (thanks again) seems like a hack to get around this
unexpected behavior. I mean, why should anyone expect that you have to do
this?

Anyway, I did find I had to add one line of code to reset the PageIndex back
to 0 when a new SELECT is displayed to keep the Pages straight.

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.SelectCommand = "SELECT [au_fname], [au_lname], [city] FROM
[authors] WHERE [city] = 'Oakland'"
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
AuthorsGridView.PageIndex = 0
End Sub

"marss" <ma******@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
>
Don Miller wrote:
>Thanks very much for the reply. What I'm really trying to do is to
dynamically change the GridView by changing (on command) the SELECT
statement for the GridView. Initially, the GridView is empty until a
command
is chosen (with a hyperlink button). In the actual application the
GridView
is only one of several on a page that are hidden by changing Views. I
would
like to do this (AJAX paging) with a SQL datasource by dynamically
changing
the SELECT statement but I run into the same disappearing GridView when I
tried that (see example below)


Save the chosen select command text in the ViewState and assign it to
GridViewSQLDataSource.SelectCommand in the page load event handler.
Example:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If (IsCallback) Then
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
End If
End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.SelectCommand = "SELECT .... "
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
End Sub

Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.SelectCommand = "SELECT ...."
GridViewSQLDataSource.SelectCommand = Me.SelectCommand
End Sub

Private Property SelectCommand() As String
Get
Return ViewState("SelectCommand").ToString()
End Get
Set(ByVal value As String)
ViewState("SelectCommand") = value
End Set
End Property

Regards

Apr 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rachel Suddeth | last post by:
Here is my scenario: I have a few custom controls that I set up on a form and tested setting properties and appearances. Then I added a couple references to the project which add classes I need to...
0
by: Pathogenix | last post by:
Greetings, I needed a GridView with an optional extra row in the THEAD and TFOOT. I extended GridView and used Dino Esposito's CreateRow overload...
2
by: | last post by:
While I was learning about baking cookies in ASP.NET, I also ran across this interesting article that outlined a few bombs to watch out for when using cookies in ASP.NET: ...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
2
by: =?Utf-8?B?dmFuZGls?= | last post by:
I have a web app that I have been working on for the last couple of weeks trying to solve this problem. One page contains a GridView with four base columns, and an unknown number of columns to be...
0
by: Don Miller | last post by:
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...
1
by: =?Utf-8?B?Um9iYnlE?= | last post by:
I'm having trouble picking up the checked state of checkboxes in my GridView. I'm working in VS2008. I added a TemplateField via smartTag "Edit Columns". My GridView markup is: <asp:GridView...
4
by: Ken Fine | last post by:
I'm making an administrative interface that lists records in a GridView. For *each* row in the gridview, I would there to be two interface elements in addition to some information associated with...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.