C# 当文本框';是通过按钮动态添加的吗?

C# 当文本框';是通过按钮动态添加的吗?,c#,asp.net,textbox,postback,C#,Asp.net,Textbox,Postback,我对编码还不熟悉,希望有人能帮我一点忙,我一直在从ASP.NET中动态添加的文本框中检索数据 我掌握了网站和内容网站。我在内容网站上添加了一些按钮,用户需要添加或删除文本框 我的问题是,我不确定如何正确检索数据。希望有人能在路上帮助我 我的内容网站: <%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inheri

我对编码还不熟悉,希望有人能帮我一点忙,我一直在从ASP.NET中动态添加的文本框中检索数据

我掌握了网站和内容网站。我在内容网站上添加了一些按钮,用户需要添加或删除文本框

我的问题是,我不确定如何正确检索数据。希望有人能在路上帮助我

我的内容网站:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

<div id="div_fortext" class="div_fortext">
    <p class="header2">
        Opret RMA Sag
    </p>

    <p class="text1">
        Her Kan de oprette alt det udstyr der skal sendes til reperation hos zenitel.
    </p>
</div>

<div id="div_insert_devices"  runat="server">   

</div>
      // 3 buttons one who add, one who remove textboxes and a submit button
<asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
<asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />


</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["DeviceCount"] = ViewState["DeviceCount"] == null ? 1 : ViewState["DeviceCount"];
            InsertLine();
        }
    }

    private void InsertLine()
    {
        int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

        for (int i = 0; i < DeviceCount; i++)
        {
            LiteralControl text = new LiteralControl("<div class=\"divPerDevice\">");
            div_insert_devices.Controls.Add(text);

            TextBox txtbox = new TextBox();
            txtbox.ID = "serial" + i;
            txtbox.CssClass = "textbox1";
            txtbox.Attributes.Add("runat", "Server");
            div_insert_devices.Controls.Add(txtbox);

            text = new LiteralControl("</div>");
            div_insert_devices.Controls.Add(text);
        }    
    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());     
        count++;

        ViewState["DeviceCount"] = count;
        InsertLine();
    }

    protected void btnRemoveRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());
        count--;
        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {   
        // Submit - save the textboxes to Strings ??? Can any body help
    }
}
<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

   <div id="div_fortext" class="div_fortext">

       <p class="header2">
           Opret RMA Sag
       </p>

       <p class="text1">
           Some TEXT
       </p>

   </div>




   </div>

   <asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
   <asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />--%>



   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

   <asp:PlaceHolder runat="server" id="DynamicDevices"></asp:PlaceHolder>

   <asp:Button id="btnAddTextBox" runat="server" text="Tilføj" CssClass="butten1" OnClick="btnAddTextBox_Click" />
   <asp:Button id="btnRemoveTextBox" runat="server" text="Fjern" CssClass="butten1" OnClick="btnRemoveTextBox_Click" />



   <asp:Button runat="server" id="Submit" text="Submit" CssClass="butten1" OnClick="Submit_Click" />
   <br /><asp:Label runat="server" id="MyLabel"></asp:Label>

