Selecting RadComboxBox Item

Thread is closed for posting
4 posts, 0 answers
  1. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 22 Jun 2009 Link to this post

    I'm using the RadComboBox control to do sorting on a grid I'm using.

    The RadComboBox I'm using is set like so:
    <telerik:RadComboBox ID="ddlSortBy" runat="server" OnClientSelectedIndexChanged="OrderSearch" 
                            CssClass="ddlSortBy" OnClientLoad="SortByLoad"
                            <Items> 
                                <telerik:RadComboBoxItem Text="Displayed Randomly" Value="1" /> 
                                <telerik:RadComboBoxItem Text="By Nightly Rate Ascending" Value="2" /> 
                                <telerik:RadComboBoxItem Text="By Nightly Rate Descending" Value="3" /> 
                                <telerik:RadComboBoxItem Text="By Weekly Rate Ascending" Value="4" /> 
                                <telerik:RadComboBoxItem Text="By Weekly Rate Descending" Value="5" /> 
                            </Items> 
                        </telerik:RadComboBox> 
    The problem I'm experiencing is setting the selected sort order when the page loads, since I pass in the url.

    What I mean, is that I can't write this:
    ddlSortBy.FindItemByValue("[query]").Selected = true
    When I write that nothing gets selected, it still remains with the first item selected.

    Is it not possible to select a specific item of the RadComboBox in MVC through code or should I be doing it another way. I've tried using javascript, but I handle the "OnClientSelectedIndexChanged" event of the control and when I select the item in javascript that event is raised causing the page to postback.

    If you could suggest a solution to a problem that would be great.

    Thanks.


  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 25 Jun 2009 Link to this post

    Hello,

    I am not sure about your exact scenario. When do you call this code? Could you please provide more info? Ideally you could send us your web site in a support ticket so we can troubleshoot.

    Regards,
    Albert
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 25 Jun 2009 Link to this post

    The code that I would write to select one of the items would be like this:
     
    <%using (Html.BeginForm("OrderSearch", "Search", new { queryString = queryString }, FormMethod.Post)) 
                          { 
    ddlSortBy.FindItemByValue(ViewData["sort"]).Selected = true
                        %> 
                        <telerik:RadComboBox ID="ddlSortBy" runat="server" OnClientSelectedIndexChanged="OrderSearch" 
                            CssClass="ddlSortBy" OnClientLoad="SortByLoad"
                            <Items> 
                                <telerik:RadComboBoxItem Text="Displayed Randomly" Value="1" /> 
                                <telerik:RadComboBoxItem Text="By Nightly Rate Ascending" Value="2" /> 
                                <telerik:RadComboBoxItem Text="By Nightly Rate Descending" Value="3" /> 
                                <telerik:RadComboBoxItem Text="By Weekly Rate Ascending" Value="4" /> 
                                <telerik:RadComboBoxItem Text="By Weekly Rate Descending" Value="5" /> 
                            </Items> 
                        </telerik:RadComboBox> 
                        <span class="label">View prices in:</span> 
                        <telerik:RadComboBox ID="ddlViewPrice" runat="server" OnClientSelectedIndexChanged="OrderSearch" 
                            CssClass="ddlViewPrice"
                            <Items> 
                                <telerik:RadComboBoxItem Text="US Dollars" Value="1" /> 
                                <telerik:RadComboBoxItem Text="Euros" Value="2" /> 
                                <telerik:RadComboBoxItem Text="British Pounds" Value="3" /> 
                                <telerik:RadComboBoxItem Text="Canada Dollars" Value="4" /> 
                            </Items> 
                        </telerik:RadComboBox> 
                        <%} %> 
     
    <script type="text/javascript" language="javascript"
            function OrderSearch(source, args) { 
                try { 
                    document.forms[1].submit(); 
                } 
                catch (err) { } 
            } 
        </script> 

    In the code above I have the combobox encapsulated in a form and I use it's OnSelectedIndexChanged event to submit the form. Now this line "ddlSortBy.FindItemByValue(ViewData["sort"]).Selected = true;", doesn't work even though it does find the "sort" value passed to it from the Controller action. With regards to the javascript function, I have two forms, thus the reason for the array indexer.

    I hope this helps you figure it out.
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 30 Jun 2009 Link to this post

    Hi robertw102,

    Indeed RadComboBox determines the selected item earlier considering the page life cycle. This is another limitation of using server side controls in ASP.NET MVC. The workaround is to update the Text property as well:

        ddlSortBy.FindItemByValue((string)ViewData["sort"]).Selected = true;
        ddlSortBy.Text = ddlSortBy.SelectedItem.Text;

    Regards,
    Albert
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top