Dropdownlist SelectedIndexChanged event

H

Halvor Nyberg

Hi,
I want to respond when the user picks another item in the
dropdownlist.

In the html section I got:
... SelectedIndexChanged="MyListChange"
(see example below)

I the script section I have the sub
sub MyListChange

MyListChange is not triggered after the Postback.
Is this logical, or have I missed something?

thanks.


<%@ Page Language="VB" %>
<script runat="server">

dim t_resp as string

Sub Page_Load(Sender As Object, E As EventArgs)


If Not Page.IsPostBack Then

dim i as integer
Dim myList As New ArrayList()
for i=0 to 10
myList.add("Test " & i)
next
DropDownList1.DataSource = myList
DropDownList1.DataBind()
datagrid1.DataSource = myList
datagrid1.DataBind()
else
t_resp="Post back Only"
End If


End Sub

Sub ApplyFilter_Click(Sender As Object, E As EventArgs)
t_resp= ("Apply button: " &
DropDownList1.SelectedItem.Text)
msgbox(t_resp)
end sub

public sub MyListChange(sender As Object, e As
EventArgs)
t_resp=("Dropdown Change: " &
DropDownList1.SelectedItem.Text)

end sub

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>test
</h2>
<hr size="1" />
<form id="F1" runat="server" EnableViewState="true">
<p>
Test&nbsp;<asp:DropDownList id="DropDownList1"
runat="server" SelectedIndexChanged="MyListChange"
AutoPostBack="true" EnableViewState="true"
Width="235px"></asp:DropDownList>
&nbsp; <asp:Label id="lblCur" runat="server"
forecolor="Red"> </asp:Label>
</p>
<p>
Test:
<asp:TextBox id="TextBox2"
runat="server"></asp:TextBox>
<asp:Button id="Button1"
onclick="ApplyFilter_Click" runat="server"
Text="Show"></asp:Button>
</p>
<asp:datagrid id="DataGrid1" runat="server"
EnableViewState="true" CellSpacing="1" GridLines="None"
CellPadding="3" BackColor="White" ForeColor="Black"
AutoGenerateColumns="True">
<HeaderStyle font-bold="True"
forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
<%response.write(t_resp)%><%%>
</form>
</body>
</html>
 
K

Ken Cox [Microsoft MVP]

Hi Halvor,

You made a typo... you wanted onselectedindexchanged. My version is below.

Does this help?

Ken
MVP [ASP.NET]

<%@ Page Language="vb" AutoEventWireup="true" %>
<html>
<head>
<script runat="server">
dim t_resp as string
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
dim i as integer
Dim myList As New ArrayList()
for i=0 to 10
myList.add("Test " & i)
next
DropDownList1.DataSource = myList
DropDownList1.DataBind()
datagrid1.DataSource = myList
datagrid1.DataBind()
else
t_resp="Post back Only"
End If
End Sub

Sub ApplyFilter_Click _
(Sender As Object, E As EventArgs)
t_resp= ("Apply button: " & _
DropDownList1.SelectedItem.Text)
' msgbox(t_resp)
end sub

public sub MyListChange _
(sender As Object, e As EventArgs)
t_resp=("Dropdown Change: " & _
DropDownList1.SelectedItem.Text)
end sub

</script>
</head>
<body style="FONT-FAMILY: arial">
<h2>test
</h2>
<hr size="1">
<form id="F1" runat="server">
<p>
Test&nbsp;<asp:dropdownlist id="DropDownList1" runat="server"
onselectedindexchanged="MyListChange" autopostback="true"
enableviewstate="true" width="235px"></asp:dropdownlist>
&nbsp;
<asp:label id="lblCur" runat="server" forecolor="Red"></asp:label>
</p>
<p>
Test:
<asp:textbox id="TextBox2" runat="server"></asp:textbox>
<asp:button id="Button1" onclick="ApplyFilter_Click" runat="server"
text="Show"></asp:button>
</p>
<asp:datagrid id="DataGrid1" runat="server" enableviewstate="true"
cellspacing="1" gridlines="None"
cellpadding="3" backcolor="White" forecolor="Black"
autogeneratecolumns="True">
<headerstyle font-bold="True" forecolor="white"
backcolor="#4A3C8C"></headerstyle>
<itemstyle backcolor="#DEDFDE"></itemstyle>
</asp:datagrid>
<%response.write(t_resp)%>
<%%>
</form>
</body>
</html>


--

Help stop SWEN in the newsgroups - Get the W32.Swen.A@mm Removal Tool:
http://securityresponse.symantec.com/avcenter/venc/data/[email protected]


Hi,
I want to respond when the user picks another item in the
dropdownlist.

In the html section I got:
... SelectedIndexChanged="MyListChange"
(see example below)

I the script section I have the sub
sub MyListChange

MyListChange is not triggered after the Postback.
Is this logical, or have I missed something?

thanks.
 
J

jeroen

Olso don't forget to set the Autopostback property of your
control to TRUE, otherwise it won't fire a postback.
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top