Creating ComboBox Programmatically

15 posts, 1 answers
  1. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 20 Jul 2009 Link to this post

    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
  2. Answer
    ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 20 Jul 2009 Link to this post

    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"
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 21 Jul 2009 Link to this post

    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
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 21 Jul 2009 Link to this post

    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
  6. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 21 Jul 2009 Link to this post

    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
  7. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 21 Jul 2009 Link to this post

    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
  8. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 21 Jul 2009 Link to this post

    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
  9. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 21 Jul 2009 Link to this post

    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
  10. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 21 Jul 2009 Link to this post

    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.
  11. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 21 Jul 2009 Link to this post

    Hi,

    did you attach a "On...Changed" handler to the combobox?
    An aspx page does not postback without a reason.
  12. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 21 Jul 2009 Link to this post

    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.
  13. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 22 Jul 2009 Link to this post

    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.
  14. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 22 Jul 2009 Link to this post

    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
  15. Lenny_shp
    Lenny_shp avatar
    343 posts
    Member since:
    Jul 2006

    Posted 22 Jul 2009 Link to this post

    Are you creating this control in Page_Load event and how are you handling the viewstate of the control?
  16. Kyle Jones
    Kyle Jones avatar
    30 posts
    Member since:
    Jul 2009

    Posted 22 Jul 2009 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017