Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gridview RowEditing Can't FindControl V2

Posted on 2013-02-06
8
Medium Priority
?
709 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
Independent Software Vendors: 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 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