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

[Solved] Select a specific value on data bind from WEBSERVICE

2 Answers 125 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
New
Top achievements
Rank 1
New asked on 12 Jan 2010, 03:10 PM

Hi,

I don't understand if this is possible... I've search and read a lot in examples and forum but I don't found any.

My scenario is a DetailsView within some ComboBox in edit template...

...when I'm in edit mode I will be select the binded value in the Combobox
(like when appends with this code <telerik:RadComboBox ID="RadCmb1" runat="server" ... SelectedValue='<%# Bind("Value")) %>' > )
...but I my case I load the ComboBox data from a webservice, but there are not items while a text is write in combo and for this reason the binding operation throws a error.

I use webservice for performance because the items in combobox are very much...
...there is a way to select an item from value when binding DetailsView?

Thanks
New

2 Answers, 1 is accepted

Sort by
0
New
Top achievements
Rank 1
answered on 12 Jan 2010, 03:37 PM
I found an approch that works:

I have a TextBox neer the RadComboBox... I bind the value for the RadComboBox in TextBox and on DataBinding event I add and select the item in ComboBox... It works but in my opinion this isn't a clean solution... there is a way to do it better?

    protected void TextBox1_DataBinding(object sender, EventArgs e)  
    {  
        // TextBox that contains the value of the item to select in ComboBox  
        TextBox txt = sender as TextBox;   
        // RadComboBox to add and select item  
        RadComboBox cmb = this.DetailsView1.FindControl("RadCmb1"as RadComboBox;  
        if (txt != null && cmb != null)  
        {  
            if (string.IsNullOrEmpty(txt.Text) == false)  
            {  
                /*
                 *  Part of code to retreive item informations from db by value
                 */ 
 
                // adding the RadComboBoxItem with infornation and value  
                cmb.Items.Add(new RadComboBoxItem(/*Informations*//*Value*/));  
                // select the interested item  
                cmb.SelectedValue = /*Value*/;                                  
            }  
        }  
    } 
0
Accepted
Simon
Telerik team
answered on 13 Jan 2010, 03:37 PM
Hello New,

Yes, you can handle the DataBound event of the DetailsView, find the RadComboBox inside it and create the Item by extracting the needed value from the DataItem.

Below is the sample code showing this:

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
    if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
    {
        RadComboBox comboBox = DetailsView1.FindControl("RadCmb1") as RadComboBox;
        RadComboBoxItem item = new RadComboBoxItem();
 
        item.Text = (string)(DetailsView1.DataItem as DataRowView)["Value"];
 
        comboBox.Items.Add(item);
    }
}

Regards,
Simon
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.
Tags
ComboBox
Asked by
New
Top achievements
Rank 1
Answers by
New
Top achievements
Rank 1
Simon
Telerik team
Share this question
or