Solved

Gridview RowEditing Can't FindControl

Posted on 2013-02-01
5
1,256 Views
Last Modified: 2013-02-07
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).

What am I doing wrong??

 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()

    End Sub

Open in new window

0
Comment
Question by:rcl58
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 38845308
Check:
http://www.experts-exchange.com/Microsoft/Server_Applications/Q_24474303.html

The e.NewEditIndex value is relative to gv page you are displaying, thus you need to calculate the correct Index. (Insert at line 2)
Dim correctIndex As Integer = e.NewEditIndex
If (GridView1.PageIndex > 0) Then
  correctIndex = e.NewEditIndex + (GridView1.PageSize * GridView1.PageIndex
EndIf

Open in new window

Then use correctIndex in all your code lines 3 to11. (instead of e.NewEditIndex)
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38845486
You need to rebind your GV before getting the control. So is your LoadOffers() rebinding the GV?

gv_Gifts.EditIndex = e.NewEditIndex
gv_Gifts.DataBind()
0
 

Author Comment

by:rcl58
ID: 38859213
Oops, the accepted solution is not working , same problem.

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

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

Also the binding code change suggestion didn't help:

gv_Gifts.EditIndex = e.NewEditIndex
gv_Gifts.DatBind()

I'm still lost and may have to repost because I already accepted the solution.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38861833
is the gridview binding in markup? I f no, please post which event(s) are binding the gridview dynamically.
Can you post the exception details?
Also if you can post the contents of gv_Gifts.Rows before the exception is thrown. I am interested in the count property whether Rows(0) is the actual first row or is the 11 row.
NOte: 1) IF you can include before/after values of my suggested code changes will be great.
2) No need to ask a new question, just request the moderator to reopen the question.
0
 

Author Comment

by:rcl58
ID: 38865596
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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