Solved

Gridview RowEditing Can't FindControl V2

Posted on 2013-02-06
8
667 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now