This is a migrated thread and some comments may be shown as answers.

SelectedValue not changing when selecting item

9 Answers 909 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Lynda Golomb
Top achievements
Rank 1
Lynda Golomb asked on 14 Dec 2009, 02:26 PM
Hi,

I have two related comboboxes (copied the code from
http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultvb.aspx).
The problem is that I have another button on the page that invokes a 'save' function where I want to extract the selectedvalue to save in the DB.
In this function, I get RadComboBoxGeneralField.SelectedValue=-1 (the first item in the combobox), when actually on the page another item is selected!!
When I get RadComboBoxGeneralField.Text it shows the item text (as in the page for example "Mathematics"),
but RadComboBoxGeneralField.SelectedItem.Text shows the first item in the list!
It looks as if the control just doesn't do the select.

Code attached below:

The first shows a list of Broad Fields:

 

 

telerik:RadComboBox ID="RadComboBoxBroadField" runat="server" OnClientSelectedIndexChanged="fnLoadGeneralField" telerik:RadComboBox>

 

The second shows a list of General Fields:

 

<telerik:RadComboBox ID="RadComboBoxGeneralField" runat="server" OnItemsRequested="RadComboBoxGeneralField_ItemsRequested" OnClientItemsRequested="ItemsLoaded" OnClientSelectedIndexChanged="fnLoadSpecificField" ></telerik:RadComboBox>

JavaScript:

 

<

 

script type="text/javascript">

 

 

 

 

 

//global variables for the general and specific comboboxes

 

 

var generalfieldCombo;

 

 

var specificfieldCombo;

 

 

function pageLoad()

 

{

 

// initialize the global variables

 

 

 

// in this event all client objects are already created and initialized

 

 

 

generalfieldCombo = $find("<%= RadComboBoxGeneralField.ClientID %>");

 

specificfieldCombo = $find(

"<%= RadComboBoxSpecificField.ClientID %>");

 

}

 

 

 

 

function

 

fnLoadGeneralField(combo, eventArqs)

 

{

 

    var item = eventArqs.get_item();

 

    generalfieldCombo.clearItems();

    generalfieldCombo.set_text(

"Loading...");

 

 

 

 

 

 

 

 

    // if a broad field is selected

 

 

 

    if (item.get_index() > 0)

 

    {

 

    // this will fire the ItemsRequested event of the

 

 

 

    // general field combobox passing the broadfieldID as a parameter

 

 

 

    generalfieldCombo.requestItems(item.get_value(), false);

 

}

 

else

 

 

 

{

 

 

    // the <Choose> item was chosen

 

 

 

    generalfieldCombo.set_text("N/A");

 

    generalfieldCombo.clearItems();

 

}

}

 

function ItemsLoaded(combo, eventArqs)

 

{

 

if (combo.get_items().get_count() > 0)

 

{

 

    // pre-select the first item

 

 

 

    combo.set_text(combo.get_items().getItem(0).get_text());

 

    combo.get_items().getItem(0).highlight();

}

 

else

 

 

 

    combo.set_text("N/A");

 

}

 

</script>

 

 

 

Server Code (VB.NET)
The BroadField is loaded through the code on page load using DataBind().

 

Protected

 

Sub RadComboBoxGeneralField_ItemsRequested(ByVal o As System.Object, ByVal e As Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs) Handles RadComboBoxGeneralField.ItemsRequested

 

        LoadGeneralField(

CInt(e.Text))

 

 

End Sub

 

 

 

 

 

Public

 

Sub LoadGeneralField(ByVal intBroadFieldId As Integer)

 

 

Dim dsDB As New DataSet

 

' Filling the dataset (code ommitted)

 

 

 

If dsDB.Tables(0).Rows.Count > 0 Then

 

    RadComboBoxGeneralField.DataTextField =

"GF_Desc"

 

 

 

    RadComboBoxGeneralField.DataValueField = "GF_Id"

 

 

 

    RadComboBoxGeneralField.DataSource = dsDB.Tables(0)

 

    RadComboBoxGeneralField.DataBind()

 

End If

 

 

 

 

 

 

End Sub

 

 

 

 

