Repeater pagination with checkboxes issue - c#

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.

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

grid view multiple row selection with pagination

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.

Where should I populate my dropdownlist so that it retains its selection?

I have seen posts which recommend that I populate the list within a if(!IsPostback) block.
However, I only generate the dropdown lists in response to postbacks so I dont think that will work for me.
What I currently do is databind the dropdownlists in Page_Init. However, when I inspect the list after a new selection is made (and therefore the autopostback has been triggered) using "inspect element" I see that the first item in the list is always "selected."
Please let me know how to keep the selection selected through a postback.
If you bind the control in Page_Init, then you'll lose your selection. Don't do that.
Bind the control once, then ViewState will retain the control data. But when the selection changes, that will be sent to the server, and the SelectedIndexChanged event will fire.
More generally, any properties you set on a control before the Render phase will be saved in ViewState. On the next Post Back, the control will load the contents of ViewState, and therefore will restore itself to the state it was in before the PostBack.
The control will then take note of any POSTed values that were sent on the PostBack. This includes changes in the selection, textbox contents, etc. The control will fire the appropriate somethingChanged event to indicate that there has been a change from the prior state of ViewState.

Issue with .net dynamic control generation

I'm working on an asp.net website (using C#). I have implemented a detailsview as part of a data entry system for this website.
The detailsview contains a drop down list used to associate a category with the record being submitted to this data entry system.
The code behind file accesses a datasource (an SQL server 2005 database table), to determine the fields associated with a selected category and to generate checkbox controls based upon the fields available in that category
I understand (I think) the .net page lifecycle, and the necessity to add dynamic controls on each postback to maintain the controls and their "state". However:
I've read that I must add dynamic controls in the Page_Init/initialisation phase of the page lifecycle, in order for the dynamic controls properties and events to be available upon a postback
The value I require to query the datasource (and to determine the number and names of the dynamic controls for a category selection) is assigned in the dropdown list's SelectedIndexChanged event handler, which is always processed after the Page_init event
I'm not sure how I can pass the required value (the dropdown list's selected index) to the Page_Init event at the correct point in the page lifecycle (the Page_init event).
I would greatly appreciate any pointers/assistance from the stackoverflow community
and thank you for taking the time to read this post.
You do not have to add the controls in the init, you can add them in page_load just fine as well. It is often recomended to add them in the init as this is the point in the page lifecycle that controls defined in the markup are instantiated. Why do you need to assign the value to determine whether the controls should be added in the SelectedIndexChanged event. If it is based on the SelectedValue of a drop down list, can you not simply access the SelectedValue and assign the value on each post back, even if it has not changed. Then you could do it in the Page_Load and then add your controls afterwards.
The value you are after is posted back to the server and can be found in Request.Form NameValueCollection. The key is the name of the dropdown list.

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

Categories

Resources