How can I refresh my FormView after an edit? - asp.net

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.

Related

GridView contents don’t update when underlying data changes

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.

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.

Page Post back initializes the dropdownlist

I face a weird problem. I have a simple aspx page with a dropdownlist. The dropdown gets filled through a function which is called from Page_Load() event. The dropdown item selection triggers event OnSelectedIndexChanged. Now the event triggers rightly
but what happens that upon post back the dropdownlist gets initialized, that is, it shows empty. Never faced this type of issue before so i wonder what's happening wrong.
The piece of code follow:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
PopulateCompanyList(GetCompanies(serverUNCPath));
return;
}
Is ViewState disabled on your dropdownlist, or perhaps the whole application?
The very first ideas:
Check EnableViewState property of
your dropdown and all the parent
controls up to the root (should not be false)
You perform rebinding on postbacks without storing
SelectedValue property
If one of your parent controls is
custom or dynamic it may incorrectly
persist ViewState info (including children).

Asp.net GridView - how to access datasource before data is bound?

I'm trying to access datasource before I bind it to grid. How do I do that? My guess I should look in one of the events like Grid.DataBinding
Yes, the GridView.DataBinding event will be raised prior to the control being bound. You can access the DataSource control in that event and make modifications if you so desire (for example, modifying parameters).
protected void Grid_DataBinding(object sender, EventArgs e)
{
mySqlDataSource.SelectCommand = "Select * from Stars";
}
One way would be to not define the datasource on the grid itself. In the page_load, create and populate your datasource and then dynamically bind it to your datagrid. That way you would be able to access it.
Hope it helps!

Which User Control event comes between data binding (other controls) and Render?

I'm currently building a user control that displays a message when a Repeater is empty.
The idea is simple, provide the user control with the ID of the Repeater. When the user control is rendered look up the Repeater and check Items.Count. If it's zero then display the message.
I would like to add one more feature though. I want to be able to hide the Repeater if there are no Items.
Obviously I can't do this in protected override void Render(HtmlTextWriter writer) because the Repeater has (possibly) already been rendered. I also can't do this when the the Repeater ID is assigned to the user control, as the databinding hasn't happened yet.
So my question is.. what event can I override in my user control where databinding has occurred, but rendering has not.
Consider the Page's PreRender event. That way binding has happened but nothing has yet been rendered although they are just about to.

Resources