473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Apr 16 '07 #1
4 2716

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 PageIndexChangi ng event, it
is not raised.

<asp:GridView ID="AuthorsGrid View" runat="server" AllowPaging="Tr ue"
EnableSortingAn dPagingCallback s="True" PageSize="3">
......
</asp:GridView>

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

If Not IsCallback Then
......
AuthorsGridView .DataSource = ds
'AuthorsGridVie w.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_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
GridViewSQLData Source.SelectCo mmand = "SELECT [au_fname],
[au_lname], [city] FROM [authors] WHERE [city] = 'Oakland'"
End Sub

Protected Sub LinkButton2_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
GridViewSQLData Source.SelectCo mmand = "SELECT [au_fname],
[au_lname], [city] FROM [authors]"
End Sub
</script>

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

<h3>Disappearin g GridViews</h3>

<asp:LinkButt on ID="LinkButton1 " runat="server"
OnClick="LinkBu tton1_Click">Oa kland Only</asp:LinkButton>
<asp:LinkButt on ID="LinkButton2 " runat="server"
OnClick="LinkBu tton2_Click">Al l Cities</asp:LinkButton>

<asp:gridview id="AuthorsGrid View"
autogeneratecol umns="False"
AllowPaging="Tr ue"
EnableSortingAn dPagingCallback s="True"
PageSize="3"
DataSourceID = "GridViewSQLDat aSource"
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>

<asp:SqlDataSou rce ID="GridViewSQL DataSource"
ConnectionStrin g="server=local host;database=p ubs;integrated
security=SSPI"
runat="server">
</asp:SqlDataSour ce>

</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
GridViewSQLData Source.SelectCo mmand in the page load event handler.
Example:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
If (IsCallback) Then
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
End If
End Sub

Protected Sub LinkButton1_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
Me.SelectComman d = "SELECT .... "
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
End Sub

Protected Sub LinkButton2_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
Me.SelectComman d = "SELECT ...."
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
End Sub

Private Property SelectCommand() As String
Get
Return ViewState("Sele ctCommand").ToS tring()
End Get
Set(ByVal value As String)
ViewState("Sele ctCommand") = 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_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
Me.SelectComman d = "SELECT [au_fname], [au_lname], [city] FROM
[authors] WHERE [city] = 'Oakland'"
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
AuthorsGridView .PageIndex = 0
End Sub

"marss" <ma******@gmail .comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.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
GridViewSQLData Source.SelectCo mmand in the page load event handler.
Example:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
If (IsCallback) Then
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
End If
End Sub

Protected Sub LinkButton1_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
Me.SelectComman d = "SELECT .... "
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
End Sub

Protected Sub LinkButton2_Cli ck(ByVal sender As Object, ByVal e As
System.EventArg s)
Me.SelectComman d = "SELECT ...."
GridViewSQLData Source.SelectCo mmand = Me.SelectComman d
End Sub

Private Property SelectCommand() As String
Get
Return ViewState("Sele ctCommand").ToS tring()
End Get
Set(ByVal value As String)
ViewState("Sele ctCommand") = 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
2602
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 get data from the server to actually do something useful. (These are generated by 3rd party database software.) After adding those references, and no other changes to my form.... all the controls disappear from the visual interface on the...
0
988
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 http://weblogs.asp.net/despos/archive/2004/11/30/272147.aspx] then added a new template for the header. I overrode CreateChildControls like this protected override void CreateChildControls ()
2
1575
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: http://www.codeproject.com/aspnet/aspnetcookies.asp The "disappearing cookie" syndrome that the author describes seems problematic: <quote> If you try to access a cookie that doesn't exist in the
7
14794
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 want to look at that data and filter it based on what is in it. I know that this could have been done with data sets and data views in asp.net 1.1 but how is this done now in asp.net 2.0?
2
16996
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 added depending on the data source chosen. I create some TemplateColumns based on the data returned for the selection, and add them into the GridView using the following code: DataSet dsTraits = ssql_common.selectRubricTraits(sConnectionString,...
0
1998
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 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...
1
1260
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 ID="GridViewProspectSearch" runat="server" CellPadding="4" EmptyDataText="No prospects found." ForeColor="#333333" GridLines="None"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
4
3004
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 the record. Those interface elements are a Button and a TextBox. The idea is that the administrator using the interface will fill in an e-mail address and press the button if they want to send that particular record to someone by e-mail. (I know...
3
5228
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 DataTable, I am using List of Objects. Here's one example: http://ryanolshan.com/technology/gridview-without-datasourcecontrol-datasource/
0
8015
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7951
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.