</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{


    static int myCount = 1;
    private TextBox[] dynamicTextBoxes;
    private TextBox[] Serial_arr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myCount = 1;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
        {
            if (myControl.ID.ToString() == "btnAddTextBox")
            {
                myCount = myCount >= 30 ? 30 : myCount + 1;
            }

            if (myControl.ID.ToString() == "btnRemoveTextBox")
            {
                myCount = myCount <= 1 ? 1 : myCount - 1;
            }

        }
    }


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dynamicTextBoxes = new TextBox[myCount];
        Serial_arr = new TextBox[myCount];
        int i;
        for (i = 0; i < myCount; i += 1)
        {
            LiteralControl literalBreak = new LiteralControl("<div>");
            DynamicDevices.Controls.Add(literalBreak);


            TextBox Serial = new TextBox();
            Serial.ID = "txtSerial" + i.ToString();
            Serial.CssClass = "";
            DynamicDevices.Controls.Add(Serial);
            Serial_arr[i] = Serial;

            TextBox textBox = new TextBox();
            textBox.ID = "myTextBox" + i.ToString();
            DynamicDevices.Controls.Add(textBox);
            dynamicTextBoxes[i] = textBox;


            literalBreak = new LiteralControl("</div>");
            DynamicDevices.Controls.Add(literalBreak);
        }
    }


    public static Control GetPostBackControl(Page thePage)
    {
        Control mycontrol = null;
        string ctrlname = thePage.Request.Params.Get("_EVENTTARGET");
        if (((ctrlname != null) & (ctrlname != string.Empty)))
        {
            mycontrol = thePage.FindControl(ctrlname);
        }
        else
        {
            foreach (string item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    mycontrol = c;
                }
            }
        }
        return mycontrol;
    }


    protected void Submit_Click(object sender, EventArgs e)
    {

        int deviceCount = Serial_arr.Count();

        DataSet ds = new DataSet();
        ds.Tables.Add("Devices");

        ds.Tables["Devices"].Columns.Add("Serial", System.Type.GetType("System.String"));
        ds.Tables["Devices"].Columns.Add("text", System.Type.GetType("System.String"));

        for (int x = 0; x < deviceCount; x++)
        {

            DataRow dr = ds.Tables["Devices"].NewRow();

            dr["Serial"] = Serial_arr[x].Text.ToString();
            dr["text"] = dynamicTextBoxes[x].Text.ToString();

            ds.Tables["Devices"].Rows.Add(dr);

        }

        //MyLabel.Text = "der er " + deviceCount +" Devices<br />";
        //foreach (TextBox tb in Serial_arr)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}
        //MyLabel.Text += "<br />";
        //foreach (TextBox tb in dynamicTextBoxes)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}


    }

    protected void btnAddTextBox_Click(object sender, EventArgs e)
    {

    }
    protected void btnRemoveTextBox_Click(object sender, EventArgs e)
    {

    }





}

奥普雷特尔马凹陷

她的职业生涯一直持续到泽尼特酒店。

//3个按钮一个添加,一个删除文本框和一个提交按钮
我的C#代码隐藏:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

<div id="div_fortext" class="div_fortext">
    <p class="header2">
        Opret RMA Sag
    </p>

    <p class="text1">
        Her Kan de oprette alt det udstyr der skal sendes til reperation hos zenitel.
    </p>
</div>

<div id="div_insert_devices"  runat="server">   

</div>
      // 3 buttons one who add, one who remove textboxes and a submit button
<asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
<asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />


</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["DeviceCount"] = ViewState["DeviceCount"] == null ? 1 : ViewState["DeviceCount"];
            InsertLine();
        }
    }

    private void InsertLine()
    {
        int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

        for (int i = 0; i < DeviceCount; i++)
        {
            LiteralControl text = new LiteralControl("<div class=\"divPerDevice\">");
            div_insert_devices.Controls.Add(text);

            TextBox txtbox = new TextBox();
            txtbox.ID = "serial" + i;
            txtbox.CssClass = "textbox1";
            txtbox.Attributes.Add("runat", "Server");
            div_insert_devices.Controls.Add(txtbox);

            text = new LiteralControl("</div>");
            div_insert_devices.Controls.Add(text);
        }    
    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());     
        count++;

        ViewState["DeviceCount"] = count;
        InsertLine();
    }

    protected void btnRemoveRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());
        count--;
        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {   
        // Submit - save the textboxes to Strings ??? Can any body help
    }
}
<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

   <div id="div_fortext" class="div_fortext">

       <p class="header2">
           Opret RMA Sag
       </p>

       <p class="text1">
           Some TEXT
       </p>

   </div>




   </div>

   <asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
   <asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />--%>



   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

   <asp:PlaceHolder runat="server" id="DynamicDevices"></asp:PlaceHolder>

   <asp:Button id="btnAddTextBox" runat="server" text="Tilføj" CssClass="butten1" OnClick="btnAddTextBox_Click" />
   <asp:Button id="btnRemoveTextBox" runat="server" text="Fjern" CssClass="butten1" OnClick="btnRemoveTextBox_Click" />



   <asp:Button runat="server" id="Submit" text="Submit" CssClass="butten1" OnClick="Submit_Click" />
   <br /><asp:Label runat="server" id="MyLabel"></asp:Label>

