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

Creating RAD Controls Completely Programatically

4 Answers 115 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
S
Top achievements
Rank 1
S asked on 01 Dec 2008, 09:26 PM
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...

4 Answers, 1 is accepted

Sort by
0
Kevin Babcock
Top achievements
Rank 1
answered on 01 Dec 2008, 09:40 PM
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
0
S
Top achievements
Rank 1
answered on 01 Dec 2008, 09:59 PM
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...
0
Princy
Top achievements
Rank 2
answered on 02 Dec 2008, 07:31 AM
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.
0
Paul
Telerik team
answered on 02 Dec 2008, 07:48 AM
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.
Tags
General Discussions
Asked by
S
Top achievements
Rank 1
Answers by
Kevin Babcock
Top achievements
Rank 1
S
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Paul
Telerik team
Share this question
or