Solved

Gridview RowEditing Can't FindControl V2

Posted on 2013-02-06
8
677 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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