GridView contents don’t update when underlying data changes - asp.net

So I have an ASP.NET page with two controls:
a GridView which displays rows from a SqlDataSource, and in which the user can select a row;
a DetailsView in which the user can see and edit the values of the selected row.
The DetailsView can update the underlying data, but even though the page reloads, the GridView still displays the old data until I manually reload the page in the browser.
How do I ensure that the GridView displays the correct data after the update in the DetailsView?
P.S. It may be important to note that due to reasons outlined in this other question, I had to use a custom OnItemUpdating event with the DetailsView. This event performs the SQL update command (successfully), sets the DetailsView back to ReadOnly mode (out of Edit mode) and then cancels the event (e.Cancel = true). If this event canceling also somehow cancels the GridView updating, how do I manually tell it to update itself?
P.P.S.: I discovered this similar question, but the answer doesn’t work: it resets the entire page back to pristine state, which means the GridView loses its selected row and the DetailsView disappears. I don’t want that.

on page load:
YourGridViewID.DataBind()

If your OnItemUpdating event generates postback
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
GridView.DataBind();//rebinding it with considering changes DetailView
}
}
If it doesn't work let me know.

Related

ASP.Net LifeCycle and Database Update order

I have been reading about the Page LifeCycle. I understand the LifeCycle, however, it's not clear on what to do, and when to do it. The problem is I use Page_Load to get database values, and set form fields. I use a button's onClick method to update the database. However, the form fields text properties were set during Page_Load, so it's really only updating the database with the OLD values.
Page_Load: I gather data, and set control text properties to reflect data.
Button_onClick: I update the database from the form
Problem: It's updating values gathered from Page_Load and not the actual form.
Certainly, I am not supposed to perform everything in the Page_Load. So where am I going wrong during this process?
Page_Load
If you are loading your database data in the Page_Load event, the very first thing to do is to wrap it within a if (!IsPostBack) statement.
IsPostBack
Gets a value that indicates whether the page is being rendered for the
first time or is being loaded in response to a postback.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
So IsPostBack = true when the page cycle is the result of postback.
In your Page_Load, you should only gather your data when IsPostBack = false, not on every page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// gather your data here
}
}
Setting fields
I personnaly prefer to set the fields content on the PreRender event handler (but honnestly i don't know it should/must be done there, it just seems more logic to me).
PreRender is executed after your postback events (click on a button, drop-down selection change...) so it ensures that your updates and more generally data modifications are done before rendering the page.

How can I refresh my FormView after an edit?

I am using a FormView to select companies from a database. This FormView is bound to an ObjectDataSource, which is bound to a DropDownList. When a user selects a company from the DropDownList, the FormView is populated with information pertaining to the company (AutoPostBack is enabled on the DropDownList to provide dynamic record changing). My FormView supports insert, update, and delete functions, of which all three work.
My problem is as follows: After I edit a record in the FormView, the record successfully changes, however, it is completely removed from the screen. Only after I select a new company from my DropDownList will a record re-display in the FormView.
Does anyone have an idea as to what may be the issue here?
You need to databind again.
void FormView_ItemInserted(Object sender, FormViewInsertedEventArgs e)
{
Gridview.Databind();
}
You need to trigger the child control to be databound when the parent control has been modified.
I've found that the most reliable palce to do this is in the parent control's DataBound event. After a successful edit, this event is called, because the control needs to re-bind to the edited data.
Here's a snippet from some actual code of mine where I do something similar. In it, I have a control called QuestionGroupResutls that has child records in a control called QuestionResults.
void QuestionGroupResults_DataBound(object sender, EventArgs e)
{
QuestionGroupsEditingDetailsView1.DataBind();
QuestionResults.DataBind();
}
I have this in Page_Load, but you can specify this declaratively in the asmx (I just don't have a code sample).
QuestionGroupResults.DataBound += new EventHandler(QuestionGroupResults_DataBound);
I don't know the name of your controls, but you should be able to do the same thing with yours.

when gridView update is fired?

I asked a question Gridview is not displayed when using stored procedure in SQLDatasource which I want to resolve . Based on my old question, I am not sure when exactly is gridView update fired. I am under the impression
Gridview Update is fired automatically when the page is loaded if you use SQL Statement in SQLDatasource
GridView needs to be manually bound using sqldatasource.databind() function to update data if you use stored procedure.
But even then, the second case is not working for me. I know this implementation is somewhat buggy.Can anyone guide me throw this, when exactly Gridview is updated and possibly throw some light on my original question also?
Thanks!
Update: After further research, I found that databind() property is not needed to update the gridView. What is needed is a post-back. So if you add a button with no code inside it, it will update the gridView simply because the page has been reposted and (maybe)the textfield values have changed too.
I still dont get any result though. What I have done is entered "not data found" in empty template for the gridview. So now I get "Data not found" instead of nothing. Which does means that SP is run but returns 0 result.
Another thing that I found is, if you bound a text field to the SQLDataSource, by default, it changes the empty textbox value to NULL. This is kind of stupid, why will you need the NULL values for? This will result in 0 records since obviosly we dont store null int the database. (Null means nothing but Microsoft though the other way). So to fix that, go to the parameters list in SQL Datasource and select the parameter. Click on the link "Show advance properties" There change the property "ConvertEmptyStringToNull" from true to false. Although it did not fixed my problem yet (but fixed it somewhere else).
I am thinking I am using 4 parameters in sqlprocedure and one of them is causing problem? But the query does not ok when testing in SQLDatasource.
Is there a reason why you must use the GridView update event? Why not just add the code to bind the GridView during the Page_Load event or something? Have you at least tried to narrow down the causes for the erroneous behavior?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataSource1.DataBind();
}
}
After my research, I found GridView is automatically updated when the page loads. It is the same weather an SQL Statement or a Stored Procedure is used in SQLDataSource. Also SDQDatasource.Databind() is not needed, but may be needed in special scenarios.
What triggers GridView update is page load. If you create a button and add a click event, just clicking that button will automatically cause gridview to update but clickevent cause page reload.
Hope it helps

