I too am not very familiar with the DataList control. I had a similar sort of problem when I was using a DataGrid control. When I selected a specific row of the grid (which was a summary view of up to 100 data records) I wanted to display detail information for that record and hide the grid. When the user was done viewing/editing the detail data I had to redraw the grid with the updated data. But when I did that the focus was always back on the first row of the grid.
To solve my problem I added a template column to my grid (as the first column) and put a 'literal' control in it as my "anchor". Then I programatically set the value of each instance of the anchor after having loaded the grid. When the user selects a row to view detail information for I save the 'key' to the row in a ViewState variable and when I come back I retrieve it using some client-side java code.
The (sorry it's
VB and no pun intended) basic code is as follows:
Code:
Private Sub FillGrid(ByVal currentSqlCommand As SqlClient.SqlCommand)
Dim dr As SqlClient.SqlDataReader
Dim item As System.Object
sqlDBCn.Open()
dr = currentSqlCommand.ExecuteReader()
If dr.HasRows Then
dgrdItems.DataSource = dr
dgrdItems.DataBind()
End If
dgrdItems.SelectedIndex = -1
dr.Close()
sqlCnDB.Close()
...
' Set up the Anchor control with the row location so we are ready to
' return from Details view to whatever row was selected in Summary view
For Each item In dgrdItems.Items
item.FindControl("ctlAnchor").Text = "<a name=""" & item.ItemIndex.ToString & """>"
Next
End Sub
Private Sub dgrdItems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgrdItems.SelectedIndexChanged
With sender.Items(dgrdItems.SelectedIndex)
...
' Save the row location we selected so we can return to it
ViewState("GridBookmark") = .ItemIndex - 3)
End With
End Sub
Private Sub cmdReturnToSummaryView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReturnToSummaryView.Click
...
' Redraw the grid with the changes and any others that other users may
' have made while we were working on this record.
FillGrid(sqlCmdPrev)
SetGridBookmark()
End Sub
Private Sub SetGridBookmark()
Dim jScript As New System.Text.StringBuilder
jScript.Append("<script language=""JavaScript"">")
jScript.Append("location.href=""#")
jScript.Append(ViewState("GridBookmark").ToString)
jScript.Append(""";")
jScript.Append("</script>")
Me.RegisterClientScriptBlock("Bookmark", jScript.ToString())
End Sub