error handles clause requires With Events Variable

S

Sean

HI There,

I am trying to submit a form when a selection is made from a dropdown list,
I keep getting the error "Handles clause requires With Events Variable".
Could someone help me with the syntax?


Sean - Thanks in advance


<script language="vb" runat="server">

Private Sub lstRegion_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lstRegion.SelectedIndexChanged
response.write ("test")
End Sub

</script>

<html>
<body>
<form runat=server>

<ASP:DropDownList id=lstRegion AutoPostBack="true" name=lstRegion
maxlength= "40" SelectedIndexChanged=lstRegion_SelectedIndexChanged
runat=server>
<asp:ListItem Value="1">New South Wales</asp:ListItem>
<asp:ListItem Value="2">Victoria</asp:ListItem>
<asp:ListItem Value="3">Tasmania</asp:ListItem>
<asp:ListItem Value="4">South Australia</asp:ListItem>
<asp:ListItem Value="5">Western Australia</asp:ListItem>
<asp:ListItem Value="6">Northern Territory</asp:ListItem>
<asp:ListItem Value="7">Queensland</asp:ListItem>
</ASP:DropDownList>


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

Alan Ferrandiz Langley

When you try to submit the ASP.NET Web Form with a dropdownlist you do it by
creating an event handler for the SelectedIndexChanged event using the
AutoPostback property. Well, for your control to expose events it has to be
declared using the WithEvents clause otherwise you won't be able to handle
any event with your code. In short, your control declaration has to look
like this:

Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList

Alan Ferrandiz
MCT,MCDBA,MCSD
MSF Practitioner
 
S

Sean

Hi Alan,

I have made a declaration at the top of the page to use the namespace, in
regards to where I put the declaration that you posted I have tried to place
it all over the page but I still receive an error, could you help me out
with the syntax?

Seam

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<% @Import Namespace="System.Web.UI.WebControls.DropDownList" %>

<script language="vb" runat="server">


Sub Page_Load(sender as Object, e as EventArgs)

' BindData()

End Sub


Private Sub lstRegion_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lstRegion.SelectedIndexChanged
Protected WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList
response.write ("test")
End Sub


</script>

<html>
<body>
<form runat=server>

<ASP:DropDownList id=lstRegion AutoPostBack="true" name=lstRegion
maxlength= "40" SelectedIndexChanged=lstRegion_SelectedIndexChanged
runat=server>
<asp:ListItem Value="1">New South Wales</asp:ListItem>
<asp:ListItem Value="2">Victoria</asp:ListItem>
<asp:ListItem Value="3">Tasmania</asp:ListItem>
<asp:ListItem Value="4">South Australia</asp:ListItem>
<asp:ListItem Value="5">Western Australia</asp:ListItem>
<asp:ListItem Value="6">Northern Territory</asp:ListItem>
<asp:ListItem Value="7">Queensland</asp:ListItem>
</ASP:DropDownList>


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

Alan Ferrandiz Langley

Well apparently you were missing the quotes when mapping the OnSelectedIndexChanged event to a lstRegion_SelectedIndexChanged event handler, I made that change to the code below and it runs ok. Plus you don't need to declare a variable for your control since you are working inline code, declaring a <% @Import Namespace="System.Web.UI.WebControls.DropDownList" %> statement is not correct since it is not a namespace but a class.


Hope this helps

Alan Ferrandiz Langley
MCT, MCDBA, MCSD
MSF Practitioner

<HTML>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">

Sub Page_Load(sender as Object, e as EventArgs)
If Not IsPostBack Then
Response.Write ("Page_Load")
End If
End Sub

Sub lstRegion_SelectedIndexChanged(sender as Object, e as EventArgs)
Response.Write (lstRegion.SelectedItem.Text)
End Sub

</script>
<body>
<form runat="server" ID="Form1">
<asp:DropDownList id="lstRegion" AutoPostBack="true" maxlength="40" runat="server" OnSelectedIndexChanged="lstRegion_SelectedIndexChanged">
<asp:ListItem Value="1">New South Wales</asp:ListItem>
<asp:ListItem Value="2">Victoria</asp:ListItem>
<asp:ListItem Value="3">Tasmania</asp:ListItem>
<asp:ListItem Value="4">South Australia</asp:ListItem>
<asp:ListItem Value="5">Western Australia</asp:ListItem>
<asp:ListItem Value="6">Northern Territory</asp:ListItem>
<asp:ListItem Value="7">Queensland</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</HTML>
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top