[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

Dropdown selectchange problem

I have the following code in a page_load event which fills a dropdownlist named ddl1 and and some other controls .

suppose ddl1 is filled with 1,2,3,4 and values to A,B,C,D

If value retruned from db is 2 then ddl1's selected text will be 2.

When I change ddl1 item to 1 I get A in the txtName textbox as expected. But when I select 2 again I don't get txtName populated with B(nothing get populated), but for all others items I get the corresponding value in the txtName textbox.

could anyone pls. help me to solve the problem?

  Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
            If Not Page.IsPostBack Then
            ddl1.DataSource = General.GetDr(sqlText2)
            ddl1.DataTextField = "ID"
            ddl1.DataValueField = "ProfileInfo"
            ddl1.DataBind()

            txtJobCardNo.Text = Request.QueryString("JobCardNo")
 
            Try
                Dim rdr As OleDbDataReader = cmd.ExecuteReader
          If rdr.Read Then
                    txtDrawingFile = rdr.Item("DrawingFile")
                    imgfile.ImageUrl = txtDrawingFile
                    ddl1.SelectedItem.Text = rdr.Item("D1")
                    txtName.Text = rdr.Item("Name1")
              ...

selectIndexChanged event;

 Private Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl1.SelectedIndexChanged
         If ddl1.SelectedIndex <> 0 Then
            txtName.Text = dd1.selectedvalue
         Else
            txtName.Text = ""
         End If
    End Sub

ayha
0
ayha1999
Asked:
ayha1999
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
YZlatCommented:
Private Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl1.SelectedIndexChanged
         If ddl1.SelectedIndex <> 0 Then
            txtName.Text = dd1.selectedItem.value
         Else
            txtName.Text = ""
         End If
    End Sub
0
 
laotzi2000Commented:
The problem is with:
ddl1.SelectedItem.Text = rdr.Item("D1")
Should be
ddl1.SelectedValue = rdr.Item("D1")
0
 
laotzi2000Commented:
Or
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByText(rdr.Item("D1"))
If D1 contains the text

If D1 contains the value, see my first post.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RejojohnyCommented:
>>But when I select 2 again I don't get txtName populated with B(nothing get populated),
Could you view source and check if all the items are indeed present within the "select'" tags

>> If ddl1.SelectedIndex <> 0 Then
u do not really need this statement within selectedindexchanged if u never expect ddl to be empty .. this event will get executed only after the first time the page is loaded and this event will run if an only if there are some items within ddl and the user has selected another value ...


Also could u also give the real values for 1,2,3,4 and A,B,C and D .. not examples .. seems like something might be worng with the data ...
0
 
ayha1999Author Commented:
Hi,

I tried the following;

                Dim ddl1 As ListItem = ddl1.Items.FindByValue(CStr(rdr2.Item("D1")))
                If Not ddl1 Is Nothing Then
                    ddl1.Selected = True
                End If
                Name.Text = rdr2.Item("Name1")
                Name2.Text = rdr2.Item("Nam2")

What happend is;

I have two dropdowns here, both populating from the same table.

suppose when page loading time I get items 1,2,3 and values a,b,c and 1 is selected item. Here when I change to 2 I get a in the textbox, when I change again to 1 I get blank textbox but if change items in the second dropdown I get the first dropdown's textbox populated with the correct value and vice versa.  Only when changin to default item I don't get textbox populated other this way(changing the second dropdown)

dropdown change event;

 Private Sub dd1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd1.SelectedIndexChanged
        Dim str1() As String
        If dd1.SelectedIndex <> 0 Then
            str1 = dd1.SelectedItem.Value.Split(":"c)
            Name.Text = str1(0)
            name2.Text = str1(1)
        Else
            Name.Text = ""
            name2.Text = ""
        End If
    End Sub

I have another dropwondw and textboxes with the same information.What is the casue of the problem? I have selectIndex 0 also there that's why I useed If <> 0.

ayha
0
 
RejojohnyCommented:
now i am bit confused .. u have changed the code completely .. what happened to ur old code .. what is a split been used ..

>> I have selectIndex 0 also there that's why I useed If <> 0.
when and where .. u always have some item with the ddl .. right?

>> Here when I change to 2 I get a in the textbox
This is worng .. right? ui r supposed to get "b"

could u please post the actual values .. instead of examples .. u say u have 2 dropdowns and u have posted the selectedindexchanged for first one .. what does the second ddl have ..
0
 
ayha1999Author Commented:
Hi Rejojohny,

My sql string is;

            Dim sqlText As String = "Select ID, FirstName + ':' + LastName as ProfileInfo from Profiles"

In the selectIndexChange event I split with ':' and retrive values.

Here I am binding on page_load

            ddl1.DataSource = General.GetDr(sqlText)
            ddl1.DataTextField = "ID"
            ddl1.DataValueField = "ProfileInfo"
            ddl1.DataBind()

and setting default value:

                Dim ddl1 As ListItem = ddl1.Items.FindByValue(CStr(rdr2.Item("D1")))
                If Not ddl1 Is Nothing Then
                    ddl1.Selected = True
                End If
                Name.Text = rdr2.Item("FirstName")
                Name2.Text = rdr2.Item("LastName")

I have two dropdown and binding same data.

Suppose I get item 1 and value A in dd1 and 5 and E and ddl2. When I change item to 2 in dd1 I get B populated in the textbox, when I cahnge item to 1 again in dd1 I dont get tebox populated with value(blank). Here if I change item in ddl2 I get textboxes for both ddl1 and ddl2 popoulated. that means All items and values are there in the dd1 but when I change to default item I dont get textbox populated with value, it needs to change item in the  other dropdown.

I tried removing "selectindex <> 0 " from selectchange event but no luck.

>>when and where .. u always have some item with the ddl .. right?<<
----- always values there: I removed it.-----

>>This is worng .. right? ui r supposed to get "b"<<
----- I get b : again by mistake. ----

ayha
0
 
YZlatCommented:
when you bind your ddl1, try adding the following code:



_______________________
 If Not Page.IsPostBack Then
            ddl1.DataSource = General.GetDr(sqlText2)
            ddl1.DataTextField = "ID"
            ddl1.DataValueField = "ProfileInfo"
            ddl1.DataBind()

           ddl1.Items.Insert(0, New ListItem(" "))
            ddl1.SelectedIndex = 0

0
 
ayha1999Author Commented:
Hi YZlat,

Thank u for your response.

Here I am binding this ddl in an update form, i.e. I have alway a default value will be there in the dropdown. I want to show user the default ID in the ddl. your solution may work but how can I keep default value in it and achieve my goal.

I use the following to keep the default value in the ddl whcih sets default value.

Dim ddl1 As ListItem = ddl1.Items.FindByValue(CStr(rdr2.Item("D1")))
                If Not ddl1 Is Nothing Then
                    ddl1.Selected = True
                End If
                Name.Text = rdr2.Item("FirstName")
                Name2.Text = rdr2.Item("LastName")

Is there any solution to this problem?

ayha
0
 
YZlatCommented:
what default value are you talking about?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now