I have a gridview with checkbox in Itemtemplate. What i need to do is check the condition:
(checkbox.checked==true)
if its true take the rows to another gridview. i binded the data of the gridview in
if(!PostBack)condition.
Everything was working fine until my employer said he doesnt wants the gridview to be bound in the if(!postback) condition but if i take that condition out checkbox.checked value is false always!
From the details of your question, I'm not sure if your boss wants the grid unbound or doesn't want the page bloated with the grid's ViewState, but in my experience, it's usually the latter. However, if your boss simply doesn't want the grid bound in the "if(!Postback)" condition and doesn't mind if it's bound or uses ViewState, then your boss doesn't understand web development using C# and server-side controls.
But... to answer your question, the reason the checkboxes are all unchecked is because you're rebinding the grid AFTER the ViewState has been applied. ViewState contains all the data needed in a postback condition to set the state of the checkboxes during postbacks and applies that data prior to the OnLoad (or Page_Load) event. By rebinding the grid, you are essentially wiping out anything that was posted back in ViewState and applied. To overcome this, turn ViewState off for the grid and apply the state of the checkboxes yourself AFTER you rebind the grid using the FORM collection.
If you want to do away with ViewState, here are some tips in this article.
http://www.codeproject.com/KB/viewstate/DataGridViewState.aspx
Related
I have a repeater in my page with some pre-conditions such as a checkbox list and listboxes that dictate what the datasource for the repeater brings back. A button is clicked which will databind the repeater which works fine. However if the user decides they want to add something else in or remove something they can check/uncheck some fields and then hit the button again which will rebind the repeater and change what is displayed, however the viewstate of all the current controls in the repeater will be lost.
Binding the repeater in the initialization event would not be possible because the ViewState for the checkboxlist/listbox values are not available at that point in the page lifecycle, and those values are required in order to pass as parameters into my datasource for the repeater.
What are my options for maintaining the state of my repeater controls?
repeater control binding fully recreates all of children controls inside repeater templates. Because new DataBind result may (or may not) contains extremely different data inside repeater.
What kind of controls and their state you want to maintain inside repeater? Maybe using ordinal html controls and operating with theirs through Request.Form collection would be a better way?
If you want to save data from repeater before applying new databinding, best place for making it is a Page.PreRender event. In this event all Page controls already recreated and their viewstate already restored. So you may iterate by Repeater.Items collection and save data from repeater row by row. And after saving all the data you may rebind repeater controls according current filter values from page.
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.
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.
I have a Repeater nested inside of a GridView. On the RowDataBound event for the GridView, I set the DataSource (based on one of the row's columns) and then bind the Repeater. This works fine for the initial load, however I need to be able to add new items to the Repeater dynamically.
I append an item to the DataSource, save it to the ViewState, and where I would normally bind using a method call, I bind to the object saved to the ViewState instead. The DataSouce reflects the change, however the page does not.
What am I missing? I have the exact same setup on another page without the nesting and it works perfectly.
if (ViewState["RepeaterObj"]!=null)
{
rpt.DataSource=(IList<DataTransferObject>)ViewState["RepeaterObj"];
}
else
{
rpt.DataSource = controller.GetObj(param);
rpt.DataBind();
}
I ended up resolving the question by cutting out use of the ViewState entirely, though I thought my temporary DataSource would be lost across the postback it wasn't. I ended up going with a class-level variable which works perfectly. It seems I didn't properly understand what happens during a postback.
First of all, you shouldn't be storing a datasource in ViewState. That's pretty much the worst place you could put it.
As for your problem, I would suggest either rebinding the GridView when new items are added to the repeater, or find the repeater in the event that saves the new record and rebind it there.
I think the problem is you are not rebinding the the Repeater. You say you change how you bind to look at the ViewState object but are you actually triggering the Bind to occur? It sounds like you are not and the page is just reloading with the current data stored with the control's ViewState.
Make sure you are calling your Repeaters bind event explicitly to sure it is getting rebound.
EDIT: I suspect it might have something to do where you might need to rebind your GridView and not just the Repeater.
I'm trying to bind my GridView at runtime, but I'm also trying to avoid running all the binding events twice.
I have a GridView that gets populated from a function that returns a DataTable. I'm not using ViewState in the grid for a couple of reasons. I seem to have a Catch-22 situation here:
If I don't bind the grid by Page_Load at the latest, the RowCommand and other grid events won't fire.
If I DO bind the grid in Page_Load, but I'm on a PostBack from a pager link, sort link, or search button, those event handlers will change the data and need to rebind it, running all the binding code again.
The grid triggers DataBound, RowDataBound, and RowCreated events, which could be performing expensive operations. I really hate to call them all in Page_Load, and then wipe out the data and call them all again if the data changes. But I can't seem to avoid this double duty, because in Page_Load I don't know if it was a grid event that will change the data, or a grid event that doesn't.
Any ideas?
Try the command arguments. If a button in the gridview was clicked, that event will be fired and you can handle it appropriately. Your question is not clear enough i'm afraid. Could you be more specific?
Check if request is a postback. Bind the datatable to the grid like so:
If(!ispostback)...
That way you wont be binding the table to the grid on each request.