SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    assigning value to textbox on indexchanged event

    i have a dropdwn1 with autopostback=true and populated with a field frm datatbase
    on its indexchanged event i have populated another dropdwn2 with data from database as per the value in dropdown1
    and even assigned textbox1.text=dropdown1.text


    for dropdwn2 with autopostback=true i want to assign assigned textbox2.text=dropdown2.text
    but i am not getting it

    how do i assign the value from dropdwn2 to the texttboxon indexchanged event.
    if i am getting for one dropdwn then whts the error with second

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Post some of you code so we can take a look at where it is going wrong

  3. #3
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coding on page load
    ===================================

    Dim da As SqlDataAdapter
    Dim ds As DataSet

    da = New SqlDataAdapter("SELECT DISTINCT(country) FROM Cmas where country<>''", myconnection)
    ds = New DataSet
    da.Fill(ds, "cmas")

    DropDownList1.DataSource = ds
    DropDownList1.DataTextField = = "country"
    DropDownList1.DataValueField = = "country"
    DropDownList1.DataBind()


    da = New SqlDataAdapter("SELECT DISTINCT(state) FROM Cmas where country='" & DropDownList1.SelectedItem.Text & "'", myconnection)
    ds = New DataSet
    da.Fill(ds, "cmas")

    DropDownList2.DataSource = ds

    DropDownList2.DataTextField = = "state"
    DropDownList2.DataValueField = = "state"
    DropDownList2.DataBind()



    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    da = New SqlDataAdapter("SELECT DISTINCT(state) FROM Cmas where country='" & DropDownList1.SelectedItem.Text & "'", myconnection)

    ds = New DataSet
    da.Fill(ds, "cmas")

    DropDownList2.DataSource = ds

    DropDownList2.DataTextField = = "state"
    DropDownList2.DataValueField = = "state"
    DropDownList2.DataBind()
    txtcountry.Value = DropDownList1.Text

    End Sub


    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged

    txtstate.Value = DropDownList2.Text

    End Sub

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    First thing. You should use only 1 = sign instead of 2 in the code:

    EG:
    DropDownList2.DataTextField = "state"
    DropDownList2.DataValueField = "state"

    Try doing this:

    txtcountry.Value = DropDownList1.SelectedItem.Text
    txtstate.Value = DropDownList2.SelectedItem.Text

    Let us know if you are still having problems after that

  5. #5
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    == was by mistake , in my coding its single

    for txtcountry i am getting the proper data on country chnage
    only prblm with state

    one more strange thing i noticed is that
    with my above code i do get the name of the state in textbox on dropdownlist2 change but with strange condition

    the condition is as follows

    if the country has more than one state , then when dropdownlist2 indexchanged i get the proper name in textbox

    but if the country has only one state , then when dropdownlist2 indexchanged the value of textbox doesnot change
    it retains the earlier value

  6. #6
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    hmmm. Hard 2 debug without seing it in action.

    Make sure you bind your main list in a if (!Page.IsPostBack) condition and also make sure u use SelectedItem.Text.

    Other than that, im not sure wat the problem is. Maybe some1 else can see something im missing. Coz its hard just to look at code and figure out wat is happening

  7. #7
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any other way to trap whts wrong in it as this is th eonly code thtas written for populating and indexchange

  8. #8
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi thanks it got solved by using
    SelectedItem.Text in all the places
    one more thing need to ask is
    by using datareader i want the value from database to appear in the dropdownlist whicg is already populated with some data

    eg:

    like i have dropdownlist1 populated with country USA, CHINA, RUSSIA, JAPAN, AUSTRIA

    for a particular record suppose RUSSIA is saved againast country in the database , when i want to view that record i want RUSSIA to appear selected, ie first.

  9. #9
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    dropdownlist1.SelectedIndex = dropdownlist1.Items.IndexOf(dropdownlist1.Items.FindByText("RUSSIA"))

    That should do what you want

  10. #10
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i had used the same
    dropdownlist1.SelectedIndex = dropdownlist1.Items.IndexOf(dropdownlist1.Items.FindByText(drp_READER("Country")))

    but i always get the first value

  11. #11
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    drp_READER("Country").ToString()

    If that still doesnt work Response.Write(dropdownlist1.Items.IndexOf(dropdownlist1.Items.FindByText(drp_READER("Country").ToString)))

    Just to make sure its finding it correctly. Best is to actually set a break point tho and step through the code, then you will see exactally wat is happening and wen


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •