471,330 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

datagrid error on change page (VB.ASP.NET)

I am getting an error in my code that references a textbox inside the
current row of my datagrid, whenever I try changing the page (paging is
enabled). This code doesn't even run unless a button in the datagrid is
clicked, but is erroring out the page when I click page 2,3,etc.

I have tried a few different notations without any luck. Can someone
look at the lines below and tell me what might be wrong? Thanks in
advance!

'event being used to change page:
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles
DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
Call BindGrid() ' bind data to the datagrid
End Sub

'code that the error is occurring in when page changed
'contains code for buttons inside template columns in datagrid
'in the .aspx file the datagrid's OnItemCommand property
'is set to reference this sub:
' OnItemCommand="ExecuteDataGridCommands"
Public Sub ExecuteDataGridCommands(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)

'reference textbox "txtID" inside current row of datagrid
Dim txtID As TextBox

'the following line is where the error occurs:
txtID = DataGrid1.Items(e.Item.ItemIndex).FindControl("txt ID")

' i tried this instead, a paging error doesnt occur,
' but this line causes a different error (control not found):
' txtID = DataGrid1.FindControl("txtID")

'someone suggested this method, but it didn't work
'Dim iIndex As Int32
'iIndex = e.Item.ItemIndex - (DataGrid1.CurrentPageIndex *
DataGrid1.PageSize)
'txtID = DataGrid1.Items(iIndex).FindControl("txtID")

etc.

Jul 21 '05 #1
1 2113
I figured it out...

Evidently the datagrid template columns code is evaluated when a change
page event happens, and at the moment it is evaluated, the current
datagrid item is -1. Simply putting in a check for -1 solved the
probrem:

Public Sub ExecuteDataGridCommands(ByVal sender As Object, ByVal e
As DataGridCommandEventArgs)
If e.Item.ItemIndex > -1 Then
Dim txtID As TextBox
txtID =
DataGrid1.Items(e.Item.ItemIndex).FindControl("txt ID")

Mad Scientist Jr wrote:
I am getting an error in my code that references a textbox inside the
current row of my datagrid, whenever I try changing the page (paging is enabled). This code doesn't even run unless a button in the datagrid is clicked, but is erroring out the page when I click page 2,3,etc.


Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by John Lau | last post: by
6 posts views Thread by Dee | last post: by
9 posts views Thread by Patrick | last post: by
1 post views Thread by Mad Scientist Jr | last post: by
2 posts views Thread by Brian Tkatch | last post: by
reply views Thread by shapper | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.