473,396 Members | 2,002 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,396 software developers and data experts.

Help with GridView postback (asp.net c# web app)

32
Hi,

I am new to gridviews, so any help would be most appreciated. Here is my problem:-

I have a form with a GridView and a button.
My gridview is bound to a collection (12000 items) (PageSize = 200) and it has 4 columns:

1st Col =

Expand|Select|Wrap|Line Numbers
  1. <asp:TemplateField>
  2.        <ItemTemplate>
  3.              <asp:CheckBox ID="_isSelected" runat ="server" />
  4.        </ItemTemplate>
  5. </asp:TemplateField>
2nd – 4th Col’s are BoundFields.


The user can select the multiple rows by clicking the checkbox and then when they click the button on the form I want to loop through the gridview & find which rows have been selected, so that I can be selective on which data I show on another form.

But once the user clicks the button the form is posted back & therefore my gridview is empty, so I can’t do my code as below:-

Expand|Select|Wrap|Line Numbers
  1. foreach (GridViewRow row in _gridView.Rows)
  2.             {
  3.                 //access the check box & find out which items have been selected
  4.                 CheckBox checkBox = (CheckBox)row.FindControl("_isSelected");
  5.                 if (checkBox != null && checkBox.Checked)
  6.                 { 
  7.                     //create a new entity
  8.                     Entity selectedEntity = new Entity();
  9.  
  10.                     //find the entity from the collection using its guid (which is a hidden column)
  11.                     selectedEntity = _entities.GetEntityByGuid(row.Cells[3].Text);
  12.  
  13.                     //add the individual entity to the entities collection
  14.                     selectedEntities.Add(selectedEntity);
  15.                 }
  16.             }
Is there another way to get around my problem?

Many thanks
Janet
Jun 14 '07 #1
12 27135
nateraaaa
663 Expert 512MB
In which event do you have the code that you posted? Is it in the click_event of the button or somewhere else?

Nathan
Jun 14 '07 #2
dixonjm
32
Yes - it is in the click event of the button where I am trying to loop through the gridview
Jun 14 '07 #3
nateraaaa
663 Expert 512MB
Yes - it is in the click event of the button where I am trying to loop through the gridview
I guess the part that I am a bit confused on is when you say that your gridview is empty after the code in your button click event executes. Is the gridview that the user selected the items from empty or is this another gridview that you are talking about?

Nathan
Jun 14 '07 #4
dixonjm
32
I guess the part that I am a bit confused on is when you say that your gridview is empty after the code in your button click event executes. Is the gridview that the user selected the items from empty or is this another gridview that you are talking about?

Nathan
Ok, here's a better explanation, hopefully...

I have a watch on my _gridView & on the first page_load event it shows _gridView.rows.count =0, if it isn't a post back I databind the _gridView to my collection (watch shows that _gridView.rows.count = 200) & then it is rendered to the user.
The user clicks multiple checkboxes (from the _gridView) and then clicks a button on the form which fires the page_load event again at which point my watch on the _gridview show that the _gridView.rows.count now = 0. I dont bind the data again, becuase its a postback - obviously, & the click event on the button is then raised, but I have no rows in my gridview.
Jun 14 '07 #5
nateraaaa
663 Expert 512MB
Ok, here's a better explanation, hopefully...

I have a watch on my _gridView & on the first page_load event it shows _gridView.rows.count =0, if it isn't a post back I databind the _gridView to my collection (watch shows that _gridView.rows.count = 200) & then it is rendered to the user.
The user clicks multiple checkboxes (from the _gridView) and then clicks a button on the form which fires the page_load event again at which point my watch on the _gridview show that the _gridView.rows.count now = 0. I dont bind the data again, becuase its a postback - obviously, & the click event on the button is then raised, but I have no rows in my gridview.
So you only want the gridview to show after the intial page_load correct? So everytime the page loads after the first your gridview should be populated by a datasource and the .DataBind() event called. If this is the case I am not sure how you are not getting any data in your gridview. If you are not doing this try calling the databind event on every page load and see if there are any rows in your gridview.

Nathan
Jun 14 '07 #6
dixonjm
32
i had viewstate switched off, when i switch it on & run through my code, when the click event of the button is fired I have rows in my datagrid!

Thanks
Jun 14 '07 #7
TRScheel
638 Expert 512MB
Ok, here's a better explanation, hopefully...

I have a watch on my _gridView & on the first page_load event it shows _gridView.rows.count =0, if it isn't a post back I databind the _gridView to my collection (watch shows that _gridView.rows.count = 200) & then it is rendered to the user.
The user clicks multiple checkboxes (from the _gridView) and then clicks a button on the form which fires the page_load event again at which point my watch on the _gridview show that the _gridView.rows.count now = 0. I dont bind the data again, becuase its a postback - obviously, & the click event on the button is then raised, but I have no rows in my gridview.
As nathan mentioned above, issues like this required a databind on every page load.
Jun 14 '07 #8
TRScheel
638 Expert 512MB
i had viewstate switched off, when i switch it on & run through my code, when the click event of the button is fired I have rows in my datagrid!

Thanks
Interesting, viewstate of what? The datagrid?
Jun 14 '07 #9
dixonjm
32
the aspx page that contains my gridview & my button.

Why would I databind on everypage load - how would I then know which checkbox have been ticked on the gridview, surely by databinding on every page load the users selection will be overritten?
Jun 14 '07 #10
TRScheel
638 Expert 512MB
the aspx page that contains my gridview & my button.

Why would I databind on everypage load - how would I then know which checkbox have been ticked on the gridview, surely by databinding on every page load the users selection will be overritten?
Actually, after you said that, I went and looked up my code where I did the databind every page, and realized that I changed my methodology for some reason and I forget why now.

Trying to remember though, I think my issue was with current gridview page, not with user selection, and the databinding every page fixed it... but like I said, I changed how I went about doing it.
Jun 14 '07 #11
nateraaaa
663 Expert 512MB
i had viewstate switched off, when i switch it on & run through my code, when the click event of the button is fired I have rows in my datagrid!

Thanks
Great. Problem solved. I mentioned rebinding the data each time the page loads because the example I was looking at had users select rows for deletion then a javascript popup window would display to the user asking them if they really wanted to delete the selected rows. I would then do a databind again so that the selected rows would be removed from the grid.

Nathan
Jun 14 '07 #12
dixonjm
32
Thanks...

My code is now working, kinda, how I want ?! The only problem I now have is when I loop through the gridview (see code snippet below) my _gridView.Rows = 200, but I have 12,000 items in the grid (Each page shows 200 items). I want the user to be able to select checkboxes on any of the gridview pages and for my code to handle paging.

Expand|Select|Wrap|Line Numbers
  1.             EntityCollection selectedEntities = new EntityCollection();
  2.  
  3.             //iterate through the grid to see which check boxes have been selected
  4.             foreach (GridViewRow row in _gridView.Rows)
  5.             {
  6.                 //access the check box & find out which items have been selected
  7.                 CheckBox checkBox = (CheckBox)row.FindControl("_isSelected");
  8.                 if (checkBox != null && checkBox.Checked)
  9.                 { 
  10.                     //create a new entity
  11.                     Entity selectedEntity = new Entity();
  12.  
  13.                     //find the entity from the collection using its guid (which is stored in the data key name field)
  14.                     selectedEntity = _entities.GetEntityByGuid(_gridView.DataKeys[row.RowIndex].Value.ToString());
  15.  
  16.                     //add the individual entity to the entities collection
  17.                     selectedEntities.Add(selectedEntity);
  18.                 }
  19.             }
  20.             //check to see if there have been any entities added to the chosen entities list
  21.             //if so - add then into Session State so they can be retrieved easily
  22.             if (selectedEntities.Count > 0)
  23.             {
  24.                 Session["SelectedEntities"] = selectedEntities;
  25.                 Response.Redirect("SelectedOwnSites.aspx"); //to display the filtered list of selected entities
  26.             }
  27.             else
  28.             { 
  29.                 //display message - no items selected
  30.             }
How do I change the above code too loop through all the items in the gridview?
Jun 15 '07 #13

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

Similar topics

0
by: yeltsin27 | last post by:
I need some advice on handling dynamically added controls in a GridView. My app takes an uploaded CSV file containing addresses, converts it to a DataTable, databinds the DataTable to a...
1
by: jmdolinger | last post by:
Hi all, I'm a newbie to Atlas (and recently ASP.NET) after coming from a long Java background, also have done quite a bit with an Ajax.NET/ASP.NET 1.1 project, but it was basically all...
3
by: AG | last post by:
Below is code (slightly modified and converted to VB) that was provided to me in response to another post. I am using it to demonstrate another problem. In order for paging and other features to...
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...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
0
by: F | last post by:
Hi, The funniest part of the problem described here is that I am not new to ASP.NET, but honestly here I have no idea about what's going on. I have a GridView with a data source that just...
0
by: Eraser | last post by:
Hi to all .NET guru guys... I have a problem in my delete button inside gridview. How to avoid postback on when i select cancel on confirmation message? But postback is okay on Ok confirmation....
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.