Asp Update Panel - Details View Updates Underlying GridView doesn't

I have an Asp.net gridview on a page, when a buttonfield is clicked (instead of select) it opens a popup form (my popup not asp's) which contains the detail of the gridview row displayed in an editable DetailsView. (This approach has been used because the grid contains 20 wideish columns and it is easier to edit / update in the detailsView format) The DetailsView takes any amendmends and writes them back to the table, fine, but the underlying Gridview is never visually updated unless of course the page is completely reloaded (I tried to use the Windows.reload function but I get the annoying winddows is trying to reopen etc, error, so that is useless). I am trying to discover the best way to get the gridview to refresh it's data. I have placed via a Me.ClientScript.RegisterStartupScript(Me.GetType() eyc, an alert box to communicate when the 'update steps' of the detailsview fires so I could interject a gridview.ReBind() but because the detailsview is held within the update panel the, for example, Protected Sub DetailsView2_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView2.ItemUpdated functions, and the roqwupdatind and so one, do not appear to fire. (they must do of course else the table would not be updating. MY problem is how do I 'communicate with update events within the panel contained detailsviews so that I can cause a reBind to occur on the main gridview. Any Ideas would be appreciated. Thank you
Detail view have an event called
protected void dvEbook_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
Response.Redirect(Request.RawUrl,false);
}
where dvEbook is my detail view.
It will call a same page.
I hope it will work for you.
Thanks

ASP.net-Listbox not retaining selected values between postbacks

I have a Listbox getting populated when the page loads itself and also in postbacks. Some items are already selected in that listbox even when the page loads or postbacks. I have to select one or more items in that textbox in addition to the selected one.
Till this point, it works perfectly, but the problem is that when i click an "update" button to save the newly added items, then also the page postbacks and so the list box get populated again. This will loose the newly selected items and none of the newly selected items will be saved.
So what should i do in order not to loose the newly selected items when i click the "update" button.
Note:- I need to populate the listbox on postbacks also. So the population of that listbox on postbacks cannot be discarded.
Kindly help me. I am new to ASP.net.
Thanks in Advance
You shouldn't need to re-populate the list if you have your view-state enabled.
To populate you should be doing something similar to this:
if(!IsPostBack){
// populate your list from database or whatever
}
This will only populate the list if the page is not a post-back, therefore (if viewstate is enabled) your selections will remain after you click update.
If you want to disable the viewstate, it is important to populate the DropDownList during the "Init" event of the Page LifeCycle not the "Load" event:
protected void Page_Init( object sender, EventArgs e ) {
DropDownList1.DataSource = ...;
DropDownList1.DataBind();
}
More information can you find in this excellent article:
Truly understanding viewstate.
I hope this helps a bit.

Resources