473,320 Members | 1,930 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,320 software developers and data experts.

Calling bind function in gridview page event handler makes GridView disappear

Hello,
Sorry for putting this problem back on the list. I had it previously under another title that now does not describe the real problem. Hopefully the new title will trigger someone that has had the same problem to respond...

The problem:

When I want to select page 2 in my grid view control, the grid view disappear.
After some trail and error (with good help from others on this network) it seems like the problem appears when the bind function is called from the event handler.
It seems that the page index is set properly, while if I store it in a session variable that is read by the function that fill in the grid on page load (FillInGridView), and then put the 'FillInGridView' function outside the 'if (!IsPostBack)' test so that it is called each time any contol causing post back is selected, the paging works.

However, I do not want to call the db each time any control is changed. Only when the next page is called.

Any ideas anybody?

Below, I've first pasted what works, then what does not work.

This works, but is a poor solution that I cannot live with:

void FillInGridView()
{
string myConnString = ConfigurationSettings.AppSettings["DSN_Fyrr"];
MySqlConnection myConnection = new MySqlConnection(myConnString);
string strSQL = "SELECT LocationName FROM PartLocationDef ORDER BY LocationName;" ;
MySqlCommand myCommand = new MySqlCommand(strSQL, myConnection);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "FailTypeDef");
g_GridCtrl.DataSource = myDataSet;
g_GridCtrl.PageIndex = (Int32)Session["GridPageIndex"];
Session["MyDataSett"] = myDataSet;
g_GridCtrl.DataBind();
myConnection.Close();
}

void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["GridPageIndex"]=0;
}
FillInGridView();
}

void GridCtrl_IndexChange(object sender, GridViewPageEventArgs e)
{
g_GridCtrl.PageIndex = e.NewPageIndex;
Session["GridPageIndex"]=g_GridCtrl.PageIndex;
FillInGridView();
}


This does not work (makes the grid disappear when page 2 is selected), but would be a better solution:

void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillInGridView();
}
}


void GridCtrl_IndexChange(object sender, GridViewPageEventArgs e)
{
g_GridCtrl.PageIndex = e.NewPageIndex;
g_GridCtrl.DataSource = (DataSet)Session["MyDataSett"];
g_GridCtrl.DataBind();
}
Jun 29 '07 #1
3 6936
Hello,
Finally I found the bug!
It seems like the GridView control declaration
EnableSortingAndPagingCallbacks has to be set to false.

Best regards
Kurt
Jun 29 '07 #2
Plater
7,872 Expert 4TB
If you are using the built in paging of the gridview object, you do not need to supply the datasource again.
Here is an example I use:
Expand|Select|Wrap|Line Numbers
  1. protected void gvIssuesSites_PageIndexChanging(object sender, GridViewPageEventArgs e)
  2. {
  3.         gvIssuesSites.PageIndex = e.NewPageIndex;
  4.         gvIssuesSites.DataBind();
  5. }
  6.  
I have had no trouble with this. Now then, if you have more then one gridview on the screen and you want to make sure they stay on their "correct page", I used this:
Expand|Select|Wrap|Line Numbers
  1. protected void gvIssuesSites_PageIndexChanging(object sender, GridViewPageEventArgs e)
  2. {
  3.         gvIssuesSites.PageIndex = e.NewPageIndex;
  4.         gvIssuesSites.DataBind();
  5.         Session.Add("gvI", e.NewPageIndex.ToString());
  6. }
  7.  
I just add a quick session value that tells me what page the user was looking at.
To reference it, in the Page_Load function I also added:
Expand|Select|Wrap|Line Numbers
  1. gvIssuesSites.DataSource = isites;//isites is my datasource
  2. if (Session["gvI"] != null)
  3. {
  4.    gvIssuesSites.PageIndex = int.Parse((string)Session["gvI"]);
  5. }
  6. gvIssuesSites.DataBind();
  7.  
Jun 29 '07 #3
Thankyou friend for your rare help......It fixed my problem ...u saved great pain,which was due, to occur.if I could't succeed
Jul 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Eran Amitai | last post by:
I have the simplest web page (using code-behind): <body> <%# MyText %> two buttons - button1 and button2 </body> My code-behind web page is very simple too: public class WebForm3 :...
2
by: Lee | last post by:
Hello all, Fairly new to .net. Using VB.net/2005 I am having a bit of trouble implementing a data driven gridview with paging. I am binding the grid to a DataTable at runtime (which is...
4
by: Jim Katz | last post by:
I have an application that updates a strongly typed data set at run time. I'd like to dynamically create a table that connects to a run time data table. For displaying the data, this works well. ...
3
by: chelsea | last post by:
I am using a GridView control with a ButtonField and receiving 2 row command events for each click of the button. Is there a configuration switch that I can turn that will ensure I only receive...
3
by: tarscher | last post by:
Hi all, I have a gridview populated with data coming from a dataset (created via the wizard). The data shown in the gridview is dependent on the the choice of a control (dropdown with...
0
by: gnewsgroup | last post by:
Well, I am trying to use the footer row of a GridView for insertion purpose. There are some articles about this, for example, the gridviewguy.com has an example, which always displays the footer...
3
by: gnewsgroup | last post by:
I have a GridView which is bound to my database. The leftmost column of the GridView has a value of either 0 or 1, and I need to highlight the cell of column 3 if the leftmost column has a...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
4
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.