I have a gridview with checkbox control as a template field and one bounded field..
The grid is binded on every postback through a function and the grid contents remain the same on every post back..now when i check one of the checkboxes and then click the button at the end of the page, I need to store that particular row information..but I'm not able to retreive that information because when I check and then click button..the page loads and then the grid again populates and then checkboxes become uncheck and no CheckedChanged event fires..Help me with this
I need to persist the state of checkbox on every postback even when it is checked..how to do this??
In the page_load event function, please use the following code for your persistent data
if (!IsPostBack)
{
//your static data
}
This particular problem is fairly common. I haven't seen any "simple" solution yet, but here are 3 separate methods I have used. Each was used because of a limitation in the system.
Solution 1
Use AJAX. By putting your controls within an update panel, you can persist the changes by making them "real-time" in the database. This is not really a "simple" solution, but in my opinion it is one of the easiest to implement. Since the change is psuedo-immediate, there is no real need to worry about post-backs and persistence.
Solution 2
Use a "change management" control of sorts. You can apply a hidden control whose value is used to keep track of any changes made in relevant controls. You would need to devise a coherent data structure that provide at least a control ID and the new value (possibly the old value if you need some kind of "roll-back" feature). This would need to be coded in JavaScript so that any changes to the hidden control's value were structured and not duplicated. Then on your postback you would need to read this control's value, make any pertinent changes, and then rebind your data as appropriate. This can be fairly cumbersome, and it would need to be well-documented in the event that you pass this application on to a successor.
Solution 3
Use the PostBack for CheckChanged events and keep all data managed in the view state. During the RowItemCreated event of the GridView you can find the checkbox control in the relevant cell and manually add the delegate handler to that control to handle the postback in the event of a CheckChanged event firing. You can then have the change immediate. The drawback to this is that PostBack events become frequent and heavy. If you're storing large amounts of data in the ViewState this also causes page load to be slow and unresponsive, so whatever structure you choose for the ViewState you'll want to keep it small.
This is possible if you are using asp.net 4.0 using
<asp:GridView id="GridView2" runat="server" EnablePersistedSelection="true">
</asp:GridView>
If you are using 3.5, you will have to retain checkbox info in viewstate. I hope this will be helpful.
http://www.codeproject.com/Articles/202938/How-to-select-multiple-records-from-the-GridView-a
Another Option:
This is how msdn have described a hotmail type gridview.. may be this can help.. this will require you to extend existing GridView Control.
http://msdn.microsoft.com/en-us/magazine/cc163612.aspx
Regards.
Related
I'm trying to persist the contents of a textbox through a postback, and I've exhausted all of my effort but can't get it working right.
What should happen is:
User selects a radiobutton
Depending which button was
selection, a usercontrol is loaded
to specify some data and a viewstate
to say which enum type it's
equivalent to.
When they click
save, if the UserControl is just a
textbox input - the simplest), the
contents are read and saved, then
saved to the database with the
format(the radiobutton choice) so
they can be deserialized again
later.
The page posts back, and
the value and format are read from
the database, then the right control
is loaded.
The problem is - the first time the page posts back, it works. Every other postback it resets to the default value of the textbox.
I have a very similar setup elsewhere, so I'm thinking it might be a minor thing I'd never think of. There's a lot of code, so it might be easier to talk about what to do (load the dynamic control, populate the values etc) rather than how to do it.
There was actually a bug in my original code which meant it would never have in the way I was using the modified version. Apparently state is restored in Page_Load, so any controls need to be initialized by to have their values restored.
Creating the control in Page_PreLoad, then populating it after Page_Load solved the problem.
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.
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
My current situation is to display an unknown number of Plantypes and within those Plantypes display a list of Participants(also unknown number), the participants have a textbox and a dropdown that is editable (you can't edit the individual rows, there is one update that does a bit of validation then updates all rows.)
I currently have a gridview nested withing a repeater, the repeater displays the Plan in a label and OnItemDataBound I call a method to populate the gridviews. It looks great, but I can't figure out how to save all the data at once. I'm not opposed to handling this a different way, as in loose the gridview and or repeater, if someone has a better idea.
This is C# and framework 2.0...there is no sorting or paging on the gridviews...just some links and the fields to update.
thanks in advance,
Padawan
HTTP is stateless. Since a user physically can only update one record at a time you should implement some way to save as the user goes. This can be a simple as having a row focus and row blur events and do the saves using AJAX on blur(unfocus). Maybe make is modal as they edit so they cannot leave the page without saving.
On an ASP.NET page, I have a GridView populated with the results of a LINQ query. I'm setting the DataSource in code, then calling DataBind on it. In the GridView's RowDataBound event, I'm selectively hiding links in some GridView fields based on the query results. (For instance, I hide the "Show Parent" link of the row in question has no parent row.)
This works fine initially. But on postback (when I don't call DataBind, but the GridView stays populated through ViewState), the data displays, but the RowDataBound event (obviously) doesn't fire, and my links don't get hidden.
What's the best way to get the links to be hidden after a postback?
The RowDataBound event only fires when the GridView's data changes during the postback. The event is short-circuited for speed so it's not re-generating the exact same data unnecessarily. Use the RowCreated event to manipulate the HTML instead - it fires on every postback regardless of whether the data has changed.
Here's how I ended up solving this:
I created a serializable class with readonly properties: PK of a row, and a boolean for each link indicating whether it's enabled or not. We'll call it LinkVisibility.
I created a serializable class inheriting from KeyedCollection to hold instances of the class above.
I created a ViewState-backed property holding an instance of that collection.
In my Search procedure (populating the GridView), I clear the collection.
In RowDataBound, which initially shows/hides the links, I add a LinkVisibility instance to the collection for each row.
In Page.Load, when IsPostBack is true, I loop through the GridView rows. I look up the LinkVisibility for each one by PK in the collection (DataKeyNames is set in the GridView), and I set the links accordingly.
I don't know that this is the best way to do this, but it certainly does work, which is more than I can say for anything else I've tried.
1) You could have a Method - ProcessDataRows() that would get called once on grid_DataBound(...). And then when you need it after PostBack.
And that way you process all rows when you want.
2) You could have methods like ShowParentLink(). That are then bound to the LinkButton in the grid (if you're using an ItemTemplate) and the link would have
Visible='<%#ShowParentLink()%>'
I would have expected the viewstate to also reflect the fact that you have removed some of the links (assuming that they were removed before viewstate was saved).
Maybe thats the question you need to ask 'why do the removed links still appear in viewstate?'.
Another solution is to put the logic in the LINQ query, so that you end up with a boolean LINQ field like "ShowParentLink". Then you can just bind the Visible property of the HyperLink field to that value - no RowDataBound required.
protected void btnHazardRating_Click(object sender, EventArgs e)
{
gvPanelRole.RowDataBound += new GridViewRowEventHandler(gvPanelRole_RowDataBound);
gvPanelRole.DataSource = dtGo;
gvPanelRole.DataBind();
ModalPopup.Show();
}
void Process Rows()
{
... do something
... process complete
datagrid.DataBind();
}
A page cannot process postback events unless it is rebuilt exactly as it was before (the postback). If you re-hide your links during the page-init, then your click events and such should fire. Unfortunately, without seeing some sample code I can't get more specific.
Also the data RowDataBound does not fire because you are not data binding. You are rebuilding the page from the viewstate- "viewstate binding" for lack of a better word.