none
Recreate Dynamic TextBox RRS feed

  • Question

  • 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>
    

    • Moved by CoolDadTx Wednesday, February 20, 2019 2:52 PM ASP.NET related
    Tuesday, February 19, 2019 6:57 PM

All replies

  • Hi Christian,

    Thank you for posting here.

    According your post, your question is more relevant to the asp.net, please post in the following forum.

    https://forums.asp.net/18.aspx/1?Web+Forms

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 20, 2019 5:26 AM