Hai all,,

Currently I'm writing code in ASP.NET using VB language. I got an issue regarding dropdownlist. In my form, I have a treeview and a dropdownlist which are populated with data from database. When I click a node in treeview, the dropdownlist should be populated with values based on the selected node. This works fine at the first time page loading. But when I move to the other node of the treeview (which should trigger different dataset for the dropdownlist), the items inside the dropdownlist aren't changed. No matter how many times I change the selected node of the treeview, the dropdownlist always keeps the items from the first time it is populated. What should I do to make it populated dynamically,based on the treeview selected node?

I put the code to populate the dropdownlist inside the Treeview_SelectedNodeChanged method.
Here is my code:

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged

                            Dim dataReader As SqlDataReader
                            Dim cmdPro As SqlCommand
                            
                            Lbl_model.Text = Nothing
                            Select Case TreeView1.Nodes(i).Value.ToString
                                Case "OH 1521"
                                    Lbl_model.Text = "TblCat_OH1521"

                                Case "OH 1526"
                                    Lbl_model.Text = "TblCat_OH1526"

                                Case "OH 1626"
                                    Lbl_model.Text = "TblCat_OH1626"

                                Case "O500R 1830"
                                    Lbl_model.Text = "TblCat_OH1830"

                                Case "ENGINE 924"
                                    Lbl_model.Text = "TblCat_ENGINE924"

                            End Select

                            cmdPro = New SqlCommand("select distinct Page from " & Lbl_model.Text & " order by Page", con)
                            con.Open()
                            dataReader = cmdPro.ExecuteReader()
                            While dataReader.Read
                                part_Page.Items.Add(dataReader(0).ToString()) 
                            End While
                            con.Close()
'part_Page is the dropdownlist'
End Sub

I'm quite newbie in .NET. So any help would be very appreciated. Thanks a lot :)

Best regards,

Dyah

Pls check that is part_Page drop down visible and binded properly if you not found anything please give me a reply

Where are you getting your data from? because it seems you have a connectionstring and yet you want to get data from a label

cmdPro = New SqlCommand("select distinct Page from " & Lbl_model.Text & " order by Page", con)

I don't understand the above part of your code

Pls check that is part_Page drop down visible and binded properly if you not found anything please give me a reply

Hi shravan24,thanx for ur reply. Finally I found out solution for this. I just had to make the databinding script in other way. Seems like adding item into dropdownlist will make it permanently attached to the dropdownlist. So I bind the data this way:

con.Open()
dataReader = cmdPro.ExecuteReader()
part_Page.DataSource = dataReader
part_Page.DataTextField = "Page"
part_Page.DataBind()
con.Close()

And it works. :)

Where are you getting your data from? because it seems you have a connectionstring and yet you want to get data from a label

cmdPro = New SqlCommand("select distinct Page from " & Lbl_model.Text & " order by Page", con)

I don't understand the above part of your code

Hi chibex64,thanx for ur comment. Ok,my bad for not clearly explain my code. The label 'Lbl_model' contains the name of the table whose data I need. I make it this way because I set the table source to be dynamic (but with consistent structure,of course).In other words, I use parameter to specify the table name.

Supposedly, that's not the source of the problem. I also use this in other works,and it works fine. :)

try setting the autopostback property of the treeview to true in the page load event ie

treeviewid.autopostback=true

At page load event be sure of the follwing

if page.IsPostBack=false Then
// Fill the tree view

End if

At page load event be sure of the follwing

if page.IsPostBack=false Then
// Fill the tree view

End if

ok,guys,,thank you very much for all of your concerns..but this problem is already solved. I forgot to mark it as solved,my bad :p
And for omar,I already put that code,too :)

ok,guys,,thank you very much for all of your concerns..but this problem is already solved. I forgot to mark it as solved,my bad :p
And for omar,I already put that code,too :)

so what was the solution ?

I've written it down on my reply to shravan24 and chibex64 above^ :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.