</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{


    static int myCount = 1;
    private TextBox[] dynamicTextBoxes;
    private TextBox[] Serial_arr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myCount = 1;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
        {
            if (myControl.ID.ToString() == "btnAddTextBox")
            {
                myCount = myCount >= 30 ? 30 : myCount + 1;
            }

            if (myControl.ID.ToString() == "btnRemoveTextBox")
            {
                myCount = myCount <= 1 ? 1 : myCount - 1;
            }

        }
    }


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dynamicTextBoxes = new TextBox[myCount];
        Serial_arr = new TextBox[myCount];
        int i;
        for (i = 0; i < myCount; i += 1)
        {
            LiteralControl literalBreak = new LiteralControl("<div>");
            DynamicDevices.Controls.Add(literalBreak);


            TextBox Serial = new TextBox();
            Serial.ID = "txtSerial" + i.ToString();
            Serial.CssClass = "";
            DynamicDevices.Controls.Add(Serial);
            Serial_arr[i] = Serial;

            TextBox textBox = new TextBox();
            textBox.ID = "myTextBox" + i.ToString();
            DynamicDevices.Controls.Add(textBox);
            dynamicTextBoxes[i] = textBox;


            literalBreak = new LiteralControl("</div>");
            DynamicDevices.Controls.Add(literalBreak);
        }
    }


    public static Control GetPostBackControl(Page thePage)
    {
        Control mycontrol = null;
        string ctrlname = thePage.Request.Params.Get("_EVENTTARGET");
        if (((ctrlname != null) & (ctrlname != string.Empty)))
        {
            mycontrol = thePage.FindControl(ctrlname);
        }
        else
        {
            foreach (string item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    mycontrol = c;
                }
            }
        }
        return mycontrol;
    }


    protected void Submit_Click(object sender, EventArgs e)
    {

        int deviceCount = Serial_arr.Count();

        DataSet ds = new DataSet();
        ds.Tables.Add("Devices");

        ds.Tables["Devices"].Columns.Add("Serial", System.Type.GetType("System.String"));
        ds.Tables["Devices"].Columns.Add("text", System.Type.GetType("System.String"));

        for (int x = 0; x < deviceCount; x++)
        {

            DataRow dr = ds.Tables["Devices"].NewRow();

            dr["Serial"] = Serial_arr[x].Text.ToString();
            dr["text"] = dynamicTextBoxes[x].Text.ToString();

            ds.Tables["Devices"].Rows.Add(dr);

        }

        //MyLabel.Text = "der er " + deviceCount +" Devices<br />";
        //foreach (TextBox tb in Serial_arr)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}
        //MyLabel.Text += "<br />";
        //foreach (TextBox tb in dynamicTextBoxes)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}


    }

    protected void btnAddTextBox_Click(object sender, EventArgs e)
    {

    }
    protected void btnRemoveTextBox_Click(object sender, EventArgs e)
    {

    }





}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类CreateRMA:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
ViewState[“DeviceCount”]=ViewState[“DeviceCount”]==null?1:ViewState[“DeviceCount”];
InsertLine();
}
}
私有void InsertLine()
{
int-DeviceCount=int.Parse(ViewState[“DeviceCount”].ToString());
for(int i=0;i

试试这个

您可以使用以下代码

protected void btnSubmit_Click(object sender, EventArgs e)
{   
    // Submit - save the textboxes to Strings ??? Can any body help
    int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

    for (int i = 0; i < DeviceCount; i++)
    {
      TextBox txtbx= (TextBox)div_insert_devices.FindControl("serial" + i);
      if(txtbx!=null)
      {
        var value= txtbx.Text;
      }
    }
}        
protectedvoid btnSubmit\u单击(对象发送方,事件参数e)
{   
//提交-将文本框保存为字符串???任何正文都能提供帮助吗
int-DeviceCount=int.Parse(ViewState[“DeviceCount”].ToString());
for(int i=0;i
我尝试这样做的方式如下:

protected void btnSubmit_Click(object sender, EventArgs e)
{
      foreach (Control control in div_insert_devices.Controls){
            if (control.GetType() == typeof(textbox)){
                   Textbox myDynTextbox = (Textbox)control;

                   Var myString = myDynTextbox.Text;
请注意,这段代码可以进一步简化,但我编写这段代码是为了让您了解它是如何工作的,我的建议是将所有字符串存储在字符串集合中,以便于维护


Kush

为什么不使用javascript保存textbox的值呢。只要将值保存在某个隐藏字段中,并在每次需要时绑定它。

嗨,我在这里找到了解决方案。。。我没有你提供的任何例子。对不起

但我几乎是白手起家,并按照我的意愿将此应用于precis:)

我的内容网站:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

<div id="div_fortext" class="div_fortext">
    <p class="header2">
        Opret RMA Sag
    </p>

    <p class="text1">
        Her Kan de oprette alt det udstyr der skal sendes til reperation hos zenitel.
    </p>
</div>

<div id="div_insert_devices"  runat="server">   

</div>
      // 3 buttons one who add, one who remove textboxes and a submit button
<asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
<asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />


</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["DeviceCount"] = ViewState["DeviceCount"] == null ? 1 : ViewState["DeviceCount"];
            InsertLine();
        }
    }

    private void InsertLine()
    {
        int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

        for (int i = 0; i < DeviceCount; i++)
        {
            LiteralControl text = new LiteralControl("<div class=\"divPerDevice\">");
            div_insert_devices.Controls.Add(text);

            TextBox txtbox = new TextBox();
            txtbox.ID = "serial" + i;
            txtbox.CssClass = "textbox1";
            txtbox.Attributes.Add("runat", "Server");
            div_insert_devices.Controls.Add(txtbox);

            text = new LiteralControl("</div>");
            div_insert_devices.Controls.Add(text);
        }    
    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());     
        count++;

        ViewState["DeviceCount"] = count;
        InsertLine();
    }

    protected void btnRemoveRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());
        count--;
        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {   
        // Submit - save the textboxes to Strings ??? Can any body help
    }
}
<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

   <div id="div_fortext" class="div_fortext">

       <p class="header2">
           Opret RMA Sag
       </p>

       <p class="text1">
           Some TEXT
       </p>

   </div>




   </div>

   <asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
   <asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />--%>



   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

   <asp:PlaceHolder runat="server" id="DynamicDevices"></asp:PlaceHolder>

   <asp:Button id="btnAddTextBox" runat="server" text="Tilføj" CssClass="butten1" OnClick="btnAddTextBox_Click" />
   <asp:Button id="btnRemoveTextBox" runat="server" text="Fjern" CssClass="butten1" OnClick="btnRemoveTextBox_Click" />



   <asp:Button runat="server" id="Submit" text="Submit" CssClass="butten1" OnClick="Submit_Click" />
   <br /><asp:Label runat="server" id="MyLabel"></asp:Label>

</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{


    static int myCount = 1;
    private TextBox[] dynamicTextBoxes;
    private TextBox[] Serial_arr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myCount = 1;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
        {
            if (myControl.ID.ToString() == "btnAddTextBox")
            {
                myCount = myCount >= 30 ? 30 : myCount + 1;
            }

            if (myControl.ID.ToString() == "btnRemoveTextBox")
            {
                myCount = myCount <= 1 ? 1 : myCount - 1;
            }

        }
    }


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dynamicTextBoxes = new TextBox[myCount];
        Serial_arr = new TextBox[myCount];
        int i;
        for (i = 0; i < myCount; i += 1)
        {
            LiteralControl literalBreak = new LiteralControl("<div>");
            DynamicDevices.Controls.Add(literalBreak);


            TextBox Serial = new TextBox();
            Serial.ID = "txtSerial" + i.ToString();
            Serial.CssClass = "";
            DynamicDevices.Controls.Add(Serial);
            Serial_arr[i] = Serial;

            TextBox textBox = new TextBox();
            textBox.ID = "myTextBox" + i.ToString();
            DynamicDevices.Controls.Add(textBox);
            dynamicTextBoxes[i] = textBox;


            literalBreak = new LiteralControl("</div>");
            DynamicDevices.Controls.Add(literalBreak);
        }
    }


    public static Control GetPostBackControl(Page thePage)
    {
        Control mycontrol = null;
        string ctrlname = thePage.Request.Params.Get("_EVENTTARGET");
        if (((ctrlname != null) & (ctrlname != string.Empty)))
        {
            mycontrol = thePage.FindControl(ctrlname);
        }
        else
        {
            foreach (string item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    mycontrol = c;
                }
            }
        }
        return mycontrol;
    }


    protected void Submit_Click(object sender, EventArgs e)
    {

        int deviceCount = Serial_arr.Count();

        DataSet ds = new DataSet();
        ds.Tables.Add("Devices");

        ds.Tables["Devices"].Columns.Add("Serial", System.Type.GetType("System.String"));
        ds.Tables["Devices"].Columns.Add("text", System.Type.GetType("System.String"));

        for (int x = 0; x < deviceCount; x++)
        {

            DataRow dr = ds.Tables["Devices"].NewRow();

            dr["Serial"] = Serial_arr[x].Text.ToString();
            dr["text"] = dynamicTextBoxes[x].Text.ToString();

            ds.Tables["Devices"].Rows.Add(dr);

        }

        //MyLabel.Text = "der er " + deviceCount +" Devices<br />";
        //foreach (TextBox tb in Serial_arr)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}
        //MyLabel.Text += "<br />";
        //foreach (TextBox tb in dynamicTextBoxes)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}


    }

    protected void btnAddTextBox_Click(object sender, EventArgs e)
    {

    }
    protected void btnRemoveTextBox_Click(object sender, EventArgs e)
    {

    }





}

