Problem with Datalist Edit Command

I am developing a small documents library app for my company using ASP.net 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)
        MyDataList.DataBind()
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.
TipstarAsked:
Who is Participating?
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.
0
sarabandeCommented:
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?

Sara
0
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
        FindData()
        listDocs.EditItemIndex = e.Item.ItemIndex
        listDocs.DataBind()

' ...

        listDocs.EditItemIndex = e.Item.ItemIndex
        listDocs.DataBind()
        FindData()


' 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
        listDocs.DataBind()

' 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?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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.
0

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
sarabandeCommented:
i found a vb sample code where they set the EditItemIndex after GetData and before DataBind.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.edititemindex%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3

Sara
0
TipstarAuthor Commented:
I found a work around but the original issue was not addressed.
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.