Creating RAD Controls Completely Programatically

5 posts, 0 answers
  1. S
    S avatar
    2 posts
    Member since:
    Nov 2008

    Posted 01 Dec 2008 Link to this post

    I would like to  create RAD controls programatically, without initially dragging and dropping the controls from the toolbar.  All of the documentation and information I've reviewed covers adding items and configuring the controls (progamatically) 'after' you drag a control onto the design surface initially.

    What I want to do is create an ASP.NET custom control, that creates and configures rad controls... can anyone help out?  Thought I could just do someting like:

    RadMenu menu = new RadMenu();
    myCtrl.Controls.Add(menu);
    etc...
  2. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 01 Dec 2008 Link to this post

    Hi,

    You are absolutely correct. In order to add a control programmatically all you need to do is simple instantiate that control and set any properties, then add that control to the containing control. So, if you wanted to create a new RadGrid, bind it to a List of data, and then add it to a RadAjaxPanel, you could do it like this:

    var radGrid = new RadGrid { ID = "RadGrid1" }; 
    radGrid.DataSource = new ArrayList 
        new { FirstName = "John", LastName = "Doe", Age = 27 }, 
        new { FirstName = "Frank", LastName = "Smith", Age = 39 }, 
        new { FirstName = "Sue", LastName = "Wilson", Age = 18 } 
    }; 
    radGrid.DataBind(); 
    RadAjaxPanel1.Controls.Add(radGrid); 

    Keep in mind that all RadControls live inside the Telerik.Web.UI namespace. So you will have to either use the fully qualified class names when instantiating the controls, or add a using directive to your source file.

    I hope this helps. Please let me know if you have any further questions.

    Regards,
    Kevin Babcock
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. S
    S avatar
    2 posts
    Member since:
    Nov 2008

    Posted 01 Dec 2008 Link to this post

    That's what I thought... although I've been working with the menu and panel bars to start... but not having much luck... for instance in trying to create a panel bar from a code-behind I have:

    RadScriptManager scriptmgr = new RadScriptManager();
    RadPanelBar panelbar = new RadPanelBar();
    RadPanelItem menuitem = new RadPanelItem();
    menuitem.Text = "Test";
    panelbar.Items.Add(menuitem);
    scriptmgr.Controls.Add(panelbar);

    This does not render anything on the aspx page however...
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Dec 2008 Link to this post

    Hello,

    Try creating the scriptmanager declaratively and then adding the dynamically created panelbar control to the control collection of the script manager as shown below.
    aspx:
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
     </telerik:RadScriptManager> 

    cs:
            RadPanelBar panelbar = new RadPanelBar(); 
            RadPanelItem menuitem = new RadPanelItem(); 
            menuitem.Text = "Test"
            panelbar.Items.Add(menuitem); 
            RadScriptManager1.Controls.Add(panelbar); 

    Also go through this link which explains on programmatic creation of the RadGrid control.

    Thanks
    Princy.
  6. Paul
    Admin
    Paul avatar
    4281 posts

    Posted 02 Dec 2008 Link to this post

    Hi there,

    Please find below your modified code snippet that shows the needed approach.

    protected void Page_Load(object sender, EventArgs e)  
    {  
        RadScriptManager scriptmgr = new RadScriptManager();  
        scriptmgr.ID = "RadScriptManager1";  
        Form.Controls.Add(scriptmgr);  
     
        RadPanelBar panelbar = new RadPanelBar();  
        panelbar.ID = "RadPanelBar1";  
        Form.Controls.Add(panelbar);  
     
        if (!IsPostBack)  
        {  
            RadPanelBar pb = (RadPanelBar)Page.FindControl("RadPanelBar1");  
     
            RadPanelItem menuitem = new RadPanelItem();  
            menuitem.Text = "Test";  
            pb.Items.Add(menuitem);  
        }  


    Best wishes,
    Paul
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017