Is there a way to set the current display text and value of a webdropdown in the server-side(code behind vb file)?
Thanks,
Sunil Mehta.
Hello Sunil,
In order to assign the selected item for your WebDropDown from the code behind file, you must set the SelectedItemIndex property of your WebDropDown. One way to do this is to find the item from the item list using FindItemsByValue.
For example:
//For this example, I am using a simple object for demonstration. Obviously your Model would be used instead.
Public Class item
Public Property value() As Integer Get Return m_value End Get Set m_value = Value End Set End Property Private m_value As Integer Public Property text() As String Get Return m_text End Get Set m_text = Value End Set End Property Private m_text As String Public Sub New(val As Integer, txt As String) value = val text = txt End Sub End Class
//Here we bind the sample items to the list
ItemDropDown.DataSource = New BindingList(Of item)() From { _ New item(1, "item1"), _ New item(2, "item2"), _ New item(1, "item3") _ } ItemDropDown.ValueField = "value" ItemDropDown.TextField = "text" ItemDropDown.DataBind()
//Lastly we find the item and then select it by index.
Dim index = ItemDropDown.Items.FindItemByValue("2").Index
ItemDropDown.SelectedItemIndex = index
Alternatively, you can also loop through the items list in order to compare against the item text or the item text and value simultaneously.
Sincerely,JonInfragistics, Inc.<http://es.infragistics.com/support/get-help.aspx>
Hi Jon,
Once I know the index, I would like to see the text of that index in the webdropdown. But I am not able to see that. How to accomplish this in the code-behind. After trying the way you described above, the item which I want to display in the webdropdown is not coming. But when I open the dropdown container, that item shows that it has been selected. I want to set that selected item to be the current display item in the webdropdown.
Troy,
I appreciate your patience on understanding the issue but do reply with a solution asap. This is bit urgent.
Hi SunilMehta,
I do appreciate the urgency. I am pursuing information and working with our engineers to address the recommended approach for your objective. I am doing some further testing based on info from our engineers and will have an update for you on Thursday.
Thanks.
I can confirm for you that when SelectedItemIndex (or SelectedValue) is set in code-behind, and its an ASYNC request (e.g., calling LoadItems from another dropdown), the text doesn't get updated. This is actually expected behavior.
The reason it is expected behavior is because load items only updates the the items list, not the whole control. Certainly not the input. The text can be manually updated after items are loaded and the state of the control is updated for the selected index or value.
SunilMehta, let me know if you need any additional assistance regarding this matter.
Hi Troy,
I did not have any intention of justifying whether SelectedItemIndex is showing expected behaviour or not. I just told you my problem and what I was trying to do. If this is expected behaviour, then please suggest me some other way to populate the INPUT of webdropdown from newly loaded items from server-side.
Hi Sunil,
I have created a private case for you, for this issue. Please see the private support case for updates regarding this issue.
Thanks!
I have researched this issue and found that by handling the client-side ItemsRequested event for the child dropdown, I am able to set the selected item index and get the selected item displayed into the text editor of the child dropdown. This is how I approached this:
function child_ItemsRequested(sender, eventArgs)
{
var selectedIndex = sender.get_selectedItemIndex();
if (selectedIndex < 0)
selectedIndex = 0;
sender.set_currentValue("", true);
sender.set_selectedItemIndex(-1);
sender.set_activeItemIndex(-1);
sender.selectItemByIndex(selectedIndex, true, true);
}
function parent_SelectionChanged(sender, eventArgs)
var childDropDown = $find('<%=child.ClientID%>');
childDropDown.loadItems(eventArgs.getNewSelection()[0].get_value(), false);
I have attached the revised sample for your reference.
Please let me know if you need any additional assistance regarding this matter.
**Sample is attached. Please note that the ig_res folder has been omitted due to file size constraints. For proper styling of the grid you will want to add the ig_res folder. To do so, simple open the web form designer (aspx) and accept the styles when prompted