2016-03-30 72 views
0

我正在嘗試使用字符串動態地創建文本框。然後嘗試讀取按鈕點擊。從動態創建的文本框中讀取

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

從代碼中可以看出,我試圖使用find控件訪問文本框,並循環遍歷。但兩者都失敗了。

+0

使用'Placeholder',而不是一個div(它會生成一個div),然後添加控制佔位符,而不是使用InnerHtml –

+0

@ ADreNaLiNe DJ我正在使用innetHTML,因爲我有表,然後添加多個文本框的列。我簡化了代碼。 – user3202862

+1

InnerHtml不會使您的ASP.Net控件「編譯/執行」。它只是插入字符串作爲/是 –

回答

1

我設法得到它的工作,但僅具有Page_Init event來創建動態文本框。所以每次有回帖時,都需要重新創建動態控件。你可以在網上查詢,他們說同樣的事情。

因此,這裏的客戶端:

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

    } 
+0

謝謝。有效。 – user3202862

0

使用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; 
     } 
    } 
} 
+0

感謝您的回答。我已經嘗試了上面的代碼。如果我沒有錯,你沒有使用「TextBoxDiv」,所以我用「TextBoxPlaceHolder」替換了它,但仍然無法獲得值。 – user3202862