• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

Gridview checkbox oncheckchanged event debug error

In my ASP.Net 2 web app. I have a gridview with a checkbox in an ascx page. I am trying to add an event handler for the checkbox oncheckchanged event. Please see my simplified code below. When  I build/debug in VS2008 I get the following error:

'chkStudent_OnCheckedChanged' is not a member of 'ASP.StudentPlanning'.

I'm sure I'm missing something very simple - can anybody help please?
HTML CODE
------------------------
 <asp:GridView ID="grdAddStudents" runat="server" AutoGenerateColumns="False"  CellPadding="4" 
                                             ForeColor="#333333" Font-Size="X-Small" Width="380px" AllowSorting="True" 
                                            AllowPaging="True" DataKeyNames="ID">
          <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>  
                            <asp:Label runat="server" ID="HeaderLevelCheckBox" Text="" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox runat="server" ID="chkStudent" OnCheckedChanged="chkStudent_OnCheckedChanged" AutoPostBack="true" /> 
                        </ItemTemplate>
                        <ItemStyle Width="5px" />
                    </asp:TemplateField>
                    
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                        Visible="False" >                  
                        <ItemStyle Width="5px" />
                                             </asp:BoundField>
                                       
                    <asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" >
                        <ItemStyle Width="75px" />
                                             </asp:BoundField>
                 
           </Columns>
  </asp:GridView>



CODE BEHIND CODE
--------------------------------------
 Protected Sub chkStudent_OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

  'do some stuff here

End Sub

Open in new window

0
Al230762
Asked:
Al230762
  • 6
  • 3
  • 2
  • +1
1 Solution
 
GlobaLevelCommented:
I believe that this is wrong:
ByVal sender As Object, ByVal e As EventArgs)

to
ByVal sender As Object, ByVal e As gridviewrowEventArgs)


something like that...I believe thats the hangup
0
 
Al230762Author Commented:
Thanks GlobalLevel - I have changed the sub as follows:
 Protected Sub chkPupil_OnCheckedChanged(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

... but still get the same error when debugging:

'chkStudent_OnCheckedChanged' is not a member of 'ASP.StudentPlanning'.
0
 
Tom BeckCommented:
I have noticed with my VS 2005 version that events don't always get wired up properly inside VS. The fix that always works for me is to copy my code behind code out of the current event handler into the clipboard, delete the event handler entirely.

--Go to the design view of the webform
--click the gridview, then click the small black arrow in the upper right of the gridview
--click Edit Templates (last option), your CheckBox will now be selectable
--select the CheckBox and look for your event handler on the Properties tab on the right
--double click the "chkStudent_OnCheckedChanged" event to re-create the event handler in code behind
--paste your code back in

I have version 2005, hopefully 2008 is the same.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Al230762Author Commented:
tommyBoy: many thanks for the suggestion. It seems in VS2008 that after selecting the checkbox in EditTemplate none of the event handlers appear in the Properties window. I think you are on the right track though. I may just try deleting the whole grid and starting again as the problem looks a bit like a bug to me.
0
 
Tom BeckCommented:
If none appear, just double click in the textbox next to the OnCheckedChanged event in the Properties tab and an OnCheckedChanged event will be created for you. It should replace the call in the markup for the CheckBox too.

Re-creating the gridview seems drastic.
0
 
ddayx10Commented:
The issue is just a simple mixup of how to properly name the event. The error message is correct there is no event "OnCheckChanged" the event should be named "CheckChanged" so if you change things around like this it will work:

<asp:CheckBox runat="server" ID="chkStudent" OnCheckedChanged="chkStudent_CheckedChanged" AutoPostBack="true" />

Protected Sub chkStudent_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
0
 
Al230762Author Commented:
ddayx10: I wish it were that simple! Thanks anyway. I don't think it matters how the Sub is actually named as long as the HTML refers correctly to it.
0
 
Al230762Author Commented:
ddayx10: Actually, my apologies, you were sort of right. It's the event name that is incorrect rather than the name of the Sub. The HTML should actually be:

CheckedChanged="xyz"

Note, the actual NAME of the Sub is irrelevant.

Intellisense only provides "OnCheckedChanged" - "CheckedChanged" is not shown in the event list for the checkbox.
0
 
Al230762Author Commented:
Simple typing error by the poster slightly confused the issue.
0
 
Tom BeckCommented:
That's mighty strange. In my VS 2005 environment the auto-generated attribute for the asp:Checkbox is OnCheckedChanged="xyz" and it fires the auto-generated event when I run the debugger. If I try to force the attribute in the markup to be CheckedChanged="xyz" I get an error "Attribute 'CheckedChanged' is not a valid attribute of element 'CheckBox'" and of course the event does not fire. Is there really that subtle difference between VS 2005 and VS 2008?
0
 
Al230762Author Commented:
tommyBoy: strange but true. I have tested again in VS2008. This works:

<asp:CheckBox runat="server" ID="chkStudent" CheckedChanged="xyz" AutoPostBack="true" />
                        </ItemTemplate>

The code below does NOT work and gives the error "'xyz' is not a member of 'ASP.MapPlanning'":

<asp:CheckBox runat="server" ID="chkStudent" OnCheckedChanged="xyz" AutoPostBack="true" />
                        </ItemTemplate>

"OnCheckedChanged" is listed by intellisense but "CheckedChanged" is not listed. Makes no sense but anyway thanks for your help.
0
 
ddayx10Commented:
:)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now