SelectedIndexChanged event masked

Joined
Sep 15, 2010
Messages
1
Reaction score
0
Hello all,

I'm new to ASP.Net (using 2010 pro version) and I have a problem with ASP.NET that is giving me fits trying to solve it.

I am trying to catch the SelectedIndexChanged event on the ddlParent DropDownList and use it's SelectedValue in code behind to repopulate the ddlChild DropDownList.

I've distilled it down to some very simple test code and I'd appreciate some understanding of why this is not working as expected and what I should do to make it work (In VB please).

I get the following compiler error:

Compiler Error Message: BC30456: 'ddlParent_SelectedIndexChanged' is not a member of 'ASP.test_aspx'.

Test Case code follows:

<%@ Page Language="vb" AutoEventWireup="True" CodeBehind="Test.aspx.vb" Inherits="WebApplication2.Test" %>

<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="ddlParent" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlParent_SelectedIndexChanged"
style="display: inline;">

<asp:ListItem Text="-- Select --" Value="0" />
<asp:ListItem Text="Parent #1" Value="1" />
<asp:ListItem Text="Parent #2" Value="2" />
</asp:DropDownList>

<br />

<asp:DropDownList ID="ddlChild" runat="server"
style="display: inline;">
</asp:DropDownList>

</form>
</body>
</html>

Public Class Test
Inherits System.Web.UI.Page

Private Sub ddlParent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If Not ddlChild Is Nothing Then
ddlChild.Items.Clear()

Select Case sender.SelectedValue
Case 1
ddlChild.Items.Add(New ListItem("Parent 1 Choice 1", 11))
ddlChild.Items.Add(New ListItem("Parent 1 Choice 2", 12))
Case 2
ddlChild.Items.Add(New ListItem("Parent 2 Choice 1", 21))
ddlChild.Items.Add(New ListItem("Parent 2 Choice 2", 22))
End Select
End If
End Sub
End Class




Code will work correctly if I remove OnSelectedIndexChanged="ddlParent_SelectedIndexChanged" from ddlParent
control and add a Handles clause to ddlParent.SelectedIndexChanged event in code behind like so:


<%@ Page Language="vb" AutoEventWireup="True" CodeBehind="Test.aspx.vb" Inherits="WebApplication2.Test" %>

<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="ddlParent" runat="server"
AutoPostBack="True"
style="display: inline;">

<asp:ListItem Text="-- Select --" Value="0" />
<asp:ListItem Text="Parent #1" Value="1" />
<asp:ListItem Text="Parent #2" Value="2" />
</asp:DropDownList>

<br />

<asp:DropDownList ID="ddlChild" runat="server"
style="display: inline;">
</asp:DropDownList>

</form>
</body>
</html>

Public Class Test
Inherits System.Web.UI.Page

Private Sub ddlParent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlParent.SelectedIndexChanged

If Not ddlChild Is Nothing Then
ddlChild.Items.Clear()

Select Case sender.SelectedValue
Case 1
ddlChild.Items.Add(New ListItem("Parent 1 Choice 1", 11))
ddlChild.Items.Add(New ListItem("Parent 1 Choice 2", 12))
Case 2
ddlChild.Items.Add(New ListItem("Parent 2 Choice 1", 21))
ddlChild.Items.Add(New ListItem("Parent 2 Choice 2", 22))
End Select
End If
End Sub
End Class



To Further complicate the issue, I want to put both DropDownLists in a FormView (in my real code they are databound).
When I put them in a Formview, the Handles clause no longer works and I still can't add the OnSelectedIndexChanged attribute to ddlParent without getting the same
Compiler Error Message: BC30456: 'ddlParent_SelectedIndexChanged' is not a member of 'ASP.test_aspx'.

Test case code for this is:

<%@ Page Language="vb" AutoEventWireup="False" CodeBehind="Test.aspx.vb" Inherits="WebApplication2.Test" %>

<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<asp:FormView ID="fvTest" runat="server" DefaultMode="Edit">
<EditItemTemplate>


<asp:DropDownList ID="ddlParent" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlParent_SelectedIndexChanged"
style="display: inline;">

<asp:ListItem Text="-- Select --" Value="0" />
<asp:ListItem Text="Parent #1" Value="1" />
<asp:ListItem Text="Parent #2" Value="2" />
</asp:DropDownList>

<br />

<asp:DropDownList ID="ddlChild" runat="server"
style="display: inline;">
</asp:DropDownList>

</EditItemTemplate>
</asp:FormView>

</form>
</body>
</html>

Public Class Test
Inherits System.Web.UI.Page

Private Sub ddlParent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ddlChild As DropDownList = fvTest.FindControl("ddlChild")

If Not ddlChild Is Nothing Then
ddlChild.Items.Clear()

Select Case sender.SelectedValue
Case 1
ddlChild.Items.Add(New ListItem("Parent 1 Choice 1", 11))
ddlChild.Items.Add(New ListItem("Parent 1 Choice 2", 12))
Case 2
ddlChild.Items.Add(New ListItem("Parent 2 Choice 1", 21))
ddlChild.Items.Add(New ListItem("Parent 2 Choice 2", 22))
End Select
End If
End Sub
End Class

I just can't seem to catch the SelectedIndexChanged event for the DropDownLists if I put the DropDownLists in a FormView.
What am I missing here? Seems like this should be dirt simple to do. Any help would be greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top