DropDownList - SelectedIndexChanged

Discussion in 'ASP .Net' started by j_stus@hotmail.com, Apr 27, 2006.

  1. Guest

    Hello there
    I was trying to find something about this on web but with no success.
    I am using code behind.
    Basically I am creating drop down list dynamically.
    Everything works but calling SelectedIndexChanged event never fires up.
    I found some discussions regarding this problem, but couldn't find
    answer....
    Here is my code.

    Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
    For z = 0 To dg.Items.Count - 1
    ddl = New System.Web.UI.WebControls.DropDownList
    ddl.Style("width") = "150"
    ddl.ID = z.ToString

    ddl.DataSource = dstCust
    ddl.DataTextField = "cust_name"
    ddl.DataValueField = "cust_id"
    ddl.DataBind()
    ddl.Attributes.Add("onchange", "return
    (ddl_SelectedIndexChanged(New Object, New System.EventArgs));")
    dg.Items(z).Cells(1).Controls.Add(ddl)
    Next

    Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
    Dim x As String = "This function is never called!!!!!!!"
    End Sub
     
    , Apr 27, 2006
    #1
    1. Advertising

  2. Adding the attribute will make the method to be called on the client side --
    that is not what you want.
    Remove this line
    ddl.Attributes.Add("onchange", "return(ddl_SelectedIndexChanged(New Object,
    New System.EventArgs));")

    Add a handler for the ddl instead
    AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged

    and then set the AutoPostBack property of the ddl to true.
    ddl.AutoPostBack = True

    --
    Swanand Mokashi
    Microsoft Certified Solution Developer (.NET) - Early Achiever
    Microsoft Certified Application Developer (.NET)

    http://www.dotnetgenerics.com/
    DotNetGenerics.com -- anything and everything about Microsoft .NET
    technology ...

    http://www.swanandmokashi.com/
    http://www.swanandmokashi.com/HomePage/WebServices/
    Home of the Stock Quotes, Quote of the day and Horoscope web services


    <> wrote in message
    news:...
    > Hello there
    > I was trying to find something about this on web but with no success.
    > I am using code behind.
    > Basically I am creating drop down list dynamically.
    > Everything works but calling SelectedIndexChanged event never fires up.
    > I found some discussions regarding this problem, but couldn't find
    > answer....
    > Here is my code.
    >
    > Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
    > For z = 0 To dg.Items.Count - 1
    > ddl = New System.Web.UI.WebControls.DropDownList
    > ddl.Style("width") = "150"
    > ddl.ID = z.ToString
    >
    > ddl.DataSource = dstCust
    > ddl.DataTextField = "cust_name"
    > ddl.DataValueField = "cust_id"
    > ddl.DataBind()
    > ddl.Attributes.Add("onchange", "return
    > (ddl_SelectedIndexChanged(New Object, New System.EventArgs));")
    > dg.Items(z).Cells(1).Controls.Add(ddl)
    > Next
    >
    > Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
    > Dim x As String = "This function is never called!!!!!!!"
    > End Sub
    >
     
    Swanand Mokashi, Apr 27, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for response.
    Unfortunatelly it's not working...
    Here is my code again

    Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
    For z = 0 To dg.Items.Count - 1
    ddl = New System.Web.UI.WebControls.DropDownList
    ddl.Style("width") = "150"
    ddl.ID = z.ToString

    ddl.DataSource = dstCust
    ddl.DataTextField = "cust_name"
    ddl.DataValueField = "cust_id"
    ddl.DataBind()
    AddHandler ddl.SelectedIndexChanged, AddressOf
    ddl_SelectedIndexChanged
    ddl.AutoPostBack = True
    Next

    Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
    Dim x As String = "This function is never called!!!!!!!"
    End Sub
     
    , Apr 27, 2006
    #3
  4. Is the form having a post back? Do you have any code in the Page load event
    ?

    --
    Swanand Mokashi
    Microsoft Certified Solution Developer (.NET) - Early Achiever
    Microsoft Certified Application Developer (.NET)

    http://www.dotnetgenerics.com/
    DotNetGenerics.com -- anything and everything about Microsoft .NET
    technology ...

    http://www.swanandmokashi.com/
    http://www.swanandmokashi.com/HomePage/WebServices/
    Home of the Stock Quotes, Quote of the day and Horoscope web services


    <> wrote in message
    news:...
    > Thanks for response.
    > Unfortunatelly it's not working...
    > Here is my code again
    >
    > Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
    > For z = 0 To dg.Items.Count - 1
    > ddl = New System.Web.UI.WebControls.DropDownList
    > ddl.Style("width") = "150"
    > ddl.ID = z.ToString
    >
    > ddl.DataSource = dstCust
    > ddl.DataTextField = "cust_name"
    > ddl.DataValueField = "cust_id"
    > ddl.DataBind()
    > AddHandler ddl.SelectedIndexChanged, AddressOf
    > ddl_SelectedIndexChanged
    > ddl.AutoPostBack = True
    > Next
    >
    > Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
    > Dim x As String = "This function is never called!!!!!!!"
    > End Sub
    >
     
    Swanand Mokashi, Apr 27, 2006
    #4
  5. Guest

    It is having post back.
    Also I have some code in the page load event, this code is executed
    only if
    If Not IsPostBack Then
     
    , Apr 27, 2006
    #5
  6. try removing the Handles ddl.SelectedIndexChanged from the event declaration
    as well as WithEvents in the declaration.

    How do you know the function is never called? Also are you getting any error
    ?

    --
    Swanand Mokashi
    Microsoft Certified Solution Developer (.NET) - Early Achiever
    Microsoft Certified Application Developer (.NET)

    http://www.dotnetgenerics.com/
    DotNetGenerics.com -- anything and everything about Microsoft .NET
    technology ...

    http://www.swanandmokashi.com/
    http://www.swanandmokashi.com/HomePage/WebServices/
    Home of the Stock Quotes, Quote of the day and Horoscope web services


    <> wrote in message
    news:...
    > It is having post back.
    > Also I have some code in the page load event, this code is executed
    > only if
    > If Not IsPostBack Then
    >
     
    Swanand Mokashi, Apr 27, 2006
    #6
  7. There is no state between page requests - every page gets recreated from
    scratch again.

    Remove the Not IsPostBack check and you should be fine.
    Attached is the code I was testing on my side

    I used a panel to add the controls in the aspx
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:panel Runat="server" ID="pnlTest"></asp:panel>
    </form>
    </body>
    Code-behind :
    Protected pnlTest As Panel

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    Dim z As Integer

    For z = 0 To 10

    Dim ddl As System.Web.UI.WebControls.DropDownList = New
    System.Web.UI.WebControls.DropDownList

    ddl.Style("width") = "150"

    ddl.ID = z.ToString

    ddl.DataSource = GetData()

    ddl.DataTextField = "cust_name"

    ddl.DataValueField = "cust_id"

    ddl.DataBind()

    AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged

    ddl.AutoPostBack = True

    pnlTest.Controls.Add(ddl)

    Next

    End Sub

    Public Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e
    As System.EventArgs)

    Dim x As String = "This function is never called!!!!!!!"

    End Sub

    Private Function GetData() As DataSet

    'Some data --replace with your own

    Dim ds As New DataSet

    Dim dt As New DataTable

    dt.Columns.Add(New DataColumn("cust_name"))

    dt.Columns.Add(New DataColumn("cust_id"))

    Dim dtRow As DataRow = dt.NewRow

    dtRow(0) = "1"

    dtRow(1) = "1"

    dt.Rows.Add(dtRow)

    dtRow = dt.NewRow

    dtRow(0) = "2"

    dtRow(1) = "2"

    dt.Rows.Add(dtRow)

    ds.Tables.Add(dt)

    Return ds

    End Function


    Hope this helps!
    --
    Swanand Mokashi
    Microsoft Certified Solution Developer (.NET) - Early Achiever
    Microsoft Certified Application Developer (.NET)

    http://www.dotnetgenerics.com/
    DotNetGenerics.com -- anything and everything about Microsoft .NET
    technology ...

    http://www.swanandmokashi.com/
    http://www.swanandmokashi.com/HomePage/WebServices/
    Home of the Stock Quotes, Quote of the day and Horoscope web services


    <> wrote in message
    news:...
    > It is having post back.
    > Also I have some code in the page load event, this code is executed
    > only if
    > If Not IsPostBack Then
    >
     
    Swanand Mokashi, Apr 27, 2006
    #7
  8. Guest

    So this is my problem.
    I didn't create drop down on Page Load event.
    Thanks for your help.
     
    , Apr 27, 2006
    #8
  9. Does that mean that fixed your problem ?

    --
    Swanand Mokashi
    Microsoft Certified Solution Developer (.NET) - Early Achiever
    Microsoft Certified Application Developer (.NET)

    http://www.dotnetgenerics.com/
    DotNetGenerics.com -- anything and everything about Microsoft .NET
    technology ...

    http://www.swanandmokashi.com/
    http://www.swanandmokashi.com/HomePage/WebServices/
    Home of the Stock Quotes, Quote of the day and Horoscope web services


    <> wrote in message
    news:...
    > So this is my problem.
    > I didn't create drop down on Page Load event.
    > Thanks for your help.
    >
     
    Swanand Mokashi, Apr 27, 2006
    #9
  10. Guest

    Yes, thanks
     
    , Apr 27, 2006
    #10
  11. adavir

    Joined:
    Sep 24, 2008
    Messages:
    3
    EnableViewState Problem

    There is an article that explains this located here.
     
    adavir, Sep 24, 2008
    #11
  12. adavir

    Joined:
    Sep 24, 2008
    Messages:
    3
    There is an article that explains this located here.
     
    adavir, Sep 24, 2008
    #12
    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:
    730
    Saravana
    Jul 4, 2003
  2. Robert Hanson
    Replies:
    0
    Views:
    540
    Robert Hanson
    Jul 10, 2003
  3. Robert Hanson
    Replies:
    0
    Views:
    469
    Robert Hanson
    Jul 10, 2003
  4. Pepito Grillo

    SelectedIndexChanged event in DropDownList

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

Share This Page