Problem with Datalist Edit Command

I am developing a small documents library app for my company using with a SQL Server Express (R2 2008) back end. On my form I have a data bound Data List that displays each record in the Master library . There is an Edit button on the Item template and Cancel and Save buttons on the edit item template. If the user clicks the edit button on 20 (of > 200) the EditCommand fires fine but the list returns to the first record and they have to scroll down to find it again. In the Edit Command event I set the DataList EditItemIndex property to the index of the DataListItem that was clicked, then rebind the data to the control, like this:

Protected Sub MyDataList_EditCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles MyDataList.EditCommand
        Dim queryString As String = "SELECT TOP 100 PERCENT (Fields) FROM (MyTable)"
        MyDataList.EditItemIndex = e.Item.ItemIndex
        MyDataList.DataSource = GetData(queryString)
End Sub

Open in new window

(GetData is what sets the data source of the Data list control and this works fine in all instances)

This happens with Cancel and Save also, it returns to the first record in the list. I want to stay on the current record and not have to scroll down to find it again.

What I am doing wrong? Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NerdsOfTechTechnology ScientistCommented:
Try SetFocus() on the selected list item row after the command fires off to set it back to the previous position.
you may try to set the EditItemIndex after GetData call.

why did you call GetData again if the data already were loaded in the grid?

TipstarAuthor Commented:
Sarabande: If remove the GetData call I get no data at all. I have another function called FindData which populates the DataList based on search criteria that is called when the user clicks the Find button. This function returns all records if no criteria are entered. If I click this the data is returned but the list still returns to the first record.
The placement of the call to either GetData or FindData makes no difference. I tested all possible combinations.
' All these return the same result
        listDocs.EditItemIndex = e.Item.ItemIndex

' ...

        listDocs.EditItemIndex = e.Item.ItemIndex

' without the GetData call no data is returned until the Find button is clicked but still goes to the first record. 
        listDocs.EditItemIndex = e.Item.ItemIndex

' placing GetData, either before or after does return data but makes no difference to the position 

Open in new window

I thought that setting the EditItemIndex property to the ItemIndex of the DataListItem was supposed this.
Nerds: SetFocus? Can you show me how please?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

TipstarAuthor Commented:
OK I found a workaround. I collect the PK of the record being edited and modified GetData to set the record source of the DataList to that one record. Still would like to know why the ItemIndex thing isn't working.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
i found a vb sample code where they set the EditItemIndex after GetData and before DataBind.

TipstarAuthor Commented:
I found a work around but the original issue was not addressed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.