selected item won't change

T

TJS

can someone tell me why the selected item won't reflect user change in this
little droplist example



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

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

mydropdown.Items.clear
mydropdown.Items.Add( New ListItem( "alpha", "alpha" ))
mydropdown.Items.Add( New ListItem( "beta", "beta" ))
mydropdown.Items.Add( New ListItem( "gamma", "gamma" ))

End Sub


Sub Select_Click(ByVal sender As Object, ByVal e As EventArgs)

Messagex.text &= " selected = "
Messagex.text &= mydropdown.SelectedIndex

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">

<table cellspacing="0" cellpadding="0" width="50%" border="0">
<tbody>
<tr valign="center">
<td align="left" width="100%">
<span>Select&nbsp;:</span>
</td>
<td align="left" width="100%">
<asp:DropDownList id="mydropdown" runat="server"
enableviewstate="true"></asp:DropDownList>
</td>
<td align="left" width="100%">
<asp:Button id="btnSelect" onclick="Select_Click"
runat="server" CausesValidation="False" Text="GO"></asp:Button>
</td>
</tr>
<tr valign="center">
<td align="left" width="100%">
<br />
<asp:Label id="Messagex" runat="server"
enableviewstate="true" forecolor="red" width="80%"></asp:Label></td>
</tr>
</tbody>
</table>
</form>

</body>
</html>
 
K

Karl

when the user clicks the button, Page_Load fires first, thus clearing the
dropdownlist and re-adding the listitems...

If Not PAge.IsPostBack then
mydropdown.Items.Add( New ListItem( "alpha", "alpha" ))
mydropdown.Items.Add( New ListItem( "beta", "beta" ))
mydropdown.Items.Add( New ListItem( "gamma", "gamma" ))
end if

Karl
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top