7

Pretty simple question. I'm quite certain I have the Class, method, codebehind, etc linked properly. Lot's of posts online say that this has something to do with compiling and/or dll/bin files, but none of their help has worked for me.

Compiler Error Message: BC30456: 'gvLegs_PageIndexChanging' is not a member of 'ASP.nestedgridview_aspx'.

Source Error:

Line 43:    <asp:Label ID="lblEmpName" runat="server" Text='<%# Eval("Location")%>'></asp:Label>
Line 44:    <asp:Literal runat="server" ID="lit1" Text="</td><tr id='trCollapseGrid' style='display:none' ><td colspan='5'>" />
Line 45:    <asp:GridView ID="gvLegs" AutoGenerateColumns="False" runat="server" EnableViewState="False"
Line 46:    DataKeyNames="EmployeeId" ForeColor="#333333" PageSize="4" AllowPaging="True"
Line 47:    OnPageIndexChanging="gvLegs_PageIndexChanging">
Source File: C:\Users\tstanley\Desktop\NestedVB\NestedVB\NestedGridView.aspx    Line: 45 

NestedGridView.aspx

<%@ Page Language="vb" AutoEventWireup="false" codebehind="NestedGridView.aspx.vb" Inherits="NestedVB.NestedGridViewPaging2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

NetedGridView.aspx.vb [Code Behind] ...

 Private Sub gvLegs_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)

If anyone has a fix for this, it would help me greatly so I can continue.... debugging the actual code lol.

Guy
  • 139
  • 2
  • 2
  • 7

1 Answers1

13

gvLegs_PageIndexChanging is private but needs to be protected or public.

Since you're using VB.NET you could also use the handles clause:

Private Sub gvLegs_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) _
    Handles gvLegs.PageIndexChanging
End Sub

Edit: Just to be clear, you have three options in ASP.NET with VB.NET to create event handlers:

  1. declaratively on aspx
  2. in code with handles clause
  3. with AddHandler (mostly for dynamical controls in VB.NET)

If you use option 1 the event handler must at least be protected since the aspx page inherits from the codebehind class.

If you use option 2 the method can be private but you need to remove the declarative event handler on the aspx.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Using the handles clause + Private, I get this error: "Event 'gvLegsPageIndexChanging' cannot be found." And if I use #2 (semi-unsure what 'declaratively on aspx' means, but using Protected), I get a completely blank web page - meaning something bad/worse is happening. – Guy Jun 22 '12 at 13:31
  • @Guy: Have you read the last sentence? You must remove the declarative event subscription for the `PageIndexChanging` event (declarative means on aspx, this part: `OnPageIndexChanging="gvLegs_PageIndexChanging"`) if you're using the `Handles` clause and making `gvLegs_PageIndexChanging` private. – Tim Schmelter Jun 22 '12 at 13:34
  • Ok well that's what I said I didnt understand lol. Ok got it. Now I have this fun error: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types." Shouldnt gridview have this event defined already? [Note: gvLegs is a nested griview inside a templatefield in a parentGridView, does this change anything? --> As in, it is not auto-created in the .designer.vb file] – Guy Jun 22 '12 at 13:42
  • @Guy: The GridView has the `WithEvents` automatically when it's declared on the aspx page on the top level(so not nested in a child control) and is not created dynamically. When it's a nested GridView you need to use option 1 or 3. Note that you need to make `gvLegs_PageIndexChanging` protected as already mentioned if you use the aspx approach (with `AddHandler` it can be private). – Tim Schmelter Jun 22 '12 at 13:50
  • Ok thanks for the help. You solved my original problem, now I just have to figure out where to put the AddHandler and every other issue I'm going to hit. – Guy Jun 22 '12 at 14:24
  • @Guy: It would be easier to use your TemplateField where you've declared the inner GridView and add the event handler there (remember to make it `Protected`). If you want to use `AddHandler` instead you could use the outer GridView's `RowCreated` event and `FindControl` to get the reference to your inner GridView. Then you can use following to add the handler programmatically: `AddHandler gvLegs.PageIndexChanging, AddressOf gvLegs_PageIndexChanging`. – Tim Schmelter Jun 22 '12 at 14:46