Solved

values from one dropdownlist depend of selected value from another dropdownlist

Posted on 2004-09-12
8
307 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEvens challenge 6 63
wordsWithout 49 79
Path of Workbook 3 45
How can i compile this github project?? 2 40
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now