How to position Formview to a specific record

Last post 05-19-2008 12:23 PM by dwfreeman. 6 replies.

Sort Posts:

  • How to position Formview to a specific record

    05-14-2008, 6:59 PM
    • Loading...
    • dwfreeman
    • Joined on 05-14-2008, 5:59 PM
    • Posts 256

    If the user has selected a record in a grid on page1 which sends him to page2 containing Formview1, how would you set Formview1 to the selected record without limiting Formview1 to only the one record?

     Would the code to do this be located in the Page.init? or where?

    Here is my attempt which isn't quite there yet.

     Protected Sub Page_init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim CurrentRecord As Int32 = Request.QueryString("user_id")
            Dim i As Int32
    
            Response.Write(CurrentRecord)               ' For testing
            Response.Write(VarType(CurrentRecord))      ' For testing
            Response.Write(FormView1.DataItemCount)     ' For testing
    
            For i = 0 To FormView1.DataItemCount - 1
                Response.Write(i)
                If FormView1.DataKey(i).value = CurrentRecord Then
                    FormView1.PageIndex = cstr(CurrentRecord)
                    Exit For
                End If
    
            Next
     End Sub

     Thanks for any help on this.

    -Newbie

  • Re: How to position Formview to a specific record

    05-14-2008, 7:35 PM
    • Loading...
    • aamador
    • Joined on 02-11-2008, 10:49 PM
    • Posts 1,105
    Try adding a databind() 
    Protected Sub Page_init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim CurrentRecord As Int32 = Request.QueryString("user_id")
            Dim i As Int32
    
            Response.Write(CurrentRecord)               ' For testing
            Response.Write(VarType(CurrentRecord))      ' For testing
            Response.Write(FormView1.DataItemCount)     ' For testing
    
            For i = 0 To FormView1.DataItemCount - 1
                Response.Write(i)
                If FormView1.DataKey(i).value = CurrentRecord Then
                    FormView1.PageIndex = cstr(CurrentRecord)
                    FormView1.DataBind()
                    Exit For
                End If
    
            Next
     End Sub
    
     
    I am not anti social, am just not user friendly
  • Re: How to position Formview to a specific record

    05-16-2008, 5:07 PM
    • Loading...
    • dwfreeman
    • Joined on 05-14-2008, 5:59 PM
    • Posts 256

    My code fails on this line

     If FormView1.DataKey(i).value = CurrentRecord Then

    It seems the value of i only works if it is = to 0. Any higher number produces an out of range error. I have tried a couple dozen variations on the syntax. Can you clarify for me how it should read? I can set the value OK once I figure out how to accomplish the loop.

     

    Thanks


     

  • Re: How to position Formview to a specific record

    05-16-2008, 6:28 PM
    • Loading...
    • aamador
    • Joined on 02-11-2008, 10:49 PM
    • Posts 1,105

    I prototyped what you have and this is what I found

    1 - DataItemCount is always zero ??!

    2- i set to count will cause an index out of range

     

     Protected Sub Page_init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim CurrentRecord As Int32 = Request.QueryString("user_id")
            Dim i As Int32
    
            For i = 0 To FormView1.DataItemCount - 1
                Response.Write(i)
                If FormView1.DataKey(i).value = CurrentRecord Then
                    FormView1.PageIndex = cstr(CurrentRecord)
                    Exit For
                End If
    
            Next
     End Sub
     
    So I tried this instead and it worked for me.
     
     
    Partial Class NdxByQryStr
        Inherits System.Web.UI.Page
    
        Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            Dim cr As Integer = Request("id")
            Dim i As Integer = 0
            Do While DataBinder.Eval(FormView1.DataItem, "CategoryId") <> cr
                FormView1.PageIndex = i
                FormView1.DataBind()
    
                If DataBinder.Eval(FormView1.DataItem, "CategoryId") = cr Then
    
                    Exit Do
                End If
                i += 1
            Loop
    
        End Sub
    
      
    End Class

     

     

     
    I am not anti social, am just not user friendly
  • Re: How to position Formview to a specific record

    05-16-2008, 7:30 PM
    • Loading...
    • cashmore
    • Joined on 11-25-2005, 11:58 PM
    • Shropshire UK
    • Posts 537

    FormView1.DataKey refers to the DataKey of the currently displayed record. A loop with FormView1.Databind is rather inefficient especially if there are a large number of records.

    Ideally you want to loop thru the datasource of the FormView to search for the user_id e.g.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Assuming you are using a sqldatasource for your FormView and field you are searchiing is user_id

    Dim CurrentRecord As Int32 = Request.QueryString("user_id")
    Dim DV As DataView = Me.SqlDataSource1.Select(DataSourceSelectArguments.Empty)

    Dim dt As DataTable = DV.ToTable

    For j As Integer = 0 To dt.Rows.Count - 1

    If dt.Rows(j)("user_id") = CurrentRecord  Then

    Me.FormView1.PageIndex = j

    Exit Sub

    End If

    Next

    End Sub

     

    Tim

  • Re: How to position Formview to a specific record

    05-19-2008, 11:42 AM
    • Loading...
    • dwfreeman
    • Joined on 05-14-2008, 5:59 PM
    • Posts 256

    Thanks cashmore. Your code sample produces 2 errors that say

    Type DataView is not defined and

    Type DataTable is not defined.

    Can you recommend a fix for those?

  • Re: How to position Formview to a specific record

    05-19-2008, 12:23 PM
    Answer
    • Loading...
    • dwfreeman
    • Joined on 05-14-2008, 5:59 PM
    • Posts 256

    Aamador -

    Thanks for your suggestion. With a little experimenting I found that the following would work:

      Protected Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
            '    Dim CurrentRecord As String = Request.QueryString("user_id")
            Dim i As Int32
            Dim CurrentRecord As Integer = Request("user_id")
            For i = 0 To FormView1.DataItemCount - 1
                FormView1.PageIndex = i
                If FormView1.DataItem("User_Id") = CurrentRecord Then
                    Exit For
                End If
            Next
        End Sub

    The problem with the DataItemCount was the placement of the code in the Page_Init. Putting it in the PreRender position solved that. Also the Databind was not needed, just indexing the PageIndex did the trick. I do have one follow on question though. You replaced the querystring with a Request("user_id"). Just so I understand correctly, to what does this refer? I ask this because there is no textbox or other control on the previous page with this name. So my conclusion is that it simply refers to the datasource on the previous page. Would that be correct?

    Also, what's the correct way to tag code on this site?

    Thanks againYes

    -Don 

     

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter