从动态创建的文本框读取

Read textBox from dynamically created

我正在尝试使用字符串动态创建文本框。然后尝试点击按钮阅读。

 <%@ 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>

这是后面的代码。

    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;
            }
        }
    }

正如您从代码中看到的那样,我尝试使用查找控件访问文本框并循环访问。但两者都失败了。

使用 Placeholder 而不是 div(它将生成 div),然后在占位符中添加控件而不是使用 InnerHtml.

所以可以这样实现:

<%@ 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>

代码隐藏:

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;
        }
    }
}

我设法让它工作,但只需要在 Page_Init event 中创建动态文本框。因此,每次出现 post 后退时,您都需要重新创建动态控件。你可以上网查一下,他们说的都是一样的

这是客户端:

     <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" />

服务器端:

  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);

    }