奥普雷特尔马凹陷

一些文本

--%>
我的C#代码隐藏:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

<div id="div_fortext" class="div_fortext">
    <p class="header2">
        Opret RMA Sag
    </p>

    <p class="text1">
        Her Kan de oprette alt det udstyr der skal sendes til reperation hos zenitel.
    </p>
</div>

<div id="div_insert_devices"  runat="server">   

</div>
      // 3 buttons one who add, one who remove textboxes and a submit button
<asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
<asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />


</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["DeviceCount"] = ViewState["DeviceCount"] == null ? 1 : ViewState["DeviceCount"];
            InsertLine();
        }
    }

    private void InsertLine()
    {
        int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

        for (int i = 0; i < DeviceCount; i++)
        {
            LiteralControl text = new LiteralControl("<div class=\"divPerDevice\">");
            div_insert_devices.Controls.Add(text);

            TextBox txtbox = new TextBox();
            txtbox.ID = "serial" + i;
            txtbox.CssClass = "textbox1";
            txtbox.Attributes.Add("runat", "Server");
            div_insert_devices.Controls.Add(txtbox);

            text = new LiteralControl("</div>");
            div_insert_devices.Controls.Add(text);
        }    
    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());     
        count++;

        ViewState["DeviceCount"] = count;
        InsertLine();
    }

    protected void btnRemoveRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());
        count--;
        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {   
        // Submit - save the textboxes to Strings ??? Can any body help
    }
}
<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

   <div id="div_fortext" class="div_fortext">

       <p class="header2">
           Opret RMA Sag
       </p>

       <p class="text1">
           Some TEXT
       </p>

   </div>




   </div>

   <asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
   <asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />--%>



   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

   <asp:PlaceHolder runat="server" id="DynamicDevices"></asp:PlaceHolder>

   <asp:Button id="btnAddTextBox" runat="server" text="Tilføj" CssClass="butten1" OnClick="btnAddTextBox_Click" />
   <asp:Button id="btnRemoveTextBox" runat="server" text="Fjern" CssClass="butten1" OnClick="btnRemoveTextBox_Click" />



   <asp:Button runat="server" id="Submit" text="Submit" CssClass="butten1" OnClick="Submit_Click" />
   <br /><asp:Label runat="server" id="MyLabel"></asp:Label>

