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

Selecting RadComboxBox Item

3 Answers 92 Views
UI for ASP.NET AJAX in ASP.NET MVC
This is a migrated thread and some comments may be shown as answers.
robertw102
Top achievements
Rank 1
robertw102 asked on 22 Jun 2009, 06:36 PM
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.


3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 25 Jun 2009, 01:38 PM
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.
0
robertw102
Top achievements
Rank 1
answered on 25 Jun 2009, 03:59 PM
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.
0
Atanas Korchev
Telerik team
answered on 30 Jun 2009, 08:40 AM
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.
Tags
UI for ASP.NET AJAX in ASP.NET MVC
Asked by
robertw102
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
robertw102
Top achievements
Rank 1
Share this question
or