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

Add Dynamic control

2 Answers 180 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bibhukalyana
Top achievements
Rank 1
Bibhukalyana asked on 31 Jan 2012, 05:02 PM
Hi everyone,
                I want to add any control dynamically in panel or layout.Can any one told me in which panel or layout i can add control dynamically.
I tried to add checkbox in panel(RadXmlHttpPanel also).
protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void RadMenu1_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
    {
        lblContent.Text = "u clicked " + e.Item.Text;
    }
    protected void RadToolBar1_ButtonClick(object sender, Telerik.Web.UI.RadToolBarEventArgs e)
    {
        lblContent.Text = "u clicked " + e.Item.Text;
    }
    protected void RadTabStrip1_TabClick(object sender, Telerik.Web.UI.RadTabStripEventArgs e)
    {
        lblContent.Text = "u clicked " + e.Tab.Text;
    }
    protected void RadButton_addControl_Click(object sender, EventArgs e)
    {
        int i;
        for (i = 0; i < 4; i++)
        {
            CheckBox ch = new CheckBox();
            ch.ID = "ch" + i.ToString();
            this.form1.Controls.Add(ch);
        }
    }
 
in xml
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
    <style type="text/css" media="screen">
        #content
        {
         
        height: 165px;
        width: 500px;
        margin-left: 75px;
        vertical-align: top;
        }
        #tabs
        {
        margin-left: 75px;
        width: 500px;
        vertical-align: bottom;
        }
    </style>
</head>
<body style="height: 437px">
    <form id="form1" runat="server">
    <asp:ScriptManager ID = "ScriptManager1" runat = "server"></asp:ScriptManager>
    <div>
    <telerik:RadMenu ID="RadMenu1" Runat="server" style="top: 0px; left: 0px"
        onitemclick="RadMenu1_ItemClick">
        <Items>
            <telerik:RadMenuItem runat="server" Text="Edit">
                <Items>
                    <telerik:RadMenuItem runat="server" Text="copy">
                    </telerik:RadMenuItem>
                    <telerik:RadMenuItem runat="server" Text="cut">
                    </telerik:RadMenuItem>
                    <telerik:RadMenuItem runat="server" Text="paste">
                    </telerik:RadMenuItem>
                </Items>
            </telerik:RadMenuItem>
            <telerik:RadMenuItem runat="server" Text="print">
            </telerik:RadMenuItem>
        </Items>
    </telerik:RadMenu>
    </div>
    <br />
    <br />
    <div>
    <telerik:RadToolBar ID="RadToolBar1" Runat="server"
        onbuttonclick="RadToolBar1_ButtonClick" Orientation="Vertical" Width="70px">
        <Items>
            <telerik:RadToolBarDropDown runat="server" Text="edit">
                <Buttons>
                    <telerik:RadToolBarButton runat="server" Text="copy">
                    </telerik:RadToolBarButton>
                    <telerik:RadToolBarButton runat="server" Text="cut">
                    </telerik:RadToolBarButton>
                    <telerik:RadToolBarButton runat="server" Text="paste">
                    </telerik:RadToolBarButton>
                </Buttons>
            </telerik:RadToolBarDropDown>
            <telerik:RadToolBarSplitButton runat="server" Text="print">
            </telerik:RadToolBarSplitButton>
        </Items>
    </telerik:RadToolBar>
    </div>
     
    <br />
    <br />
    <div id="content" runat="server">
        <asp:Label ID="lblContent" runat="server" Text="Label"></asp:Label>
 
        <div id="tabs" runat="server">
            <telerik:RadTabStrip ID="RadTabStrip1" runat="server" Orientation="HorizontalBottom"
                Skin="WebBlue" OnTabClick="RadTabStrip1_TabClick" SelectedIndex="0" Width="500px">
            </telerik:RadTabStrip>
        </div>
    </div>
    <telerik:RadButton ID="RadButton_addControl" runat="server"
            onclick="RadButton_addControl_Click" Text="AddControl">
          </telerik:RadButton>
    </form>
    </body>
</html>


This is working for first time but when i am trying do any other operation the check box is removed from the panel.
How i will add the control ?
Can any one give a simple example ?

thanks
bibhu

2 Answers, 1 is accepted

Sort by
0
RvdGrint
Top achievements
Rank 1
answered on 31 Jan 2012, 10:00 PM
bibhu,

when adding a control dynamically you should add it every time a PostBack took place. So on every postback you should re-add the control. 
My solution to this is that I save in the Session which control has been added dynamically (ViewState will also be an option) . When a PostBack is triggered I rebuild the added controls on the OnInit or Page_load from the Session.

Regards,
  Jos.
0
Bibhukalyana
Top achievements
Rank 1
answered on 01 Feb 2012, 10:09 AM
Thanks jos
I followed this and able to do it.

regards
bibhu
Tags
General Discussions
Asked by
Bibhukalyana
Top achievements
Rank 1
Answers by
RvdGrint
Top achievements
Rank 1
Bibhukalyana
Top achievements
Rank 1
Share this question
or