error handles clause requires With Events Variable

  • Thread starter Thread starter Sean
  • Start date Start date
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>
 
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
 
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>
 
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>
 
Back
Top