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

[Solved] WCF Params

4 Answers 140 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Vistalogix
Top achievements
Rank 1
Vistalogix asked on 13 Jan 2010, 10:39 PM
Hi,

I have a RadComboBox that i would like to be populeated from a WCF Service, i see all of your examples and they all work just great, one thing i can figure out how to do is send in a param that has nothing to do with the dropdownlist other than what it is loading...  So lets say i have 3 ComboBox's on the screen and each of them should use the same exact WebMethod from my service, the difference is they each send in a different param.  The following is my ServiceMethods

        [WebGet(ResponseFormat = WebMessageFormat.Xml)]  
        IEnumerable<RadComboBoxItemData> ICommonManager.EnumToList(Type t)  
        {  
            SortedList<string, int> myList = Common.GetEnumDataSource(t);  
            List<RadComboBoxItemData> items = new List<RadComboBoxItemData>();  
            for (int i = 0; i < myList.Count; i++)  
            {  
                RadComboBoxItemData itemData = new RadComboBoxItemData();  
                itemData.Text = myList.Keys[i];  
                itemData.Value = myList.Values[i].ToString();  
                items.Add(itemData);  
            }  
            return items;  
        }  
 
As you can see this method needs to be called with the Type of Enum send in.  I have seen from examples on the site that you can send in 

RadComboBoxContext

 

 but that does not help as all that is, is an IDictionary.  There are over 100 Enums that i try to do this with, it would be horrible if i had to create a WebMethod for each and everyone of these Enums.

Is there something i can set when i create this combobox(i create all of my dynamically with the ability to know if it's datasource comming from a webservice before the control is created) to send in to the webservice the type of enum i need it to return?  Also all of my dropdowns have an 'EmptyMessage' set to something.

BTW: the reason i do that SortedList is so that i can return the Enum in Alpa order, here is that function for those that would like it.

        public static SortedList<string, Int32> GetEnumDataSource(Type enumType)  
        {  
            SortedList<string, Int32> returnCollection = new SortedList<string, Int32>();  
            try  
            {  
                if (enumType.BaseType == typeof(Enum))  
                {  
                    string[] enumNames = Enum.GetNames(enumType);  
                    foreach (string current in enumNames)  
                    {  
                        returnCollection.Add(current, Convert.ToInt32(Enum.Parse(enumType, current)));  
                    }  
                }  
                return returnCollection;  
            }  
            catch (Exception ex)  
            {  
                return null;  
            }  
        } 


EDIT:: I know i can easily take in a param of string and do reflection to create the Type, and that would be fine, i just need to the ability to let the webservice know which Enum to create and retrun...

4 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 14 Jan 2010, 05:27 PM
Hello Vistalogix,

Passing parameters to your WebMethod is as easy as hooking on the itemsRequesting client event and using something like the following code:

<script type="text/javascript">
        function itemsRequesting(sender, args) {
            var context = args.get_context();
            context["test"] = "test";
        }
 
    </script>

Then, on the server, you can use the passed param as follows:

for (int i = 0; i < numberOfItems; i++)
        {
            ComboBoxItemData itemData = new ComboBoxItemData();
            itemData.Text = "Item " + i + context["test"];
            items.Add(itemData);
        }

More information regarding that event can be found here.

I have prepared sample project that demonstrates the approach. You can find it as an attachment.

All the best,
Genady Sergeev
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
Vistalogix
Top achievements
Rank 1
answered on 14 Jan 2010, 09:58 PM
Hi,

You know i seen this example, and it never kicked in, lmao.  I normally don't ask alot of questions on here, as i can normally find what i need just by searching.  And this one just totally escaped me, man i must be getting old, been doing this for over 15 years...

Thanks,
Andrew
0
Genady Sergeev
Telerik team
answered on 18 Jan 2010, 01:23 PM
Hi Vistalogix,

Please, make sure that your WebMethod accepts one RadComboBoxContext parameter named as context. It is really important the name to be context, otherwise the WebMethod won't work. This approach works on regular ASP.NET services along with WCF services.

Did you manage to reproduce the problem on the attached sample? I have recorded a video for you showing the feature working on the sample project that I sent you. If this is still failing on your side, could you please open a support ticket and attach a sample project demonstrating the failure. We will then take a look ant try to fix the problem for you.

Kind regards,
Genady Sergeev
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
Vistalogix
Top achievements
Rank 1
answered on 18 Jan 2010, 04:37 PM
Thanks,

The whole problem i was have was my Bindings in my Service, i was using ws2007HttpBinding instead of webHttpBinding for populating data in the dropdowns.  ws2007HttpBinding works great if you are calling from CodeBehind but not from Ajax.

Thanks,
Andrew
Tags
ComboBox
Asked by
Vistalogix
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Vistalogix
Top achievements
Rank 1
Share this question
or