Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

values from one dropdownlist depend of selected value from another dropdownlist

Posted on 2004-09-12
8
340 Views
Last Modified: 2011-09-20
hi all,
I work in vb.net and I am begener.
I have a problem with event of DropDownList. I made two dropdownlists: ddlMG & ddlOpstine. The values both of them are loaded from database (tables MG and Opstine). I should make that values of ddlOpstine depends of selected value on ddlMG. For example: if I select value '050' (it's a network group) from ddlMG, in ddlOpstine should be loaded subset of values (cities) that belong to '050' network group, if I select value '051' from ddlMG, in ddlOpstine should be loaded subset of values that belong to '051' network group.
I tried with ddlMG_SelectedIndexChanged event but it doesn't fire this event.
Can somebody help me to resolve my problem?

Thanks

dragy
0
Comment
Question by:dragy
  • 4
  • 4
8 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12041233
Well there must be some problem with your code because SelectedIndexChanged event is supposed to fire whenever the selection changes.

On this event you'll need to re-populate your other combobx after filtering say a dataTable which contains data...

0
 
LVL 1

Author Comment

by:dragy
ID: 12041496
ok,
it works when I set AutoPostBack on true, but whenever I change the value on master ddl, old value (first value from ddl) come back after reloading. What I should to do to keep new value after refresh.
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12041588
Set the EnableViewState property of the ddl combo to True
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:dragy
ID: 12042077
EnableViewState was set to true, it doesn't work. I would be glad arif if you can look at my code, it's not very large.

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

Private Sub napuni_combo_opstina()
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.oledb.4.0;Data Source=D:\988\CENTAR.mdb")
        conn.Open()

        Dim sql As StringBuilder = New StringBuilder
        sql.Append("select id, naziv, mg from opstina")
        sql.Append(" where mg = '")
        sql.Append(ddlMG.SelectedValue)
        sql.Append("'")

        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql.ToString, conn)
        conn.Close()
        Dim ds As DataSet = New DataSet
        da.Fill(ds, "opstina")

        ddlOpstina.DataSource = ds.Tables(0)
        ddlOpstina.DataTextField = ds.Tables(0).Columns(1).ColumnName.ToString()
        ddlOpstina.DataValueField = ds.Tables(0).Columns(0).ColumnName.ToString()
        ddlOpstina.DataBind()

    End Sub

Private Sub ddlMG_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMG.SelectedIndexChanged
        napuni_combo_opstina()
 End Sub

Private Sub napuni_combo_ddlMG is like Private Sub napuni_combo_opstina(), I didn't write it here.
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 125 total points
ID: 12042223
First thing on Page Load call napuni_combo_MG() only in case page is loaded for the first time so your code should be...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       If Not IsPostBack Then
            napuni_combo_MG()
        End If
End Sub

However this is not what you asked, your code looks fine, the problem that the ddlMG comboBox does not retain its value after post back still remains. It has nothing to do with your code its just property setting. Ideally EnableViewState property of the combobox ddlMG and also the PAGE must be True, also the page's enableSessionManagement should be True.

Also since you were not checking for PostBack in your  Page_Load you must have used some technique in the function napuni_combo_MG() to avoid multiple entries in the combobox you'd be clearing the combobox i.e. ddlMG.Items.Clear etc. Now since you are checking at the page_Load for IsPostBack do not do any Items.Clear, may be the value was being lost because of that.
0
 
LVL 1

Author Comment

by:dragy
ID: 12042476
that's it arif, it works fine.
Thanks

I have one more question for you about this problem.
I added third ddl named ddlMjesto. It should change its values depending on selected value of ddlOpstina. What happens. When I change ddlMG, ddlOpstina load right values and first value is selected value. ddlMjesto should load values for ddlOpstina's first value, but it didn't. Values in ddlMjesto stay same lake before. I set AutoPostBack property of ddlOpstina on true and did same thing like with ddlMG.

Do you know what I should to do.
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12042542
When you are loading ddlOpstina on the SelectedIndexChange of ddlMG, store the first value of ddlOpstina in a variable and based on this value populate the ddlMjesto within the same event.

See ddlMjesto will not be populated now because the SelectedIndexChange of ddlOpstina has not fired however we know that when we are filling ddlOpstina its first value will be shown as selected so we can safely populate ddlMjesto along with ddlOpstina, However the same process as for dllMG will repeat fro ddlOpstina also just as you have done.

0
 
LVL 1

Author Comment

by:dragy
ID: 12042870
It works,

thanks

dragy
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question