locked
Recreate Dynamically Created TextBoxes RRS feed

  • Question

  • User-1385105225 posted

    I have a page that will dynamically create text boxes based on a NumberOfFilesValue and place them in a placeholder. This page might postback a few times as the user changes values on the page. I'm not able to find the placeholder in Pre_Init.

    using DataServices_v2.Shared;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace DataServices_v2
    {
    	public partial class test : System.Web.UI.Page
    	{
            protected void Page_PreInit(object sender, EventArgs e)
            {
                List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("Filename")).ToList();
                foreach (string key in keys)
                {
                    AddTextBoxRow tbRow = new AddTextBoxRow();
                    FilenamePlaceholder.Controls.Add(tbRow.NewRow(key));
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
    		{
                //
    		}
    
            protected void NumberOfFilesValue_TextChanged(object sender, EventArgs e)
            {
                FilenamePlaceholder.Controls.Clear();
                int file_count = Convert.ToInt16(NumberOfFilesValue.Text);
                string pre = "Filename ";
                for (int i = 1; i <= file_count; i++)
                {
                    string control_text = pre + i.ToString();
                    AddTextBoxRow tbRow = new AddTextBoxRow();
                    FilenamePlaceholder.Controls.Add(tbRow.NewRow(control_text));
                }
            }
    
            protected void BatchSubmit_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using DataServices_v2.Mover;
    
    namespace DataServices_v2.Shared
    {
        public class AddTextBoxRow
        {
    
            public HtmlGenericControl NewRow(string ControlText)
            {
                HtmlGenericControl main_div = new HtmlGenericControl("div"); //main div
                main_div.Attributes["class"] = "form-group";
    
                Label l = new Label
                {
                    ID = ControlText.Replace(" ", String.Empty) + "Label",
                    Text = ControlText,
                    CssClass = "control-label col-md-2"
                };
                main_div.Controls.Add(l); //add label to main div
    
                HtmlGenericControl value_div = new HtmlGenericControl("div"); //secondary div
                value_div.Attributes["class"] = "col-md-10";
    
                TextBox tb = new TextBox
                {
                    ID = ControlText.Replace(" ",String.Empty) + "Value",
                    CssClass = "form-control"
                };
                value_div.Controls.Add(tb);
    
                main_div.Controls.Add(value_div); //add secondary div to main div
                return main_div;
            }               
      
        }
    }
    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="DataServices_v2.test" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    
         <div class="form-group">
                <asp:Label ID="NumberOfFilesLabel" runat="server" Text="Number of Files" CssClass="control-label col-md-2" />
                <div class="col-md-10">
                    <asp:TextBox ID="NumberOfFilesValue" runat="server" OnTextChanged="NumberOfFilesValue_TextChanged" AutoPostBack="true" CssClass="form-control" style="width:50px" />
                </div>
            </div>
    
            <asp:Panel ID="FilenamePlaceholder" runat="server" />
            <hr />
        <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <asp:Button ID="BatchSubmit" runat="server" Text="Create Files" OnClick="BatchSubmit_Click" CssClass="btn btn-default" />
                </div>
            </div>
    
    
    </asp:Content>

    Wednesday, February 20, 2019 3:22 PM

Answers

  • User-1174608757 posted

    Hi fr33kyd33ky,

    According to your description, I have made a sample here.You couldn't find the placeholder because preInit is fired before the controls are initialized.So, it is obvious that you couldn't get the placeholder. I suggest you to put the control in page_load event. You could see as below:

    aspx:

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <div class="form-group">
                <asp:Label ID="NumberOfFilesLabel" runat="server" Text="Number of Files" CssClass="control-label col-md-2" />
                <div class="col-md-10">
                    <asp:TextBox ID="NumberOfFilesValue" runat="server" OnTextChanged="NumberOfFilesValue_TextChanged" AutoPostBack="true" CssClass="form-control" style="width:50px" />
                </div>
            </div>
    
            <asp:Panel ID="FilenamePlaceholder" runat="server" />
            <hr />
        <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <asp:Button ID="BatchSubmit" runat="server" Text="Create Files" OnClick="BatchSubmit_Click" CssClass="btn btn-default" />
                </div>
            </div>
    </asp:Content>
    

    aspx.cs:

    public partial class text : System.Web.UI.Page
        {
            protected void Page_PreInit(object sender, EventArgs e)
            {
               
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("Filename")).ToList();
                foreach (string key in keys)
                {
                    addrow tbRow = new addrow();
    
                    FilenamePlaceholder.Controls.Add(tbRow.NewRow(key));
                }
    
            }
            protected void NumberOfFilesValue_TextChanged(object sender, EventArgs e)
            {
                FilenamePlaceholder.Controls.Clear();
                int file_count = Convert.ToInt16(NumberOfFilesValue.Text);
                string pre = "Filename ";
                for (int i = 1; i <= file_count; i++)
                {
                    string control_text = pre + i.ToString();
                    addrow tbRow = new addrow();
                    FilenamePlaceholder.Controls.Add(tbRow.NewRow(control_text));
                }
            }
    
            protected void BatchSubmit_Click(object sender, EventArgs e)
            {
    
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 21, 2019 6:07 AM

All replies

  • User-1174608757 posted

    Hi fr33kyd33ky,

    According to your description, I have made a sample here.You couldn't find the placeholder because preInit is fired before the controls are initialized.So, it is obvious that you couldn't get the placeholder. I suggest you to put the control in page_load event. You could see as below:

    aspx:

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <div class="form-group">
                <asp:Label ID="NumberOfFilesLabel" runat="server" Text="Number of Files" CssClass="control-label col-md-2" />
                <div class="col-md-10">
                    <asp:TextBox ID="NumberOfFilesValue" runat="server" OnTextChanged="NumberOfFilesValue_TextChanged" AutoPostBack="true" CssClass="form-control" style="width:50px" />
                </div>
            </div>
    
            <asp:Panel ID="FilenamePlaceholder" runat="server" />
            <hr />
        <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <asp:Button ID="BatchSubmit" runat="server" Text="Create Files" OnClick="BatchSubmit_Click" CssClass="btn btn-default" />
                </div>
            </div>
    </asp:Content>
    

    aspx.cs:

    public partial class text : System.Web.UI.Page
        {
            protected void Page_PreInit(object sender, EventArgs e)
            {
               
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("Filename")).ToList();
                foreach (string key in keys)
                {
                    addrow tbRow = new addrow();
    
                    FilenamePlaceholder.Controls.Add(tbRow.NewRow(key));
                }
    
            }
            protected void NumberOfFilesValue_TextChanged(object sender, EventArgs e)
            {
                FilenamePlaceholder.Controls.Clear();
                int file_count = Convert.ToInt16(NumberOfFilesValue.Text);
                string pre = "Filename ";
                for (int i = 1; i <= file_count; i++)
                {
                    string control_text = pre + i.ToString();
                    addrow tbRow = new addrow();
                    FilenamePlaceholder.Controls.Add(tbRow.NewRow(control_text));
                }
            }
    
            protected void BatchSubmit_Click(object sender, EventArgs e)
            {
    
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 21, 2019 6:07 AM
  • User-1385105225 posted

    Beautiful! Thank you!

    Thursday, February 21, 2019 3:00 PM