DropDownList - SelectedIndexChanged

J

j_stus

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
 
S

Swanand Mokashi

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
 
J

j_stus

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
 
S

Swanand Mokashi

J

j_stus

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
 
S

Swanand Mokashi

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
 
S

Swanand Mokashi

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
 
J

j_stus

So this is my problem.
I didn't create drop down on Page Load event.
Thanks for your help.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top