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
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 have a repeater control that pulls back some data from a database and creates a radiobuttonlist for values related to each row. I don't know how many rows will be coming back, so I don't know how many radiobuttonlists there will be. This repeater is inside of a form, and once I click "Submit" on the form I need to get the values from the radiobuttons generated by the repeater.
Any thoughts on how to do this?
There are a few options.
One is to use a nested Repeater to display the RadioButtons.
The other is to create the controls during the original repeaters. Repeater.ItemDataBound event.
In either case, you need to be very aware of the Page Lifecycle.
You'll want to avoid databinding in the Page_Load event, unless the databinding is being done with an if(!Page.IsPostback) block. If you fail to check for postback, you'll run into issues where the Page_Load calls a DataBind() call that then wipes the values of the repeater before any event handlers can use it.
Alternatively, you can do your binding in Page_Init, which occurs BEFORE the viewstate can be applied, so you won't lose your values.
You can also use USer Controls for the RadioButtons. There's a previous post that covers how to use nexsted user controls within a Repeater here.
In an ASP.NET webform, I have 3 TextBoxes, each linked to a different SQL Database Table. Now I want to store data into table when Textbox Change event occurs. I can do successfully with make Update Query in TextBoxChanged event. Now What I want is when this update operation going on I dont want it to make interrupt my page and other operations I want to perform. I use an Asynchronous Postback from the TextBoxChanged event.
The problem that I am having with this is:
When I change one TextBox and click on another TextBox, and hit "backspace" inside that textbox, the page reacts as if it was a browser back button-click, and redirect to other page.
I hope you can understand the question I want to perform TextBoxChanged and handle the asynchronous postback without interrupting other operations.
You will have to use jQuery for the capturing of the textbox changing. However, you cannot connect directly to onchange as the changed value is not stored until after the event. You could probably tie to the blur event if you only want to capture when the user changes and leaves the textbox, though. Otherwise, you will have to use a timeout to continuously check if the value has changed ( How to detect a textbox's content has changed )
Within that event, you should use AJAX to make the save calls for persisting the value.
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.
In a Sharepoint web part, I have a DataGrid with paging that I load with all of the data (not using custom paging - custom paging would require a significant overhaul in the current process and is probably one of the last options I can try). I was wondering if it was possible to have it page through the data without re-binding the data source to the grid in the page index changed event? If I remove my current calls to re-bind the data, it remains on the first page no matter what.
With a datagrid, I think you need to re-bind the grid whenever you want to go to a new page.
"A typical handler for the PageIndexChanged event sets the CurrentPageIndex property to the index of the page you want to display and then uses the DataBind method to bind the data to the DataGrid control."
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.onpageindexchanged(VS.71).aspx
If you want to avoid querying/fetching the data over again from the source, then you'll need to "cache" the data between postbacks. There are various options here, each with it's own advantages and drawbacks.
If the size of the data is not too large and is not sensitive, you can simply put the data in viewstate on the first page load, and read it out again when the page index is changed. Another option might include using Session to "cache" the data, although this can get tricky if not done right, and of course there will be more load on the server side with this method (with varying amounts, depending on if the Session is In-Proc, State Server, or Database). There may be other methods to "cache" the data, but that is what you'd need to do in this case.