Ciao!
I have a page with three dropdown boxes, we can call them dropdown1, dropdown2 and dropdown3. When the value in dropdown1 is changed, the value in dropdown2 should change and son on with dropdown3. Using the valuechanged server event this works fine, when changes are made on the client side.
Now i want to set the initial value of dropdown1 (selected value) and I cant get that to work. The first dropdown contains available languages and I want to set this according to the language used in the current browser. I have the language to use, but i don't know how and where to set this initial value.
i tried to set the value in the dropdown1's DataBound event but when I do that it doesn't fire any other event in the hierarchy, like dropdown1_ValueChanged. i suppose that that event only fires when something is happening on the client side...
what is the best solution for setting the initial value in my case?
thanks for helping out!
/Henrik
Hello again!
yes, this is more or less what I did... i also used the DataBound event and the expression IsNotPostBack.
in that event, I could set the initial value for each control.
so for now, this is taken care off... perhaps not the best solution but it works...
thanks!
/H
If you change the value (or selection) programatically, no events will be fired, because it is assumed that the developer knows what he is doing - so he doesn't need the events to perform extra functionality. I suggest to refactor a bit so that the code in your event handlers goes to a different method, that you can call outside the event handler as well.
Normally the events are fired when an end user does some interaction on the control, so that the developer has control over these actions and can use the event handlers do do additional logic.
Hope it helps,
Angel
To describe it a bit more, i have attached some code in the bottom of this message. My first dropdown is called ddLanguage and it contains available languages.The function GetLanguageIDByISOLetters return the value that should be set for that dropdwon control.
When the language is changed, i should change the values in the other dropdowns (ddEC3Country, ddProfileType) as well.
The value is easily set, and it works. The problem is that it isn't updating the rest of the controls.
when i am changing something on the client side, all teh events are fired in order but no event is fired when i set the value in the DataBound function.
i hope this helps in understanding my problem better. sorry for the first description ;-)
'
If Not IsPostBack Then
ddLanguage.SelectedValue = Language.GetLanguageIDByISOLetters(System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName)
End If
End Sub
Protected Sub ddLanguages_ValueChanged(ByVal sender As Object, ByVal e As Infragistics.Web.UI.ListControls.DropDownValueChangedEventArgs) Handles ddLanguage.ValueChanged
Try
Dim EC3CountryID As Long = Language.GetEC3CountryIDByLanguageID(ddLanguage.SelectedValue)
Dim ProfileTypeID As Long = Language.GetProfileTypeIDByLanguageID(ddLanguage.SelectedValue)
Bind()
ddEC3Country.SelectedValue = EC3CountryID
ddProfileType.SelectedValue = ProfileTypeID
Catch ex As Exception
End Try
''
Protected Sub ddEC3Country_ValueChanged(ByVal sender As Object, ByVal e As Infragistics.Web.UI.ListControls.DropDownValueChangedEventArgs) Handles ddEC3Country.ValueChanged
Protected Sub ddProfileType_ValueChanged(ByVal sender As Object, ByVal e As Infragistics.Web.UI.ListControls.DropDownValueChangedEventArgs) Handles ddProfileType.ValueChanged
Private Sub Bind()
' Language
Dim siLanguage As Integer = ddLanguage.SelectedItemIndex
ddLanguage.DataBind()
ddLanguage.SelectedItemIndex = siLanguage
' EC3Country
Dim siEC3Country As Integer = ddEC3Country.SelectedItemIndex
ddEC3Country.DataBind()
ddEC3Country.SelectedItemIndex = siEC3Country
' ProfileType
Dim siProfileType As Integer = ddProfileType.SelectedItemIndex
ddProfileType.DataBind()
ddProfileType.SelectedItemIndex = siProfileType
Hi Henrik,
If you want to only change the text in the input, you can set the CurrentValue property. This won't make anything selected though. If you want to mark initially selected item - you have two choices:
1) just set the SelectedItemIndex to the index of the item in the collection (this will also update the current value)
2) set the SelectedValue to the item's "Value" property - this will also update the current input value with the item's Text. (you can map the Value property of the item to some unique ID field in the database).
Hope it helps. Please let me know if i can help with anything else,