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

Creating ComboBox Programmatically

14 Answers 352 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kyle Jones
Top achievements
Rank 1
Kyle Jones asked on 20 Jul 2009, 07:58 PM
I am trying to create a RadComboBox strictly using only the code behind.  I am coming into a problem.  I can create the object and populate it with data but when I try to click on the drop down to view and select the data I have stored in it nothing happens. Basically it is not letting me have access to my data.  When I add the <telerik:RadComboBox> tag to the design page and give it the same ID name that I did my object I created on the code behind it works just fine but this is not how I need to have it created.  Can you help me figure out what I am missing or what I am doing wrong?

Thanks,

Kyle

14 Answers, 1 is accepted

Sort by
0
Accepted
ManniAT
Top achievements
Rank 2
answered on 20 Jul 2009, 08:55 PM
Hi,

good question - "Can you help me figure out what I am missing or what I am doing wrong?"
Since I don't know what you exactly do I can't tell you what you do wrong :)

Anyhow - I can provide you with a working sample:
protected void Page_Load(object sender, EventArgs e) {  
    RadComboBox rcB = new RadComboBox();  
    rcB.ID = "MyCombo";  
    rcB.Items.Add(new RadComboBoxItem("Text1","val1"));  
    rcB.Items.Add(new RadComboBoxItem("Text2","val2"));  
    rcB.Items.Add(new RadComboBoxItem("Text3","val3"));  
    rcB.Items.Add(new RadComboBoxItem("Text4","val4"));  
    divPlaceHolder.Controls.Add(rcB);  
}  
And the markup looks something like this:
<telerik:RadScriptManager ID="rsmMain" runat="server" ...  
</telerik:RadScriptManager> 
<telerik:RadAjaxManager ID="ramManager" runat="server" ...  
</telerik:RadAjaxManager> 
<div> 
  <div id="divPlaceHolder" runat="server"></div> 
  ...  
</div> 
 
The inner div is used to have a good place where to insert the control.
RadScriptManager and RadAjaxManager could be on a masterpage - if you have one.

Do YOU know now what you were missing? :)

Regards

Manfred
PS: if the answer solved your problem - please "Mark as answer"
0
Kyle Jones
Top achievements
Rank 1
answered on 21 Jul 2009, 01:01 PM
OK,  Thank you for your response but unfortunately I did everything that you are showing me and it would not let me select anything out the drop down.  It would create the drop down for me but I was not able to click the down arrow to view my data nor able to click the text box and it show my data.  Any thoughts?

Kyle
0
ManniAT
Top achievements
Rank 2
answered on 21 Jul 2009, 01:26 PM
Hi,

no - no idea. For me it works.

I created a page on my test site
http://www.pp-p.net/TDynCombo.aspx
The page markup:
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
    <telerik:RadScriptManager runat="server">  
    </telerik:RadScriptManager> 
    <div> 
    <div runat="server" id="divPlaceHolder"></div> 
    <p> 
        <href="TDynCombo.aspx">Refresh</a></p>  
    <p> 
        <href="Default.aspx">Home</a></p>  
          
    </div> 
    </form> 
</body> 
</html> 
 
And the code (no changes to the previous sample):
public partial class TDynCombo : System.Web.UI.Page {  
    protected void Page_Load(object sender, EventArgs e) {  
        RadComboBox rcB = new RadComboBox();  
        rcB.ID = "MyCombo";  
        rcB.Items.Add(new RadComboBoxItem("Text1", "val1"));  
        rcB.Items.Add(new RadComboBoxItem("Text2", "val2"));  
        rcB.Items.Add(new RadComboBoxItem("Text3", "val3"));  
        rcB.Items.Add(new RadComboBoxItem("Text4", "val4"));  
        divPlaceHolder.Controls.Add(rcB);  
    }  
}  
 
Does this combo work for you?
Could you (just to check it out) create an empty page in your solution and add this combo and code to it?

And by the way - which version of the controls do you use?
I use 2009.2.701.35

Regards

Manfred
0
Kyle Jones
Top achievements
Rank 1
answered on 21 Jul 2009, 02:40 PM
Thanks for the help.  I was able to get it working finally.  My next question though is how can I get my dropdown to keep the data that I selected?  Meaning,  when I select data that is in the drop down and then click somewhere else on the page it refreshes my drop down back to the first item.

