Solved

Gridview RowEditing Can't FindControl V2

Posted on 2013-02-06
8
685 Views
Last Modified: 2013-02-12
Oops, I accepted a solution that is not working , same problem. ( http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28017315.html#a38859213 )

When entering Edit mode on my GridView I place the focus on a field with the FindControl method. All is well, but if I move to page #2 my code crashes, (ArgumentOutOfRangeException).

Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

        If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex
        gv_Gifts.DataBind()
        gv_Gifts.Rows(e.NewEditIndex).FindControl("giftTitle").Focus()

    End Sub

Open in new window


After going to page 2 and entering edit on the first row
e.NewEditIndex = 10
.PageSize = 10
.PageIndex = 1

Help?
0
Comment
Question by:rcl58
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38859581
So what happens when you incorporate the change suggested in your previous question?
0
 

Author Comment

by:rcl58
ID: 38859662
Same ArgumentOutOfRangeException exeption...

correctIndex = e.NewEditIndex + (GridView1.PageSize * GridView1.PageIndex)" resulted in a value of 20.
0
 

Author Comment

by:rcl58
ID: 38860839
Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

        If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex

        LoadOffers() 
        'NOTE: This queries db, creates dataset, assigns table to  gv_Gifts.DataSource, and binds
 
        Dim correctIndex As Integer 
        correctIndex = e.NewEditIndex - (gv_Gifts.PageSize * gv_Gifts.PageIndex)

        gv_Gifts.Rows(correctIndex).FindControl("giftTitle").Focus()

    End Sub 

Open in new window

Ok something strange is going on. Again it works on the first page.

GRID PAGE ONE (second record down)
Private Sub gv_Gifts_RowEditing...
e.NewEditIndex........1
e.PageSize...........10
e.PageIndex...........0
gv.Rows.Count........10

GRID PAGE TWO (second record down)
Private Sub gv_Gifts_RowEditing...
 e.NewEditIndex......11
 e.PageSize..........10
 e.PageIndex..........1
 gv.Rows.Count........3
 
 Hence, correctIndex = e.NewEditIndex - ( gv_Gifts.PageSize *  gv_Gifts.PageIndex)
 
 GRID PAGE ONE
  gv_Gifts.Rows(1).FindControl("giftTitle").Focus() - WORKS
 
 GRID PAGE TWO
  gv_Gifts.Rows(1).FindControl("giftTitle").Focus() - FAIL

ALSO...

Entering edit mode with a double click.

 Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

        For Each row As GridViewRow In gv_Gifts.Rows
            If row.RowType = DataControlRowType.DataRow Then

                If gv_Gifts.EditIndex <> row.DataItemIndex Then

                 row.Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperlink(gv_Gifts, "Edit$" & row.DataItemIndex, True)

                End If
            End If
        Next

        MyBase.Render(writer)
    End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38863471
So it fails with

correctIndex = e.NewEditIndex - ( gv_Gifts.PageSize *  gv_Gifts.PageIndex)

and

correctIndex = e.NewEditIndex + ( gv_Gifts.PageSize *  gv_Gifts.PageIndex)


>GRID PAGE TWO (second record down)
?Private Sub gv_Gifts_RowEditing...
>e.NewEditIndex......11

Is that neweditindex 11 before the calculation? It should be 1 on page 2.
0
 

Author Comment

by:rcl58
ID: 38863568
Yes it fails with both "+" and "-" in the calculation. Yes it is 11 before the calculation.

Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

 If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex
        LoadOffers()
     
        Diagnostics.Debug.WriteLine(" e.NewEditIndex..........." & e.NewEditIndex.ToString)
        Diagnostics.Debug.WriteLine(" e.PageSize..........." & gv_Gifts.PageSize.ToString)
        Diagnostics.Debug.WriteLine(" e.PageIndex..........." & gv_Gifts.PageIndex.ToString)
        Diagnostics.Debug.WriteLine(" .Rows.Count.........." & gv_Gifts.Rows.Count.ToString)

        Dim correctIndex As Integer = e.NewEditIndex - (gv_Gifts.PageSize * gv_Gifts.PageIndex)

        Diagnostics.Debug.WriteLine(" correctIndex ..........." & correctIndex.ToString)

        gv_Gifts.Rows(correctIndex ).FindControl("giftTitle").Focus() 'Fails Here on Page Two

Open in new window

GRID PAGE ONE (second record down)
e.NewEditIndex........1
 e.PageSize...........10
 e.PageIndex...........0
 .Rows.Count..........10
 correctIndex .........1
 
 GRID PAGE TWO (second record down)
 e.NewEditIndex.......11
 e.PageSize...........10
 e.PageIndex...........1
 .Rows.Count...........3
 correctIndex .........1


Also, LoadOffers() calls this code with current sort and direction params.

 Private Sub LoadOffers(sortExpression As String, direction As String)

        ViewState("sortField") = sortExpression
        If direction = ASCENDING Then
            GridViewSortDirection = SortDirection.Ascending
        Else
            GridViewSortDirection = SortDirection.Descending
        End If

        Dim Conn As SqlConnection
        Dim Cmd As SqlCommand
        Dim dAdapter As New SqlDataAdapter
        Dim dsGifts As New DataSet

        Conn = New SqlClient.SqlConnection(dbConn())
        Cmd = New SqlCommand("sp_Gift_Offers", Conn)
        Cmd.CommandType = CommandType.StoredProcedure
        Conn.Open()
        Cmd.ExecuteNonQuery()
        Conn.Close()
        dAdapter.SelectCommand = Cmd
        dAdapter.Fill(dsGifts, "Gifts")

        dsGifts.Tables("Gifts").DefaultView.Sort = sortExpression & direction

        gv_Gifts.DataSource = dsGifts.Tables("Gifts")
        gv_Gifts.DataBind()

        'LoadOffers
    End Sub

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 38863643
If you handle the RowDataBound event of gridview instead and use following, does this code get hit?

            If e.Row.RowState = DataControlRowState.Edit
                e.Row.Cells(YourCellIndex).Controls(0).Focus()
            End If
0
 

Accepted Solution

by:
rcl58 earned 0 total points
ID: 38865576
Ok, I had to abandon using the Overrides Sub Render event that I hoped would let me enter the Edit mode of a record with a double-click. I was trying to adapt the code found here: http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx?CommentPosted=true#commentmessage

 For some reason placing
e.Row.Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperlink(gv_Gifts, "Edit$" & e.Row.DataItemIndex, True)

Open in new window

in the Render event made the e.NewEditIndex property report the wrong value in RowEditing event.

My solution:

(1) Move the “GetPostBackClientHyperlink(…” code line from Render to RowDataBound
(2) Change the e.Row.DataItemIndex param to e.Row.RowIndex
(3) Change the “True” param to “False”

e.Row.Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperlink(gv_Gifts, "Edit$" & e.Row.RowIndex, False)

(4) By changing from “True” to “False” I had to add (<%@ Page EnableEventValidation="False" %>) to the page directive

(5) The RowEditing event e.NewEditIndex property then worked as expected.

Private Sub gv_Gifts_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_Gifts.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
         
            e.Row.Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperlink(gv_Gifts, "Edit$" & e.Row.RowIndex, False)

        End If

End Sub

Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

        If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex
        LoadOffers()

        gv_Gifts.Rows(e.NewEditIndex).FindControl("giftTitle").Focus()

        'gv_Gifts_RowEditing
End Sub

Open in new window

My Gridview now enters edit mode by double-clicking a row and places the cursor in the first textbox.
0
 

Author Closing Comment

by:rcl58
ID: 38879512
Thanks for the help CodeCruiser.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question