Formview - How to navigation to a record based on query string and not lose paging

Last post 11-08-2009 2:20 AM by irokhes. 2 replies.

Sort Posts:

  • Formview - How to navigation to a record based on query string and not lose paging

    11-07-2009, 7:12 PM
    • Member
      90 point Member
    • magicjoef
    • Member since 02-26-2007, 5:32 PM
    • UK
    • Posts 178

    Hi All,

    I have a datalist with links that pass a querystring of UserId to a page that displays the appropriate data record in a form view.

    However, at the moment I am using the SQL datasource to return the required information, which means I lose out on the paging function I would like to have, as only one row of data is returned.

    Is it possible to navigate programatically to a record in a form view that matches a querystring value, rather than restrict the information I am getting from the database?

    I am using VB

    Thanks in advance for any help!

    Filed under: , ,
  • Re: Formview - How to navigation to a record based on query string and not lose paging

    11-07-2009, 8:59 PM
    Answer
    • All-Star
      25,628 point All-Star
    • PeteNet
    • Member since 01-21-2009, 6:15 PM
    • Posts 3,647

    magicjoef:
    Is it possible to navigate programatically to a record in a form view that matches a querystring value, rather than restrict the information I am getting from the database?

    you'll have to find the data row, in the source that the formview is bound to, that matches the querystring value and set the PageIndex of the FormView to that row.

    'take a page level variable for comparison in the DataBound event:
    Dim ID As String = String.Empty

    'set it up in the page load event to the querystring value coming in
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
          If Request.QueryString("ID") IsNot Nothing Then
               ID = Request.QueryString("ID").ToString()
          End If   
    Else
          ID = String.Empty
    End If

    End Sub


    'in the DataBound event handler wired up to the FormView:

    Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
      'this event will fire at all times (paging etc) so..
      If Not String.IsNullOrEmpty(ID) Then
            Dim fv As FormView = DirectCast(sender, FormView)
            If fv IsNot Nothing Then
                Dim myDataRowView As DataRowView = TryCast(fv.DataItem, DataRowView)

                For iRowIndex As Integer = 0 To myDataRowView.DataView.Count - 1
                    If myDataRowView.DataView(iRowIndex).Row(0).ToString() = ID Then
                        'found record
                        fv.PageIndex = iRowIndex
                        Exit For
                    End If
                Next
            End If
      End If
    End Sub


    * remove any QueryStringParameters you've currently set up on the FormViews' SqlDataSource

    you may like to refer this thread: http://forums.asp.net/t/1481943.aspx?PageIndex=2 (does the same with a DetailsView, but I've replaced code for a FormView for you)

    Regards,
    Peter
  • Re: Formview - How to navigation to a record based on query string and not lose paging

    11-08-2009, 2:20 AM
    • Contributor
      5,298 point Contributor
    • irokhes
    • Member since 10-29-2009, 5:46 AM
    • Posts 800
    Please mark this post as answered if it helped you!

    mY BlOg
Page 1 of 1 (3 items)