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://ko.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,
Can you show me your code snippets? I'd like to see your ItemsRequested server side code and the client side event code that calls the LoadItems method.
As you are taking this approach I am wondering if you are doing soemthing with cascading WebDropDown providers, where one drop down provider serves as a filter for another WebDropDownProvider??
If you can offer some clarification on this I can better understand your objectives.
Hi SunilMeta,
I pulled together a demo that shows the cascading dropdown approach. Let me know if you need any additional assistance.
Hi Troy,
I have implemented cascading webdropdown functionality in a similar way as descibed in the demo. I sent a code-sample as well to you. I am looking for a solution to my problem where I am not able to set the current display item of a webdropdown from code-behind vb file.
I am revisiting this issue with a revised sample and will follow-up with you on this issue on or before Monday.
I would like to take a moment to clarify with you whether the issue we are addressing here is that:
(1) for WebDropDown1 --> when setting the dropdown value in code (such as: Dim index = ItemDropDown.Items.FindItemByValue("2").Index)......, the dropdown is not loading with the text for that index in the dropdown text editor
Or is the issue that,
(2) for WebDropDown2 --> for the index that WebDropDown1 is set to, the WebDropDown2_ItemsRequested event does not fire, resulting in the value list being empty
If the issue is the first (1), I would like to verify that you are using the most recent service release for your NetAdvantage product. If you are using NetAdvantage 2012 vol 1 the build you should be using is 20121.2048. If you are using NetAdvantage 2011 vol 2, the build you should be using is 20112.2159.
However, if the issue is the second (2), I can confirm that I am finding that when item index is set in code, for that item that ("WebDropDown1") is set to, the ItemsRequested event is not firing, which does make sense. To address this I set the WebDropDown Load event:
Protected Sub WebDropDown2_Load(sender As Object, e As System.EventArgs) Handles WebDropDown2.Load
Dim selectedValue As Integer = Integer.Parse(WebDropDown1.SelectedValue.ToString())
Dim persons As List(Of Person) = DataProvider()
Dim filtered As List(Of Person) = persons.Where(Function(p) p.ID = selectedValue).ToList()
Me.WebDropDown2.DataSource = filtered
Me.WebDropDown2.TextField = "Fname"
End Sub
Attached to this thread is the my full sample. Please note that the ig_res folder has been omitted due to file size constraints. For proper styling 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.
Please let me know if you need any additional assistance regarding this matter.
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.
**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.
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.