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

[Solved] RadComboBox Template

5 Answers 244 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Stacey Haslag
Top achievements
Rank 1
Stacey Haslag asked on 28 Jan 2010, 05:41 PM
I have tried multiple ways of getting a RadComboBox to work in my application, and have not had any success yet. I am hoping I can get some guidance.

My situation seems pretty simple and common: I need a drop down list of items to contain two unique id's and some text for my items. Whichever item is selected by the user, I need to get the two unique id's for that selected item.  Also, I only want to load my radcombobox during the initial page load, not on individual postbacks.  And I want the first item in the list to be a "default" type of item that has no unique id's assigned to it and the test is just something like "<Select>".

I have tried using the ItemTemplate, and setting up attributes to hold my 2 unique id's, but the attributes disappear after a postback.

I tried using the ItemTemplate with some Hidden Field controls to hold my 2 unique id's.

I tried not using the ItemTemplate, but just setting the item.DataField to my object that contain the information I want.

All combinations of these i have tried loading several ways...calling the DataBind() on the main combobox control; Adding the items one at a time, and calling the DataBind() on each individual item...but each time, I get something to work, but not work 100%.

Is there an example that someone can give me of a good way to do this? I would think my situation is fairly common...

Thanks in Advance!
-Stacey


5 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 28 Jan 2010, 06:41 PM

I'd try something like:

protected void Page_Load(object sender, EventArgs e)  
{  
   if (!Page.IsPostBack)  
   {  
      PopulateSearchBoxes();  
   }  
}  
 
private void PopulateSearchBoxes()  
{  
   _yourDDL.DataSource = YourDataSource;  
   _yourDDL.DataTextField = "id";  
   _yourDDL.DataValueField = "name";  
   _yourDDL.ItemDataBound += new Telerik.Web.UI.RadComboBoxItemEventHandler(YourDDLDataBound);  
   _yourDDL.DataBind();  
 
protected void YourDDLDataBound(object sender, Telerik.Web.UI.RadComboBoxItemEventArgs e)  
{  
   if (!String.IsNullOrEmpty(e.Item.Value))  
   {  
      e.Item.Value = e.Item.Value + "_" + yourOtherUniqueId  
   }  
   else 
   {  
      e.Item.Text = "Your Intro No value"//set during the datasource populate  
   }  

That would give the Value for each DDL item something like "2938_20394"  where 2938 is ID1 and 20394 is ID2. When they choose something just split the selected item value on "_" or whatever delimeter you chose.
0
Stacey Haslag
Top achievements
Rank 1
answered on 28 Jan 2010, 06:47 PM
Thanks for the suggestion; I actually have gotten a scenario like this to work in the past by splitting out the value field. I could do that, but I know there has to be other ways; and it seems like the capabilities are there with the RadComboBox - I just haven't been able to get it to work.
0
Kalina
Telerik team
answered on 29 Jan 2010, 03:59 PM
Hi Stacey Haslag,

RadComboBox items provide a special collection called Attributes. You can use this collection to expand the information stored with the items. More about this topic you can find here.

I made for you an example page that implements your requirements: RadComboBox is databound on PageLoad event, there is an initial "Select..." item in it, and there are two IDs for every RadComboBox item.

The approach is simple: when databinding the RadComboBoxItems I add one custom attribute to represent the secondID, then I simply retrieve the item's text, value and secondID attribute in the click event handler of a button.

Sincerely yours,
Kalina
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
Stacey Haslag
Top achievements
Rank 1
answered on 01 Feb 2010, 05:03 PM
Thanks for the response. That would work except for a few things I don't want to do in my situation.

I don't want to have to reload the RadComboBox on each page postback; I only want to load the control one time - during the initial page load. And it seems that with using Attributes, I will lose the attribute if I don't reload on each postback...is this correct?

Also, the "default" item in the list I would like to be able to be "selected". Sometime the users will need to change their minds - after they have selected a valid item, they may need to actually change this back to the "default" item.  So instead of just displaying some text in the "selected" section of the combo box, I would like there to actually be a "<Select>" type of item in my list of items to select.

Do you have any more suggestions for me to try? I would really like to be able to use this RadComboBox for this type of situation. What I am doing now is what Mike suggested, and just catting my id's together to store in the one value property, and then splitting them apart when I am retrieving the selected item.
0
Kalina
Telerik team
answered on 02 Feb 2010, 02:08 PM
Hello Stacey Haslag,

RadComboBox provides its own Load On Demand mechanism - it loads its Items when you open the DropDown for the first time and when you type any text in the input.

At this example the first RadComboBox (“Product”) uses this mechanism and Attributes.

More details about adding initial “select” item in RadComboBox you can find here.

Kind regards,
Kalina
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
ComboBox
Asked by
Stacey Haslag
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Stacey Haslag
Top achievements
Rank 1
Kalina
Telerik team
Share this question
or