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.
Hi SunilMehta,
The WebDropDown is an ajax control. It will send an async call.
For this issue I recommend updating to the latest service release for NetAdvantage 2012 vol 1. This will update you to 20121.2048. Let me know if after updating you are having the same issue.
If so, the next recommended step will be to update my sample such that it reproduces the issue and send back my modified sample. I will then be able to research this matter further for you.
Thank you SunilMehta. I will look for your update.
Hi Troy,
I tried the sample provided by you and I can see the same issue with that sample as well. I replaced the below statement in the 'WebDropDown2_ItemsRequested" procedure
Me.WebDropDown2.TextField = "Fname"
with
Me.WebDropDown2.SelectedItemIndex = 1
and then tried selecting "Item2" in webdropdown1. Below are my findings after doing this:
1. The texteditor of webdropdown was still empty. I was expecting "Ivan" to be in the texteditor as the currentvalue.
2. When I opened webdropdown2, the second item "Ivan" shows highlighted in blue color. It gives a kind of impression that the item has been selected. But why "Ivan" is not getting displayed in the texteditor.
I am attaching the updated sample below. Please check the issue and let me know the solution.
To add more into it, try adding
in the "WebDropDown2_Load" procedure. So, at page load, webdropdown2 correctly shows text in the texteditor of webdropdown2. But after changing the selection in webdropdown1, when control goes to ItemsRequested procedure, then the item does not showup in the texteditor. I am not able to understand the reason why this is happening.
Troy,
I appreciate your patience on understanding the issue but do reply with a solution asap. This is bit urgent.
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.
Hi Sunil,
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
I have created a private case for you, for this issue. Please see the private support case for updates regarding this issue.
Thanks!
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.
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.