0

I am trying to create a textbox dynamically using a string. and then trying to read on a buttton click.

 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="textboxtest.aspx.cs" Inherits="test2.textboxtest" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 <div id="TextBoxDiv" runat="server" class="asid box">
 </div>
<asp:Button ID="Button1" runat="server" Text="CreateTextBox" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="ReadTextBox" onclick="Button2_Click" />
</asp:Content>

Here is the code behind.

    protected void Button1_Click(object sender, EventArgs e)
    {           
        string finalText = @"<input type=""text"" ID=""T1"" runat=""server"">
                             <asp:TextBox ID=""TB1"" runat=""server""></asp:TextBox>";            
        TextBoxDiv.InnerHtml = finalText;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {          
        TextBox txtAddress2 = (TextBox)Page.FindControl("TB1");  
        foreach (Control c in TextBoxDiv.Controls)
        {
            if (c is TextBox)
            {
                TextBox txt = (TextBox)c;
                string str = txt.Text;
            }
        }
    }

As you can see from the code i have tried to access the textbox using find control and also looping through. but both are failing.

user3202862
  • 207
  • 4
  • 19
  • use a `Placeholder` instead of a div (it will generate a div) and then add controls in the placeholder instead of using `InnerHtml` – ADreNaLiNe-DJ Mar 30 '16 at 09:52
  • @ADreNaLiNe-DJ I am using innetHTML because I have got table and then adding multiple columns with textboxes. i have simplified the code. – user3202862 Mar 30 '16 at 09:57
  • 1
    InnerHtml doesn't make your ASP.Net controls to be "compiled/executed". It just insert the string as/is – ADreNaLiNe-DJ Mar 30 '16 at 09:58
  • So have you tried changing the TB1 to T1 in button2 click. to see if you can find the input control. Also you are missing a"/" for your input control in the button 1 click – JustLearning Mar 30 '16 at 10:00
  • @ShareYourKnowledge i have tried both T1 and TB1 and also added "/" but still no joy. – user3202862 Mar 30 '16 at 10:05
  • @user3202862 you should also remove the foreach loop and debug to see if `TextBox txtAddress2 = (TextBox)Page.FindControl("T1"); ` is null or not – JustLearning Mar 30 '16 at 10:26
  • @ShareYourKnowledge "txtAddress2" is null. – user3202862 Mar 30 '16 at 10:30
  • Why don't you put textboxes inside div instead of creating them on button click ? – ADreNaLiNe-DJ Mar 30 '16 at 10:38
  • @ADreNaLiNe-DJ beacuse i need to add multiple textboxes and populate them with values from and xml file, if there is any value. – user3202862 Mar 30 '16 at 10:44
  • See this post, it should work for you [Generate dynamic controls](http://stackoverflow.com/a/21617697/6128276). – Vivek Singh Mar 30 '16 at 10:46
  • @VivekSingh Thanks. I tried that before posting this question. It works in a single page but when i use master page it doesn't work. – user3202862 Mar 30 '16 at 10:55

2 Answers2

1

I managed to get it to work, but by only having to create dynamic textboxes in Page_Init event. So every time there is a post back you need to re-create your dynamic controls. You can check online, they say the same thing.

So here is the client side:

     <asp:PlaceHolder runat="server" id="TextBoxesHere" />
            <asp:Button ID="Button1" CssClass="btn btn-primary btn-outline" runat="server" 
                        Text="CreateTextBox" OnClick="Button1_Click" />
            <asp:Button ID="Button2" CssClass="btn btn-primary btn-outline" runat="server" 
                        Text="ReadTextBox" OnClick="Button2_Click" />

Server Side:

  protected void Page_Init(object sender, EventArgs e)
    {
        TextBox txt = new TextBox();
        txt.ID = "T1";
        txt.CssClass = "form-control";
        TextBoxesHere.Controls.Add(txt);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox txt = new TextBox();
        txt.ID = "T1";
        txt.CssClass = "form-control";
        TextBoxesHere.Controls.Add(txt);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)TextBoxesHere.FindControl("T1");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('" + txt.Text + "');", true);

    }
JustLearning
  • 3,164
  • 3
  • 35
  • 52
0

use a Placeholder instead of a div (it will generate a div) and then add controls in the placeholder instead of using InnerHtml.

So this can be achieved like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="textboxtest.aspx.cs" Inherits="test2.textboxtest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:PlaceHolder runat="server" ID="TextBoxPlaceHolder"></asp:PlaceHolder>
    <asp:Button ID="Button1" runat="server" Text="CreateTextBox" onclick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="ReadTextBox" onclick="Button2_Click" />
</asp:Content>

Code-behind:

protected void Button1_Click(object sender, EventArgs e)
{           
    string finalText = @"<input type=""text"" ID=""T1"" runat=""server"">"; 
    var textbox = new TextBox();
    textbox.ID = "TB1";
    this.TextBoxPlaceHolder.Controls.Add(new LiteralControl(finalText));
    this.TextBoxPlaceHolder.Controls.Add(textbox);
}

protected void Button2_Click(object sender, EventArgs e)
{          
    TextBox txtAddress2 = (TextBox)Page.FindControl("TB1");  
    foreach (Control c in TextBoxDiv.Controls)
    {
        if (c is TextBox)
        {
            TextBox txt = (TextBox)c;
            string str = txt.Text;
        }
    }
}
ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
  • Thanks for the answer. I have tried the above code. If i am not wrong you are not using "TextBoxDiv" so i have replaced it with "TextBoxPlaceHolder" but still i can't get the values. – user3202862 Mar 30 '16 at 10:18