DropDownList control and OnSelectedIndexChanged event

Discussion in 'ASP .Net' started by =?Utf-8?B?Z2xlbm4=?=, Apr 21, 2006.

  1. Hi folks,

    I am trying to determine which item in a DropDownList Web control has been
    selected.

    I have posted an OnSelectedIndexChanged subroutine in my code with a
    reference to the subroutine in my asp tag as shown here:
    <asp:DropDownList
    id="ddlTo"
    runat="server"
    DataValueField="cname"
    AutoPostBack="True"
    DataSource='<%# GetCompanyNamesForThisProject() %>'

    OnSelectedIndexChanged="ddlTo_SelectedIndexChanged"

    />

    My subroutine has a Response.Write inside to see if the event is fired as
    shown here:

    Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
    Response.Write ("your dropdownlist event has fired")
    End Sub

    I successfully have fired the event when I select an item in the
    dropdownlist control where the text "your dropdownlist event has fired"
    writes to the screen.

    Unfortunately, when I attempt to get the index number of the item I
    selected, I get an error stating that ddlTo is not declared. ddlTo is the id
    that was set in the <asp:DropDownList tag.

    Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim indexNum as integer
    indexNum = ddlTo.SelectedIndex
    Response.Write(indexNum )
    End Sub

    It seems to me that ddlTo was declared in the asp tag as the id. How else
    should I be declaring ddlTo?

    Thanks for any help on this...
    Glenn
     
    =?Utf-8?B?Z2xlbm4=?=, Apr 21, 2006
    #1
    1. Advertising

  2. Hello again folks,

    Another thought I had is that this DropDownList control is in a DataGrid
    control and is not visible until an Edit link (in the same row is clicked).
    Therefore, the control is written out instead of double clicking it in design
    mode that automatically writes the code for the Sub and End Sub statements.

    Not sure if this changes things but I have tested a small sample with code
    shown here. Oddly enough, the sample code runs without any problem. Go
    figure ??

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

    Sub Button_Click(sender As Object, e As EventArgs)
    Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
    End Sub 'Button_Click

    Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim indexNum as integer
    indexNum = DropDownList1.SelectedIndex
    Response.Write(indexNum)
    End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">
    <h3>DropDownList Example
    </h3>
    Select an item from the list and click the submit button.
    <p>
    <asp:DropDownList id="DropDownList1" runat="server"
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    AutoPostBack="True">
    <asp:ListItem Value="Item 1">Item 1</asp:ListItem>
    <asp:ListItem Value="Item 2">Item 2</asp:ListItem>
    <asp:ListItem Value="Item 3">Item 3</asp:ListItem>
    <asp:ListItem Value="Item 4">Item 4</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:Button id="Button1" onclick="Button_Click" runat="server"
    Text="Submit"></asp:Button>
    <br />
    <br />
    <asp:Label id="Label1" runat="server"></asp:Label>
    </p>
    </form>
    </body>
    </html>




    "glenn" wrote:

    > Hi folks,
    >
    > I am trying to determine which item in a DropDownList Web control has been
    > selected.
    >
    > I have posted an OnSelectedIndexChanged subroutine in my code with a
    > reference to the subroutine in my asp tag as shown here:
    > <asp:DropDownList
    > id="ddlTo"
    > runat="server"
    > DataValueField="cname"
    > AutoPostBack="True"
    > DataSource='<%# GetCompanyNamesForThisProject() %>'
    >
    > OnSelectedIndexChanged="ddlTo_SelectedIndexChanged"
    >
    > />
    >
    > My subroutine has a Response.Write inside to see if the event is fired as
    > shown here:
    >
    > Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
    > Response.Write ("your dropdownlist event has fired")
    > End Sub
    >
    > I successfully have fired the event when I select an item in the
    > dropdownlist control where the text "your dropdownlist event has fired"
    > writes to the screen.
    >
    > Unfortunately, when I attempt to get the index number of the item I
    > selected, I get an error stating that ddlTo is not declared. ddlTo is the id
    > that was set in the <asp:DropDownList tag.
    >
    > Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
    > Dim indexNum as integer
    > indexNum = ddlTo.SelectedIndex
    > Response.Write(indexNum )
    > End Sub
    >
    > It seems to me that ddlTo was declared in the asp tag as the id. How else
    > should I be declaring ddlTo?
    >
    > Thanks for any help on this...
    > Glenn
    >
     
    =?Utf-8?B?Z2xlbm4=?=, Apr 21, 2006
    #2
    1. Advertising

  3. Controls within a DataGrid only have a scope within that datagrid, to access
    the control outside of that scope you have to use the find control method of
    the parent control and cast the return value corect type for the child
    control. In you case though because you want to reference the DropDownList
    from within it's own event handler you could use the sender object within the
    event handler.

    In C# you would do something like the following

    protected void ddlTo_SelectedIndexChanged(object sender, e EventArgs)
    {
    DropDownList MyDDL = (DropDownList)sender;
    }

    MyDDL is a reference to the DropDownList control that triggered the
    SelectedIndexChanged event and you can treat it like any other DDL. Sorry i
    don't really speak VB, but i think the Vb equivelent of (DropDownList)sender
    is something like CType(sender).



    DataGrid.FindControl("DropdownListID");


    "glenn" wrote:

    > Hello again folks,
    >
    > Another thought I had is that this DropDownList control is in a DataGrid
    > control and is not visible until an Edit link (in the same row is clicked).
    > Therefore, the control is written out instead of double clicking it in design
    > mode that automatically writes the code for the Sub and End Sub statements.
    >
    > Not sure if this changes things but I have tested a small sample with code
    > shown here. Oddly enough, the sample code runs without any problem. Go
    > figure ??
    >
    > <%@ Page Language="vb" %>
    > <script runat="server">
    >
    > Sub Button_Click(sender As Object, e As EventArgs)
    > Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
    > End Sub 'Button_Click
    >
    > Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
    > Dim indexNum as integer
    > indexNum = DropDownList1.SelectedIndex
    > Response.Write(indexNum)
    > End Sub
    >
    > </script>
    > <html>
    > <head>
    > </head>
    > <body>
    > <form runat="server">
    > <h3>DropDownList Example
    > </h3>
    > Select an item from the list and click the submit button.
    > <p>
    > <asp:DropDownList id="DropDownList1" runat="server"
    > OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    > AutoPostBack="True">
    > <asp:ListItem Value="Item 1">Item 1</asp:ListItem>
    > <asp:ListItem Value="Item 2">Item 2</asp:ListItem>
    > <asp:ListItem Value="Item 3">Item 3</asp:ListItem>
    > <asp:ListItem Value="Item 4">Item 4</asp:ListItem>
    > </asp:DropDownList>
    > <br />
    > <br />
    > <asp:Button id="Button1" onclick="Button_Click" runat="server"
    > Text="Submit"></asp:Button>
    > <br />
    > <br />
    > <asp:Label id="Label1" runat="server"></asp:Label>
    > </p>
    > </form>
    > </body>
    > </html>
    >
    >
    >
    >
    > "glenn" wrote:
    >
    > > Hi folks,
    > >
    > > I am trying to determine which item in a DropDownList Web control has been
    > > selected.
    > >
    > > I have posted an OnSelectedIndexChanged subroutine in my code with a
    > > reference to the subroutine in my asp tag as shown here:
    > > <asp:DropDownList
    > > id="ddlTo"
    > > runat="server"
    > > DataValueField="cname"
    > > AutoPostBack="True"
    > > DataSource='<%# GetCompanyNamesForThisProject() %>'
    > >
    > > OnSelectedIndexChanged="ddlTo_SelectedIndexChanged"
    > >
    > > />
    > >
    > > My subroutine has a Response.Write inside to see if the event is fired as
    > > shown here:
    > >
    > > Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
    > > Response.Write ("your dropdownlist event has fired")
    > > End Sub
    > >
    > > I successfully have fired the event when I select an item in the
    > > dropdownlist control where the text "your dropdownlist event has fired"
    > > writes to the screen.
    > >
    > > Unfortunately, when I attempt to get the index number of the item I
    > > selected, I get an error stating that ddlTo is not declared. ddlTo is the id
    > > that was set in the <asp:DropDownList tag.
    > >
    > > Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
    > > Dim indexNum as integer
    > > indexNum = ddlTo.SelectedIndex
    > > Response.Write(indexNum )
    > > End Sub
    > >
    > > It seems to me that ddlTo was declared in the asp tag as the id. How else
    > > should I be declaring ddlTo?
    > >
    > > Thanks for any help on this...
    > > Glenn
    > >
     
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 21, 2006
    #3
    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. Alexander Reichman

    Dropdownlist onselectedIndexChanged event not fires

    Alexander Reichman, Oct 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    23,363
  2. Paul L
    Replies:
    1
    Views:
    7,357
  3. Replies:
    8
    Views:
    789
  4. TS
    Replies:
    2
    Views:
    1,060
  5. Paul L
    Replies:
    0
    Views:
    266
    Paul L
    May 5, 2005
Loading...

Share This Page