Kyle
0
ManniAT
Top achievements
Rank 2
answered on 21 Jul 2009, 02:50 PM
Hi,

what you say tells me two things:
a.) You have autopostback on at the combo (so finishing a selection "postbacks")
b.) ASP.NET works as expected :))

The thing is "comlicated" - a bit.
Not basically - but it depends a lot on what you do.
I guess (not sure) you set the "SelectedIndex" or something on your code behind.

If you need this initially - make a condition around it
if(!IsPostBack) {
   SetSelectedIndexSomeHow();
}

I just added one button to the testpage (no code behind) - and you can see the combo keeps its selection.
    <asp:Button runat="server" Text="PostBack" /> 
You can see it at http://www.pp-p.net

Regards

Manfred
0
Kyle Jones
Top achievements
Rank 1
answered on 21 Jul 2009, 02:58 PM
OK, Here is what i have going on. When I am creating my combo box.

RadComboBox RadComboBox1 = new RadComboBox();
RadComboBox1.ID = "RadComboBox1";
Page.Master.FindControl("DropDown").Controls.Add(RadComboBox1);
RadComboBox1.EnableTextSelection = true;
RadComboBox1.AutoPostBack = false;
RadComboBox1.DataTextField = "TABLE_NAME";
RadComboBox1.DataValueField = "TABLE_SCHEMA";
RadComboBox1.DataSource = PopulateDDL("Some Query");
RadComboBox1.DataBind();

As you can see I do not have autopostback set to true.  That is what is puzzling to me.  I am not even trying to get the data out of my drop down yet.  I am confused why when I select the data and just click a random spot on the screen the drop down refreshes and resets my selection back to the first item of the list.

Kyle
0
ManniAT
Top achievements
Rank 2
answered on 21 Jul 2009, 03:03 PM
Is it possible, that you have enable AutoPostBack for the page?

And another thing - communication runs fast now -- it maybe you wait for the next answer...
I'm leaving to go to a "Mostbauer" for dinner - don't expect an answer before 21:00 (it's 17:00 at the moment).
Definition Mostbauer: A kind of farmer who offers self made products. You sit in front of the house and eat the great selfmade things :)
--Bauer==Farmer
Definition Most - Applejuice on his way to alcohol :)
Regards

Manfred
0
Kyle Jones
Top achievements
Rank 1
answered on 21 Jul 2009, 03:12 PM
I have looked at my code and I don't see any reference to AutoPostBack being set to true.  I don't want the page to post back at all.  I have it set up right now to run off radajaxmanager along with a radgrid that I have already created and have up and working.
0
ManniAT
Top achievements
Rank 2
answered on 21 Jul 2009, 06:30 PM
Hi,

did you attach a "On...Changed" handler to the combobox?
An aspx page does not postback without a reason.
0
Kyle Jones
Top achievements
Rank 1
answered on 21 Jul 2009, 06:34 PM
I don't think that is is posting back cause the page is not refreshing like a normal post back would cause.  It is just resetting itself.  I dont have any "on...change" set in my code.  All the code that I have written in the code that I sent you previously.
0
Atanas Korchev
Telerik team
answered on 22 Jul 2009, 08:07 AM
Hi Kyle Jones,

I recommend you open a support ticket and send us a running web site showing the problem you are experiencing.

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
ManniAT
Top achievements
Rank 2
answered on 22 Jul 2009, 08:59 AM
Hy Kyle,

this would be the best solution - it's not really predictable why things do what they do without knowing your code.

Regards

Manfred
0
Lenny_shp
Top achievements
Rank 2
answered on 22 Jul 2009, 12:47 PM
Are you creating this control in Page_Load event and how are you handling the viewstate of the control?
0
Kyle Jones
Top achievements
Rank 1
answered on 22 Jul 2009, 01:35 PM
Thanks for all your help on this problem guys.  I just found out what my problem was.  I was populating my drop down via a method that called my DB.  I was storing the DB data in the DataSource field but was never creating the new items in the RadComboBox so every time I would click it it would just refresh the ComboBox cause I did not have any declared items.

Kyle
Tags
ComboBox
Asked by
Kyle Jones
Top achievements
Rank 1
Answers by
ManniAT
Top achievements
Rank 2
Kyle Jones
Top achievements
Rank 1
Atanas Korchev
Telerik team
Lenny_shp
Top achievements
Rank 2
Share this question
or