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

SelectedValue not changing when loading via JavaScript method

3 Answers 105 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Billy
Top achievements
Rank 2
Billy asked on 16 Jul 2010, 08:17 PM
Hello,

I have a RadComboBox using EnableLoadOnDemand="true" and I do not want the user to be able to type custom text into this combo box. Looking around I came across this article. These changes work, however when the user clicks a button I need to retrieve the SelectedValue in the combo box. It appears that it does not know an item was selected as the SelectedValue property never changes. Is there a way to handle this?

I could change this to allow the user to type custom text into the combo box, however according to this article the "In load-on-demand scenario the user will be able to type anything into the input area and if no item matches - the input area will be reset to blank." statement does not see to be true because whatever they type in is staying in the box and not being cleared out if the the item is not in the list.

I can go either way, except neither seem to be fully working for me.

Thanks!

3 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 21 Jul 2010, 04:51 PM
Hi Billy Hasan,

I just checked the first setup and it worked on my side withe the latest version of Telerik.Web.UI.

Please compare my code (in the attached page) and let me know if I am missing something.

All the best,
Simon
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Billy
Top achievements
Rank 2
answered on 21 Jul 2010, 07:21 PM
Your code worked, however I need to have a default item selected in the list. Once I added the code for that your code no longer works. Sorry I did not mention that before, I thought it wasn't working without the default selected item code but apparently your's seems to work without it.

Here is my code:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="NewInnovations.Demographics.Web.WebForm1" %>
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        <telerik:RadComboBox ID="RadComboBox1" runat="server" OnClientDropDownOpening="onDropDownOpening"></telerik:RadComboBox>
    </div>
 
    <asp:Button ID="Button1" runat="server" Text="alabala" />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
 
    <script type="text/javascript">
        function onDropDownOpening(sender) {
            var attributes = sender.get_attributes();
            if (attributes.getAttribute("DefaultItem") == "true") {
                sender.requestItems("", false);
                attributes.setAttribute("DefaultItem", "false");
            } else if (sender.get_items().get_count() == 0) {
                sender.requestItems("", false);
            }
        }
    </script>
</body>
</html>
Imports Telerik.Web.UI
 
Public Class WebForm1
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.IsPostBack = False Then
            RadComboBox1.Attributes.Add("DefaultItem", "true")
            RadComboBox1.Items.Add(New RadComboBoxItem("item 3", "3"))
        End If
    End Sub
 
    Private Sub RadComboBox1_ItemsRequested(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs) Handles RadComboBox1.ItemsRequested
        RadComboBox1.Items.Clear()
        '
        RadComboBox1.Items.Add(New RadComboBoxItem("item 1", "1"))
        RadComboBox1.Items.Add(New RadComboBoxItem("item 2", "2"))
        RadComboBox1.Items.Add(New RadComboBoxItem("item 3", "3"))
        RadComboBox1.Items.Add(New RadComboBoxItem("item 4", "4"))
    End Sub
 
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = RadComboBox1.SelectedValue
    End Sub
End Class
0
Simon
Telerik team
answered on 23 Jul 2010, 05:23 PM
Hi Billy Hasan,

Thank you for clarifying this.

This behavior is normal when you do not have Load On Demand enabled and the RadComboBox is read-only. After postback, the RCB has only one Item - the default one as it is loaded from ViewState (LOD Items are not persisted across postbacks), so its Text and Value are the actual ones, so the selection made on the client is lost.

You can work around this if you disable the ViewState of the RCB, enable its AllowCustomText property and make the control read-only on the client:
<telerik:RadComboBox ID="RadComboBox1" runat="server" OnClientDropDownOpening="onDropDownOpening"
    EnableViewState="false" AllowCustomText="true" OnClientLoad="onLoad">
</telerik:RadComboBox>
function onLoad(sender) {
    sender.get_inputDomElement().readOnly = "readonly";
    sender.get_inputDomElement().parentNode.parentNode.className = "rcbReadOnly";
}

Then you have to add the Default Item on each postback and pre-select it only on the first page load:
Protected Sub Page_Load(sender As Object, e As EventArgs)
    RadComboBox1.Attributes.Add("DefaultItem", "true")
    RadComboBox1.Items.Add(New RadComboBoxItem("item 3", "3"))
 
    If Not Page.IsPostBack Then
        RadComboBox1.Items(0).Selected = True
    End If
End Sub

Please let me know if this setup is useful to you.

Kind regards,
Simon
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
ComboBox
Asked by
Billy
Top achievements
Rank 2
Answers by
Simon
Telerik team
Billy
Top achievements
Rank 2
Share this question
or