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

Related Combo not sending SelectedValue to Server

1 Answer 72 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 29 Apr 2010, 03:26 PM
Hi,

I'm attempting to use related Combo box's here is my code
var cboUsers; 
        var chkNewUser; 
        function pageLoad() { 
            cboUsers = $telerik.findComboBox('<%= Page.FindControlRecursive("cboUsers").ClientID %>'); 
            chkNewUser = $telerik.$('#<%= Page.FindControlRecursive("chkNewUser").ClientID %>'); 
        } 
 
        function cboUser_Load(sender) { 
            debugger
            var cboCompany = $telerik.findComboBox('<%= Page.FindControlRecursive("cboCompanies").ClientID %>'); 
            var cboUser = $telerik.toComboBox(sender); 
            var selectedValue = cboCompany.get_value(); 
            cboUser.set_text("Loading..."); 
            cboUser.clearSelection(); 
 
            if (selectedValue != "") { 
                cboUser.requestItems(selectedValue, false); 
                 
            } 
        } 
 
        function cboUser_IndexChanged(sender, args) { 
            debugger
            var item = args.get_item(); 
             
            var cnHidVal = $telerik.$('#'+'<%= Page.FindControlRecursive("selectedUser").ClientID %>'); 
 
            cnHidVal.val(item.get_value()); 
 
        } 
 
        function LoadUsers(combo, eventArgs) { 
            var cboCompany = $telerik.toComboBox(combo); 
            var item = eventArgs.get_item(); 
            cboUsers = $telerik.findComboBox('<%= Page.FindControlRecursive("cboUsers").ClientID %>'); 
            cboUsers.set_text("Loading..."); 
            cboUsers.clearSelection(); 
            if (item.get_index() != -1) { 
                cboUsers.requestItems(item.get_value(), false); 
            } 
        } 
 
        function companyDropDownClosed(sender, args) { 
            var cboCompanybox = $telerik.toComboBox(sender); 
 
            var selectIndex = cboCompanybox.get_selectedIndex(); 
            cboUsers = $telerik.findComboBox('<%= Page.FindControlRecursive("cboUsers").ClientID %>'); 
 
            if (selectIndex == -1) { 
                cboUsers.clearSelection(); 
                cboUsers.clearItems(); 
            } 
 
        } 
 
        function cboUserItemsRequest(sender, args) { 
            var userCbo = $telerik.toComboBox(sender); 
            userCbo.trackChanges(); 
 
            if (userCbo.get_items().get_count() == 0) { 
                chkNewUser.trigger('click'); 
                userCbo.disable(); 
                userCbo.commitChanges(); 
                return
            } 
            else { 
 
                userCbo.enable(); 
                var comboItem = new Telerik.Web.UI.RadComboBoxItem(); 
                comboItem.set_text(""); 
                comboItem.set_value(""); 
                var items = userCbo.get_items() 
                items.insert(0, comboItem); 
                userCbo.commitChanges(); 
                if (chkNewUser.attr("checked")) 
                    chkNewUser.trigger('click'); 
            } 
 
        var cnHidVal = $telerik.$('#' + '<%= Page.FindControlRecursive("selectedUser").ClientID %>'); 
        if (cnHidVal.val() != "") { 
            for (i = 0; i < userCbo.get_items().get_count(); i++) { 
                var item = userCbo.get_items().getItem(i); 
                if (item.get_value() == cnHidVal.val()) { 
                    item.select(); 
                } 
 
            } 
        } 
Then on the server side i have an Custom Validator which does the following
protected void cboUsers_Validate(object sender, ServerValidateEventArgs args) 
        {  
            var newUserchecked = (System.Web.UI.WebControls.CheckBox)Page.FindControlRecursive("chkNewUser"); 
            RadComboBox cboUsers = (RadComboBox)Page.FindControlRecursive("cboUsers"); 
            args.IsValid = (newUserchecked.Checked) || (cboUsers.SelectedValue != "-1" && !newUserchecked.Checked); 
        } 
HTML Code here
<telerik:RadComboBox runat="server" OnClientLoad="cboUser_Load" OnClientItemsRequested="cboUserItemsRequest" OnClientSelectedIndexChanged="cboUser_IndexChanged" 
                                                        OnItemsRequested="cboUsers_ItemsRequested" Width="100%" ID="cboUsers" /> 
                                                    <asp:CustomValidator OnServerValidate="cboUsers_Validate" ErrorMessage="Please Select a User" ControlToValidate="cboUsers" 
                                                        EnableClientScript="false" Display="None" runat="server" /> 
BUT on the server side code SelectedValue is "" i have got a work around which is to put the selected value into a hidden input field but i shouldn't have too.


1 Answer, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 05 May 2010, 09:52 AM
Hi David,

I used your code to create a sample page and debugged this page on our side.

The issue that you describe is caused by cboUserItemsRequest function.

Within this function there is a new RadComboBoxItem created with Text and Value properties set to empty string.  When I commented these lines of code - the SelectedValue property started to pass its correct value to server.
function cboUserItemsRequest(sender, args) {
    var userCbo = $telerik.toComboBox(sender);
    userCbo.trackChanges();
 
    if (userCbo.get_items().get_count() == 0) {
        chkNewUser.trigger('click');
        userCbo.disable();
        userCbo.commitChanges();
        return;
    }
    else {
 
        userCbo.enable();
//      var comboItem = new Telerik.Web.UI.RadComboBoxItem();
//      comboItem.set_text("");
//      comboItem.set_value("");
//      var items = userCbo.get_items()
//      items.insert(0, comboItem);
        userCbo.commitChanges();
        if (chkNewUser.attr("checked"))
            chkNewUser.trigger('click');
    }
 
    var cnHidVal = $telerik.$('#' + '<%= Page.FindControl("selectedUser").ClientID %>');
    if (cnHidVal.val() != "") {
        for (i = 0; i < userCbo.get_items().get_count(); i++) {
            var item = userCbo.get_items().getItem(i);
            if (item.get_value() == cnHidVal.val()) {
                item.select();
            }
 
        }
    }
}


Could you please revise the client-side logic that you use?

If the issue persists - please paste the full code of your page here.
Thank you in advance.

Best wishes,
Kalina
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
David
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Share this question
or