persisting dynamic controls

2 posts, 0 answers
  1. esteban
    esteban  avatar
    1 posts
    Member since:
    Jul 2008

    Posted 09 Jul 2008 Link to this post

    i am adding 2 controls dynamically, a textbox, and a button. they add ok, to each of the 3 tabs, i add them to a pageview.

    the problem is that when i click the button in any tab, is causing a postback, and then the controls disapear.

    how can i do to persist the controls after the postback is made?

    i am using version 2008.1.415.20
  2. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 09 Jul 2008 Link to this post

    Hello Esteban,

    You can use the ViewState collection of the page to store any configurations you might need to recreate the controls and add them to the page during a postback. This will allow you to persist your dynamic controls across postbacks.

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TabStrip.aspx.cs" Inherits="TabStrip" %> 
    2  
    3 <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    4  
    5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    6  
    7 <html xmlns="http://www.w3.org/1999/xhtml"
    8 <head runat="server"
    9     <title>Untitled Page</title> 
    10 </head> 
    11 <body> 
    12     <form id="form1" runat="server"
    13         <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
    14          
    15         <telerik:RadTabStrip ID="RadTabStrip1" runat="server" 
    16             MultiPageID="RadMultiPage1"
    17             <Tabs> 
    18                 <telerik:RadTab Text="One" /> 
    19                 <telerik:RadTab Text="Two" /> 
    20             </Tabs> 
    21         </telerik:RadTabStrip> 
    22          
    23         <telerik:RadMultiPage ID="RadMultiPage1" runat="server"
    24             <telerik:RadPageView ID="RadPageView1" runat="server" 
    25                 OnLoad="RadPageView1_Load" > 
    26                 <asp:Button ID="Button1" runat="server"  
    27                     Text="Add TextBox"  
    28                     OnClick="Button1_Click" /> 
    29             </telerik:RadPageView> 
    30             <telerik:RadPageView ID="RadPageView2" runat="server" 
    31                 OnLoad="RadPageView2_Load" > 
    32                 <asp:Button ID="Button2" runat="server"  
    33                     Text="Add Button" 
    34                     OnClick="Button2_Click" /> 
    35             </telerik:RadPageView> 
    36         </telerik:RadMultiPage> 
    37      
    38     </form> 
    39 </body> 
    40 </html> 
    41  

    1 using System; 
    2 using System.Web.UI.WebControls; 
    3  
    4 public partial class TabStrip : System.Web.UI.Page 
    5
    6     protected void RadPageView1_Load(object sender, EventArgs e) 
    7     { 
    8         if (ViewState["RadPageView1_Button_Text"] != null
    9         { 
    10             var button = new Button(); 
    11             button.Text = ViewState["RadPageView1_Button_Text"as string
    12             button.Click += new EventHandler(Button_Click); 
    13             RadPageView1.Controls.Add(button); 
    14         } 
    15     } 
    16  
    17     protected void RadPageView2_Load(object sender, EventArgs e) 
    18     { 
    19         if (ViewState["RadPageView2_TextBox_Text"] != null
    20         { 
    21             var textbox = new TextBox(); 
    22             textbox.Text = ViewState["RadPageView2_TextBox_Text"as string
    23             RadPageView2.Controls.Add(textbox); 
    24         } 
    25     } 
    26  
    27     protected void Button1_Click(object sender, EventArgs e) 
    28     { 
    29         var button = new Button { Text = "Click me!" }; 
    30         button.Click += new EventHandler(Button_Click); 
    31         RadPageView1.Controls.Add(button); 
    32         ViewState["RadPageView1_Button_Text"] = button.Text; 
    33  
    34     } 
    35  
    36     protected void Button2_Click(object sender, EventArgs e) 
    37     { 
    38         var textbox = new TextBox { Text = "Write in me!" }; 
    39         RadPageView2.Controls.Add(textbox); 
    40         ViewState["RadPageView2_TextBox_Text"] = textbox.Text; 
    41     } 
    42  
    43     protected void Button_Click(object sender, EventArgs e) 
    44     { 
    45         // Do something 
    46     } 
    47
    48  

    Please let me know if you continue to need help.

    Sincerely,
    Kevin Babcock
Back to Top