I have a usercontrol that has custom unbound data columns in a gridView. I want to retain view state when switching between screens in my app in terms of selected tabs and focused rows etc.
When I do this switch and restore that view state, my custom data is gone and the CustomUnboundColumnData event handler does not get used at all.
Is there a way to raise the CustomUnboundColumnData event for this gridView after I restore my view state?
Call the RefreshRow or RefreshRowCell methods, passing a row handle and optionally a column.
If you are using the ASPxGridView, I would suggest that you turn off the EnableRowsCache property. One more solution is to call the ASPxGridView's DataBind method explicitly within the Page_Init method.
Found a work around. I was refreshing my data in this grid view anyway. However, the few columns that had unbound data needed to be re-added to the grid before refreshing this data.
Related
I am creating a set of tables, dropdowns, buttons, labels dynamically on OnInit() event on the basis of list of objects( fetched from database). I have bound events with these dynamic controls as well.
On dropdowns_SelectedIndexChanged event I have to populate my list of objects again from database on the basis of selected value and recreate these tables, labels again.
First time dropdown selection is working fine for me, but as soon as I am recreating control here on dropdowns_SelectedIndexChanged, the event of dropdowns are not bound (as it is compulsory to add dynamic controls on OnInit() to bound events with them) .
Now for an alternative I am trying to get dropdown's selected value on any Page event near by pre_render() so that I could recreate my controls there with updated objects List without any dropdowns_SelectedIndexChanged event.
Any help will be appreciated. Thanks in advance.
Yes, here is one nasty way!
You can index the Request.Form["__EVENTTARGET"] that should get you what your after.
BUT I wouldn't necessarily/always recommend it. Even when adding controls dynamically you should be able to get the selected value, in the correct event handler. It's probably owing to where you are adding the control in the page lifecycle.
I've got a Repeater control, bound to a PagedDataSource, which datasource is a list of custom controls I've made. These custom controls contains a couple of text boxes.
I have a save button, and when it is clicked I want to save the data in all the custom controls to a database, no matter which page they are on - but currently I only got access to the custom controls displayed on the current page.
What I've tried to do is to, in the btnSave_Click event, create a new temporary datasource equal to the current one, except its not a PagedDataSource. That way my repeater contains all custom controls - BUT - the changes made in the textbox fields are no longer available. I then tried to add JavaScript onchange events on the textboxes in the custom control, so that a postback would be fired whenever text was changed, and the property in the user control codebehind would be updated. This didnt work either.
Any ideas?
save the changed values on each page index changing event (or prev /next buttons) into your persistance object (List)
http://www.dotnetfunda.com/articles/show/1611/how-to-select-multiple-records-from-multiple-pages-of-the-gridview-and
The reason your non-PagedDataSource is empty is because the changes in your text box exist in the client and not on the server - you'll need to synchronise the values from your controls with the empty slots in your repeater.
The Repeater does not have built-in Pagination (like the GridView or other complex controls) so it does not offer events such as the PageIndexChanging event. I assume therefore, that you have your own Page navigation implementation. You should therefore call the function you have presented within that implemented function.
Try Using a generic List and Skip and Take methods of that
I have a data access layer, a business logic layer and a presentation layer (ie. the pages themselves).
I handle the OnPreInit event and populate collections required for the page. All the data comes from an SQL server database and I do not use caching.
I handle a button click event to grab values from a form and insert a new object into the database. The problem is that by the time I handle the click event, the collections have already been populated, so the new item which has been inserted into the database has not been retrieved.
What is the accepted solution to this?
I could insert the new object directly into the collection and re-bind the GridView, but the SQL query selects only a set of objects and the new object could fall outside of this set.
Thanks!
I generally do databinding in two places in my pages:
Inside Page_Load, if !IsPostBack to load the initial state.
As one of the last lines in event handlers to show the result of adding/editing/deleting a record.
The issue you're running into is just the result of using OnPreInit to do data binding, so you're probably going to have to stop doing that, or try one of the sub-optimal approaches that others have suggested (redirecting back to the page after a postback).
Perform your data binding operations later in the page lifecycle, e.g. in the page PreRender event which fires after any control events have been handled.
You should just handle the click event. Then after your database update/insert, you must rebind your data. Depending upon your implementation, you might need to re-post the page. If you were to use normal data binding you could just call the bind method again.
Retrieve data on the Page.LoadComplete event and do a data bind.
If for some reason, should retrieve them only on PreInit, then retrieve them on for !IsPostBack condition and bind them and for PostBack conditions, handle them on LoadComplete
You could use this method to determine which control issued the postback in the PreInit: http://blogs.microsoft.co.il/blogs/gilf/archive/2010/03/01/discover-which-control-raised-a-postback.aspx
No database involved here, I am creating my own datatable in code, and binding it to a textbox and a datagridview.
When I change current record in the grid the textbox updates to the current value.
But what's the best way to synchronise changes between the values in the textbox and the datagrid. If I change one then it doesn't change the other unless I go to another record and back.
I can do this by adding a datagrid.Refresh() to the textbox Validated event, and presumably something to the CellValidated event on the datagrid, but it seems like I might be going about this the wrong way.
Edit:
Based on the answer below, my question should be: is there a way to notify bound controls of changes in a DataTable they are bound to, or must the code use a BindingList or do it manually instead.
I suggest you use a BindingList<T> rather than a DataTable, where T is your "business object" that represents each record displayed in the grid. Then your business object should implement INotifyPropertyChanged and fire NotifyPropertyChanged whenever the value in the text box changes, either by binding the desired property to TextBox.Text or updating the appropriate property of the selected business object whenever TextBox.TextChanged fires.
Baby steps rolling... I have a form with a data grid bound to a table. I have some textboxes on the form bound to the table[columns], so as I scroll the grid, the textboxes show corresponding data.
I go to an "Edit Mode" of the textboxes and change the content and hit a save button. The grid doesn't refresh the changed context until I physically click in the cell which forces a call to the tables OnChanging and OnChanged events...
How can I FORCE whatever event to "flush" the table at the end of my edit and have it refreshed in the datagridview.
Thanks
Have you tried calling the dataGridView's Invalidate method?
this.dataGridView1.Invalidate();
Are you using a BindingSource? If so, call its EndEdit method on the TextBox's Leave event.