Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DropDownList - SelectedIndexChanged

Reply
Thread Tools

DropDownList - SelectedIndexChanged

 
 
j_stus@hotmail.com
Guest
Posts: n/a
 
      04-27-2006
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

 
Reply With Quote
 
 
 
 
Swanand Mokashi
Guest
Posts: n/a
 
      04-27-2006
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
 
 
 
j_stus@hotmail.com
Guest
Posts: n/a
 
      04-27-2006
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

 
Reply With Quote
 
Swanand Mokashi
Guest
Posts: n/a
 
      04-27-2006
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
j_stus@hotmail.com
Guest
Posts: n/a
 
      04-27-2006
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

 
Reply With Quote
 
Swanand Mokashi
Guest
Posts: n/a
 
      04-27-2006
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
Swanand Mokashi
Guest
Posts: n/a
 
      04-27-2006
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">
<aspanel Runat="server" ID="pnlTest"></aspanel>
</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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
j_stus@hotmail.com
Guest
Posts: n/a
 
      04-27-2006
So this is my problem.
I didn't create drop down on Page Load event.
Thanks for your help.

 
Reply With Quote
 
Swanand Mokashi
Guest
Posts: n/a
 
      04-27-2006
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> So this is my problem.
> I didn't create drop down on Page Load event.
> Thanks for your help.
>



 
Reply With Quote
 
j_stus@hotmail.com
Guest
Posts: n/a
 
      04-27-2006
Yes, thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist selectedindexchanged event not firing.. please help. DesignerX ASP .Net 2 11-14-2003 03:25 PM
SelectedIndexChanged event in DropDownList Pepito Grillo ASP .Net 0 07-28-2003 08:36 AM
Re: Dropdownlist SelectedIndexChanged event problem Robert Hanson ASP .Net 0 07-10-2003 10:58 PM
Dropdownlist SelectedIndexChanged event problem Robert Hanson ASP .Net 0 07-10-2003 08:28 PM
Catching an SelectedIndexChanged event from a DropDownList inside a DataGrid Dominic ASP .Net 2 07-04-2003 04:43 AM



Advertisments