</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{


    static int myCount = 1;
    private TextBox[] dynamicTextBoxes;
    private TextBox[] Serial_arr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myCount = 1;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
        {
            if (myControl.ID.ToString() == "btnAddTextBox")
            {
                myCount = myCount >= 30 ? 30 : myCount + 1;
            }

            if (myControl.ID.ToString() == "btnRemoveTextBox")
            {
                myCount = myCount <= 1 ? 1 : myCount - 1;
            }

        }
    }


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dynamicTextBoxes = new TextBox[myCount];
        Serial_arr = new TextBox[myCount];
        int i;
        for (i = 0; i < myCount; i += 1)
        {
            LiteralControl literalBreak = new LiteralControl("<div>");
            DynamicDevices.Controls.Add(literalBreak);


            TextBox Serial = new TextBox();
            Serial.ID = "txtSerial" + i.ToString();
            Serial.CssClass = "";
            DynamicDevices.Controls.Add(Serial);
            Serial_arr[i] = Serial;

            TextBox textBox = new TextBox();
            textBox.ID = "myTextBox" + i.ToString();
            DynamicDevices.Controls.Add(textBox);
            dynamicTextBoxes[i] = textBox;


            literalBreak = new LiteralControl("</div>");
            DynamicDevices.Controls.Add(literalBreak);
        }
    }


    public static Control GetPostBackControl(Page thePage)
    {
        Control mycontrol = null;
        string ctrlname = thePage.Request.Params.Get("_EVENTTARGET");
        if (((ctrlname != null) & (ctrlname != string.Empty)))
        {
            mycontrol = thePage.FindControl(ctrlname);
        }
        else
        {
            foreach (string item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    mycontrol = c;
                }
            }
        }
        return mycontrol;
    }


    protected void Submit_Click(object sender, EventArgs e)
    {

        int deviceCount = Serial_arr.Count();

        DataSet ds = new DataSet();
        ds.Tables.Add("Devices");

        ds.Tables["Devices"].Columns.Add("Serial", System.Type.GetType("System.String"));
        ds.Tables["Devices"].Columns.Add("text", System.Type.GetType("System.String"));

        for (int x = 0; x < deviceCount; x++)
        {

            DataRow dr = ds.Tables["Devices"].NewRow();

            dr["Serial"] = Serial_arr[x].Text.ToString();
            dr["text"] = dynamicTextBoxes[x].Text.ToString();

            ds.Tables["Devices"].Rows.Add(dr);

        }

        //MyLabel.Text = "der er " + deviceCount +" Devices<br />";
        //foreach (TextBox tb in Serial_arr)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}
        //MyLabel.Text += "<br />";
        //foreach (TextBox tb in dynamicTextBoxes)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}


    }

    protected void btnAddTextBox_Click(object sender, EventArgs e)
    {

    }
    protected void btnRemoveTextBox_Click(object sender, EventArgs e)
    {

    }





}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统数据;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类CreateRMA:System.Web.UI.Page
{
静态int myCount=1;
私有文本框[]动态文本框;
专用文本框[]串行地址;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
myCount=1;
}
}
受保护的无效页_Init(对象发送方,事件参数e)
{
Control myControl=GetPostBackControl(this.Page);
if(myControl!=null)
{
if(myControl.ID.ToString()=“btnAddTextBox”)
{
myCount=myCount>=30?30:myCount+1;
}
if(myControl.ID.ToString()==“btnRemoveTextBox”)
{

myCount=myCount每次在page load中以及page load事件后,都需要创建动态创建的控件。它们的viewstate将被再次存储,之后您可以从中获取值。我不确定是否需要添加我在txtboxname中给它的普通名称或它在源代码中的名称e到txtbox:serial0--源代码示例:ctl00$mainsite$serial0使用Request.QueryString.Form.allkeys播放我已经尝试了您编写的内容,但出于某种原因,txtbx每次都为null。请在调试模式下查找此内容