Dropdownlist SelectedIndexChanged event

Discussion in 'ASP .Net Web Controls' started by Halvor Nyberg, Sep 29, 2003.

  1. 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>
    Halvor Nyberg, Sep 29, 2003
    #1
    1. Advertising

  2. 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/


    "Halvor Nyberg" <> wrote in message
    news:012b01c38697$53a12800$...
    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.
    Ken Cox [Microsoft MVP], Sep 30, 2003
    #2
    1. Advertising

  3. Halvor Nyberg

    jeroen Guest

    Olso don't forget to set the Autopostback property of your
    control to TRUE, otherwise it won't fire a postback.
    jeroen, Sep 30, 2003
    #3
  4. Thanks Ken !
    Halvor Nyberg, Sep 30, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominic
    Replies:
    2
    Views:
    716
    Saravana
    Jul 4, 2003
  2. Robert Hanson
    Replies:
    0
    Views:
    525
    Robert Hanson
    Jul 10, 2003
  3. Robert Hanson
    Replies:
    0
    Views:
    457
    Robert Hanson
    Jul 10, 2003
  4. Pepito Grillo

    SelectedIndexChanged event in DropDownList

    Pepito Grillo, Jul 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    423
    Pepito Grillo
    Jul 28, 2003
  5. DesignerX
    Replies:
    2
    Views:
    3,825
    Bob Carsto
    Nov 14, 2003
Loading...

Share This Page