9 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 16 Dec 2009, 10:26 AM
Hi Lynda Immerman,

Items loaded on demand are lost on postback; in this case only the Text and SelectedValue properties persist, the SelectedIndex property returns -1 and SelectedItem is null.

Can you please verify whether you are binding the second RadComboBox on the server-side as well as in the ItemsRequested event handler?

All the best,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Lynda Golomb
Top achievements
Rank 1
answered on 16 Dec 2009, 02:32 PM
Yes, I confirm that this is the case, the combobox is loaded on page_load through server side and then by items requested.
I get -1 in the SelectedValue, the only property I have is the text and I would not want to rely on that to update the DB.
Is there any way to get at least the SelectedValue?
0
Simon
Telerik team
answered on 19 Dec 2009, 09:12 AM
Hello Lynda Immerman,

Mixing the two types of Items - server-side and Load On Demand - could result in a similar issue.

Still I am not sure whether your case is exactly this one. Could you provide me with all of the code related to the RadComboBox exhibiting the described issue?

Sincerely yours,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Lynda Golomb
Top achievements
Rank 1
answered on 22 Dec 2009, 06:41 AM
Thanks.
I have a function

LoadBroadField which binds the control through a filled dataset:

 

RadComboBoxBroadField.DataSource = dsBroadField.Tables(0)

RadComboBoxBroadField.DataTextField =

"BF_Desc"

 

 

 

 

RadComboBoxBroadField.DataValueField =

"BF_Id"

 

 

 

 

RadComboBoxBroadField.DataBind()



And LoadGeneralField(intBroadFieldId) which binds the second drop down in the same way, but the dataset is filled according to the

 

intBroadFieldId

 

RadComboBoxGeneralField.DataTextField =

"GF_Desc"

 

RadComboBoxGeneralField.DataValueField =

"GF_Id"

 

RadComboBoxGeneralField.DataSource = dsDB.Tables(0)

RadComboBoxGeneralField.DataBind()



In Page_Load I call LoadBroadField. If according to the DB it should have a value selected I call LoadGeneralField with the selected id.
In the RadComboBoxGeneralField_ItemsRequested I call LoadGeneralField(CInt(e.Text))

Apart from this there is js according to the example in the first list.

Thanks for the help!

0
Simon
Telerik team
answered on 23 Dec 2009, 01:07 PM
Hi Lynda Immerman,

Depending on where you bind the RadComboBoxGeneralField RadComboBox you can try resolving the issue by clearing the Items in the same RCB on each postback (except for the first page load):

RadComboBoxGeneralField.Items.Clear()

In other words, if you are binding the RCB in Page_Load with some Items you could put the code above right before binding the RCB.

Please let me know whether this helps.

Best wishes,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Lynda Golomb
Top achievements
Rank 1
answered on 23 Dec 2009, 01:19 PM
Nope, I tried this already... (I tried it again now just to make sure).
Such a pity, the drop downs work so well, only when it comes to saving the values the problem starts :-(
0
Simon
Telerik team
answered on 23 Dec 2009, 01:29 PM
Hi Lynda Immerman,

I am sorry I could not help you resolve this issue. 

Your use-case is specific and the workaround with clearing Items on each page load should work.

Now I suggest you open a support ticket and send us a project showing the issue there and describe how to reproduce it. We will investigate the case and will provide more specific assistance and information.

All the best,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Phil
Top achievements
Rank 2
answered on 27 Dec 2009, 03:50 AM
I use a combination of clear and an assignment as follows:
protected void RadComboBox2_Initialize() 
    RadComboBox2.Items.Clear(); 
    RadComboBox2.Text = ""
 

Phil
PS.  I think you also need a
   EnableLoadOnDemand="true"
in you combo-box.
0
Lynda Golomb
Top achievements
Rank 1
answered on 27 Dec 2009, 07:23 AM
This did the trick!!! Excellent, thank you very very much!
Tags
ComboBox
Asked by
Lynda Golomb
Top achievements
Rank 1
Answers by
Simon
Telerik team
Lynda Golomb
Top achievements
Rank 1
Phil
Top achievements
Rank 2
Share this question
or