Gridview row information disappearing after selectedIndexChanged - asp.net

I have a master/details setup with a GridView and DetailsView both in UpdatePanels. When the DetailsView is edited and updated, I want these changes to be reflected in the GridView, but without rebinding that data (which could change the selectedItem's sort order among other problems it causes) On DetailsView ItemUpdated I have the following:
' Update Gridview '
ProductsGridView.Rows(selectedIndex).Cells(1).Text = e.NewValues("ProductName")
ProductsGridView.Rows(selectedIndex).Cells(2).Text = e.NewValues("Category")
This works fine when updating, but when a new item is selected in the Gridview, the updated text disappears. Why is this and how can I keep that info? When it is rebound it is fine if it changes position, put until it gets re-bound I would like that data to persist. Thanks.

Answer: It was because the cell's text was not being stored in the viewstate. I added Literal controls to hold the cell's text and updated the literal's text accordingly instead of using the cell's text, like so:
' Update Gridview '
CType(ProductsGridView.Rows(selectedIndex).FindControl("thisLit"), Literal).Text = e.NewValues("SomeValue")
CType(ProductsGridView.Rows(selectedIndex).FindControl("someOtherLit"), Literal).Text = e.NewValues("OtherValue"))
The information is now kept in viewstate and works perfectly =)

Related

Update values in gridview row after data has changed, without invoking a resort via DataBind()

A page has a gridview bound to a sqldatasource. I don't allow edits in the gridview. Most of it's columns are template fields with label controls. a formview control on the same page shows the data for the currently selected gridview row. Updates are made to the formview control. I want the gridview to reflect the updated data after it has been saved. A simple gv.DataBind() works but the data update can be involved in the gridview's sort, so if I call gv.DataBind() the updated row in gv may end up on some distant page of the grid. I would prefer that the gridview simply update any changed data in the already selected row.
What I've done so far is to have the sub that update the formview also change the text values of the labels on the selected gv row. That seems to work but does not persist. I see the changes in the gv cells, but as soon as I select another row in the gridview, the modified cells in the previously edited row go blank, for some reason.
What's a good way to do this?
Get the current page of the gridview (.PageIndex), do a DataBind(), then (re)set the page.

Dynamically generated content in a PlaceHolder inside of a FormView?

Reaching out for some help here, as this has me stumped. Long story short, I've got a dynamic Table that is built from Functions, displayed in a Placeholder inside a FormView.
I have a 'save' button inside the EditItemTemplate of the FormView, CommandName="Save" - I have a Select Case (using VB here) and (almost) everything is working as expected.
However, when I pull my PlaceHolder in Code Behind, I'm showing no controls in the PlaceHolder.
Here's a brief rundown:
FormView ItemTemplate has a View_PlaceHolder that shows data from dynamically generated table correctly.
FormView EditItemTemplate has an Edit_PlaceHolder that loads the same Data from dynamically generated table into TextBox (works fine, same data is shown as expected)
When I click Save, my current code is:
Dim Edit_PlaceHolder as PlaceHolder = FormView1.FindControl("Edit_FV_PlaceHolder")
Dim EditTable as Table = Edit_PlaceHolder.FindControl("Edit_Plan_Holder")
Edit_PlaceHolder is not nothing, but has 0 controls in Controls.Count
Oh, one more bit of information - my Table is built and added to the PlaceHolder in the FormView.DataBound event.
What am I doing wrong?
You must repopulate and add the dynamically added table on PostBacks too, not just on the initial load. Check out the answer to this question for more detail.
Ok, so here's what was happening:
1) As NoAlias stated, need to keep the ID's. I used Page_Load to set a ViewState variable if I was in 'view' or 'edit' mode.
2) I used a separate BuildViewTable() method and BuildEditTable() method, and inside those called my separate class and constructed the table.
3) It's all working now :) Thanks, NoAlias!

How can I pevent Gridview.DataBind when a commandField is pressed

I have a gridview included "Edit CommandFields Update and Cancel" at the last column of my gridview. Also, I have a search button above my Gridview and everything works properly together. When I am searching for a particular Column the "sqlDataSource" of my Gridview is changing respectively on the query and displays the results.
However, the Edit/Update/Cancel command field buttons refresh the whole Gridview and I am loosing the selected editable row derived from the search results.
How can I prevent grdiview Databing when I click the Edit Command Field ?
Any advice would be appreciated,
You are probably missing the famous is Page.IsPostback check.In vb.net you will have to do some thing like this
If Not Page.IsPostBack
->your binding code goes here
End if
C#
if (!Page.IsPostBack)
{
->your binding code goes here
}
This way your results are not lost when the page reloads.

Update DropDownList control after inserting an item with a detailsview

I have an update panel that includes a dropdownlist control and a detailsview control. The dropdownlist is populated by a sqldatasource control which grabs the data from a table called Places (just a list of venues). The detailsview is insert mode only and inserts places into the database table that populates the dropdownlist. I am trying to allow someone to insert a new place inside the update panel and have the dropdownlist refreshed at the same time so the user will see all places in the dropdownlist, including the one they just added with the details view. Right now, the detailsview is adding the place to the database properly but not updating the dropdownlist. If I refresh the page after adding a new place, the droplist updates with the new place. Any suggestions?
Disclaimer: This answer was posted before any code was posted in the question.
This also assumes both controls are inside the update panel.
In the event handler for Updating or Inserting for your DetailsView control, you need to add the following code:
MyDropDownList.DataBind()
And, worst case scenario, you can do this if the update panel is causing you problems. It's not the greatest performance-wise though.
Response.Redirect(Request.RawURL)

On postback lose value from dynamically created textbox gridview

I dynamically create gridview, and in this grid I have template field also
field.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
field.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName)
grdEmpty.Columns.Add(bfield)
but when enter some value in text box in this template field i lose value on postback. And also on postback I lose all template field and i must re-create this grid.
My goal is: I have button and i want to add new row in this grid, but i want to have old value also.
I struggle with this all day, and any help is welcome.
Tnx,
Just recreate the grid during each postback. If you have viewstate enabled in the page and the gridview then the property will be restored.
See: here for more explanation

Resources