grid view multiple row selection with pagination - c#

I have a Gridview with three or more pages' worth of data. The user will check checkboxes in all these pages and click the submit button. The checked values in all the pages need to be stored in the database.
The problem is only the selected page's values got stored to the database and not the checked values on other pages.

I'm assuming you're using Web Forms. I think pagination in a GridView causes a post back event, which means any state that isn't in the server side is lost.
You need to store which entries are checked in server state somehow (putting a Dictionary or List in the ViewState for example) either each time you check a box, or each time you page. Then, when you page, you should use the saved state to restore the check box values on the current page.

Related

How to maintain the state of a page which contains controls even after postback in asp.net

I have a page with dropdowns and grid view control. User selects the drop downs and gridview gets populated. I am providing rating for each row in grid view. when user trys to rate the row, I need to redirect him to the login page.Once he logins he needs to be redirected back to the same page with all the dropdowns and grid populated.
To achieve this how to maintain the state and what is the best approach.
Please help me.Thank you
All depends on size of grid and the number of users?
If the grid is small and there are not alot of users on the site you can store the Grids DataSource into Session object then check on pageload if the object exist then prepopulate the grid. (also store the selected dropdown value and set it)
If the grid is large and/or you will have many users.
You could just store the dropdown list selected value in a cookie (or session) then on page load check if that cookie exist and if so set dropdown list and reload Grid data from database
You have different choices here.
Save datasource of gridview in Session state (Daveo suggestion)
Problem with this is that it expires and depending on the size of the table might have problems with memory resources
Redirect to login page with query string
a. Something like window.location="../loginpage.aspx?value1=''&value2=''"
b. Then on login button click check if value1 is not null redirect again to your page with something like window.location="../userratingpage.aspx?value1=''&value2=''"
c. Populate gridview using the querystring values
Another suggestion would be is to have a sort of popup screen for the login rather than redirecting the page. You would no longer have problems retaining the state and it would look more interactive. Just like "login with facebook" functionality of other sites
Ajax Modal pop up will give you more choices to customize instead of others.
Here is a sample of it.
http://www.codeproject.com/Articles/24924/Login-SignUp-Screen-Using-AJAX-ModalPopupExtender

How to load viewstate before page render

I create some tables dynamically in C#, and they are giving me viewstate issues. I have a checkbox table and a number entry table that depends on the first. When I press a button that causes postback, I get some problems. Here is a flow chart.
Page_Load
create checkbox table
create number entry table (check box is always empty, table has wrong number of rows, but numbers are populated correctly from postback)
Postback button
create number entry table (check box is now populated, table has correct number of rows, but numbers did not survive postback).
Is there a way I can load the viewstate to the checkbox table before I create the number entry table? I have tried to create the number entry tables in one or the other place (on page load or postback button method), but neither is completely correct. I know that dynamic content needs to be created on every load for the viewstate to appear.
I suspect I may need to create a datatable and store that in the viewstate for the checkboxes to reappear correctly...
I would add your dynamic controls in the PreInit or Init event of your page. Although you can add them afterwards, after the Init phase is when ViewState data is loaded for controls.
If you add your controls during the Page_Load the control has to play catch up, which means it runs through all of the events in the page lifecycle until it catches up with the rest of the page. This might be causing the issue you're seeing.
Here's an excellent write-up regarding the Page Lifecycle and ViewState:
http://www.codeproject.com/Articles/24611/ASP-NET-Internals-Viewstate-and-Page-Life-Cycle

ASP.NET VS2008 C# - dropdown list - postback

How can use a dropdown list without the autopostback=true.
The value on the server is not being changed according to the one selected from the client side. As I already stated I do not wish that for each dropdown I have the autopostback will trigger a post back.
Any time I have lost the value of the drop-down it is because I messed up and repopulated the drop down before handling the value change. For me, it has been drop-downs that I need to do something special with like add item attributes for Javascript, etc. This is data that needs to be added on every page load (aka data that is not persisted in the drop down like the names and values of each item). In these cases I have done this work on load, then I try to retrieve the value later in the page lifecycle and DOH!
Here is the page lifecycle:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Dollars to donuts that is what is happening. You are probably just reloading the items before you get to handling whatever postback event you are using to grab the value. If you are doing this and cannot get around this work flow, just save the selected index at the beginning of the logic that populates the drop-down, then set the selected index of the drop down with that value when done.
it'll be saved in the viewstate, so the value will be correct when you do eventually post back, and if you're really desperate to get the current value without a postback, javascript would be the way to do this.
Worst case you can grab the value right off the request object:
string selectedID = Request[DropdownControl.UniqueID];
You should make sure you are only filling the select box with options during the initial page load, and not again during the postback
if (!this.Page.IsPostBack) {
//fill select box here
}

How to save user entered data to session state from a dynamically created control?

using c# (asp.net)
i'm programmatically creating several drop down lists (random number).
the user selects a particular value from each list and i need to save the user selected value.
if i try to save the data to session state by using a button click event it says the drop down list object hasn't been created. (obviously cuz i'm creating the drop down lists in the page load event under !IsPostBack.)
if i try to save the data to session state in the page load event under IsPostBack i only get the first value from each list. (obviously cuz when the page is recreated after the postback, the drop down lists have been recreated and the user entered data is lost).
How do i save the user selected value from the drop down lists when a button is clicked?
i'm doing this in the code behind file.
As John Saunders said, you have to recreate all of your DropDownList controls on every postback. Additionally, you have to create them before state is restored. That means that "under !IsPostBack" in the page load event is already too late. Move it up to Page Init.
You have to recreate all of the dropdowns on every postback.
Personally (without the ability to switch to MVC) I would consider getting rid of the postbacks altogether and reading the values back directly from a standard HTTP POST request.
The implementation of dynamic controls with postbacks often ends up rather convoluted and difficult to follow

Repeater pagination with checkboxes issue

I have a repeater that displays data from database, each item has a checkbox that are used to "mark" items for deletion and etc. (there is no item in database for chekbox!).
Because I use pagination (on data access and presentation levels), there is no way to preserve checkboxes values between page movements.
How can I solve this issue?
The way I've solved this in the past is to have a handler on the checkbox that does some AJAX back to the server to store the state of that checkbox when it is toggled. This information gets stored in the user's session. During paging, I check the state for each checkbox and set it appropriately as the page is rendered. Any actions that depend on the state of the checkbox use the information from the session. Once the action is completed, I remove the state from the session. You'll also have to figure out how you want to handle this with respect to page navigation as well.

Categories

Resources