472,122 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 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 26879
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

Post your reply

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

Similar topics

1 post views Thread by jmdolinger | last post: by
3 posts views Thread by AG | last post: by
2 posts views Thread by =?Utf-8?B?dmFuZGls?= | last post: by
1 post views Thread by Evan M. | last post: by
reply views Thread by F | last post: by
4 posts views Thread by Craig Buchanan | last post: by
11 posts views Thread by SAL | last post: by
reply views Thread by leo001 | last post: by

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.