I'm using Infragistics 2008 Volume 3 UltraWebGrid in an ASP.NET application targeting the 3.0 framework. In my Page_Load I set UltraWebGrid1.DataSource to a dataset returned by another class. The grid is populated all well and good. I have edits/adds/deletes enabled on the grid. I noticed that when I attempt to obtain the dataset bound to the grid's DataSource in a PostBack method call, it is set to Nothing. How do I get the changes that may have been performed by the user to the grid? Do I nee to catch the client side events and make the changes myself on a copy of the Dataset stored in a session variable? I'm fairly new to the ASP.NET side of things. On the WinForms side I'd just grab the dataset bound to the Datasource and call it's .HasChanges method to see if the user did anything; Likewise, I could call the DataTables .GetChanges method to get just what has been changed.
If I am missing something totally obvious, please forgive my ignorace, otherwise and assistance would be appreciated.
Hello,
Can you provide a simplified project with source code, demonstrating clearly what your problems are ?
You said:
>I noticed that when I attempt to obtain the dataset bound to the grid's DataSource in a PostBack method call, it is set to Nothing.
During a PostBack, the DataSet is lost if you don't recreate the DataSet or at least store the DataSet in Session or in Cache.
I have a Webform with the following in the Page_Load:
Try
If Me.IsPostBack = True Then Return Dim Connections As New ConnectionConfiguration UltraWebGrid1.DataSource = Connections.SearchResults() UltraWebGrid1.DataBind() Catch ex As System.Exception Throw ex End Try
I also have a the following:
Protected Sub WebImageButton1_Click(ByVal sender As Object, ByVal e As Infragistics.WebUI.WebDataInput.ButtonEventArgs) Handles WebImageButton1.Click Try Dim Connections As New ConnectionConfiguration Connections.Save(UltraWebGrid1.DataSource) Catch ex As Exception Throw ex End TryEnd Sub
In the event above, UltraWebGrid1.Datasource is ALWAYS nothing, even though it was bound in the Page_Load and the grid visually displays data. I have ViewState turned on for both the page, button and the grid. Am I missing something?
This is a common issue with ASP.NET data binding. Each time you hit the page, it's as if it's a new request, with the exception of any details that were passed back and forth in the page.
In your Page_Load event handler, you're only setting the grid's data source when the page is not posting back. Thus, whenever you're processing your button's Click event, you've not set your grid's data source at all.
My suggestion: Handle the grid's InitializeDataSource event, and set the grid's DataSource there (in addition to populating the data source itself). This is called on every postback. It gets called early in the page's lifecycle on every postback, which means that it won't "clobber" any changes the user has made, and thus can ignore the "IsPostBack" check.
Imports Infragistics.WebUI.UltraWebGrid...Protected Sub UltraWebGrid1_InitializeDataSource(ByVal sender as Object, ByVal e As UltraGridEventArgs) Handles UltraWebGrid1.InitializeDataSource Try Dim Connections as New ConnectionConfiguration UltraWebGrid1.DataSource = Connections.SearchResults() Catch ex As System.Exception Throw ex End TryEnd Sub
If you wanted to avoid reloading your connections on every postback, you may want to store the results of your Connections.SearchResults() method somewhere such as session state, and re-set the grid's DataSource property to what's stored in the session state whenever you have it. If you take this approach, I suggets you make the "gridDataSource" a private field of your page. (Note that I haven't tested the below code, so I may not have this entirely correct.)
Imports Infragistics.WebUI.UltraWebGrid... ' Replace "ResultType" below with the actual data type you get from your SearchResults() method. Private gridDataSource as ResultType...Protected Sub UltraWebGrid1_InitializeDataSource(ByVal sender as Object, ByVal e As UltraGridEventArgs) Handles UltraWebGrid1.InitializeDataSource ' Replace "ResultType" below with the actual data type you get from your SearchResults() method. gridDataSource = TryCast(Session("Results"), ResultType)
If gridDataSource is Nothing Then ' Couldn't find the search results in Session, so re-load them Try Dim Connections as New ConnectionConfiguration gridDataSource = Connections.SearchResults() Catch ex As System.Exception Throw ex End Try End If UltraWebGrid1.DataSource = gridDataSourceEnd Sub...Protected Sub Page_Unload(ByVal sender as Object, ByVal e as EventArgs) Handles Page.Unload ' Store the grid's DataSource in session state Session("Results") = gridDataSourceEnd Sub
That will assure that my grid's datasource is always populated with an object, but how do I go about getting any user changes after the page has been rendered back into that object? Am I correct in assuming that because of the page lifecycle, after the page has been rendered the object bound to the datasource of the grid is gone?
How do I go about getting the changed data out of the grid and back into an object so I can save the changes to the database? Do I have to spin through the grid rows and pick out all of the changes and manually rebuild my object, or is there some easier way?
Art Gola said:Am I correct in assuming that because of the page lifecycle, after the page has been rendered the object bound to the datasource of the grid is gone?
Art Gola said:How do I go about getting the changed data out of the grid and back into an object so I can save the changes to the database? Do I have to spin through the grid rows and pick out all of the changes and manually rebuild my object, or is there some easier way?