如何获取codebehind中的文本框值以及使用loop动态生成的文本框?ASP.NET C#

如何获取codebehind中的文本框值以及使用loop动态生成的文本框?ASP.NET C#,c#,asp.net,.net,gridview,textbox,C#,Asp.net,.net,Gridview,Textbox,如何在code behind中获取textbox值,以及使用循环以Namaly方式生成的textbox。我想使用这些textbox在gridview中插入值。这是我的gridview`图像 ASPX: ASPX.CS public void show(object sender, EventArgs e) { string s = Session["num"].ToString(); int num = Int32.Parse(s); for (int i =0;

如何在code behind中获取
textbox
值,以及使用循环以Namaly方式生成的
textbox
。我想使用这些
textbox在gridview中插入值。这是我的
gridview`图像

ASPX:


ASPX.CS

 public void show(object sender, EventArgs e)
{
    string s = Session["num"].ToString();
    int num = Int32.Parse(s);

    for (int i =0; i <num; i++)
    {
        TextBox _text = new TextBox();
        _text.Visible = true;
         _text.ID = i.ToString();
         this.form.Controls.Add(_text);
   }

}

public void save(object sender, EventArgs e)
{
   string s = Session["num"].ToString();
    int num = Int32.Parse(s);
    DataTable t = (DataTable)Session["tab"];
    DataRow row1 = t.NewRow();
    for (int i = 0; i < num; i++)
    {
        foreach (Control f in form.Controls)
        {
            if (f is TextBox)
            {
                TextBox txt = (TextBox)f;
                string str = txt.Text;
                row1["Name"] = str;
                row1["address"] = str;
                row1["num"] = str;
                t.Rows.Add(row1);
            }

        }
    }
    GridView1.DataSource = t;
    GridView1.DataBind();
}
public void show(对象发送方,事件参数e)
{
字符串s=Session[“num”].ToString();
int num=Int32.Parse;

对于(inti=0;i,您需要解决许多问题

ASPX

请注意,添加了占位符控件。这用于更轻松地管理动态控件

<form>
  <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
  <asp:Button ID="Button3" runat="server" OnClick="save" Text="Save" />
  <asp:Button ID="Button2" runat="server" OnClick="show" Text="Button" />
</form>

  • 这里单击了Show按钮,并通过调用CreateTbx()创建了动态字段


  • 在这里,我使ID更符合传统的命名标准

    private void CreateTbx ()
    {
        if ( ( bool ) Session[ "shown" ] )
        {
            string s = Session[ "num" ].ToString();
            int num = Int32.Parse( s );
    
            for ( int i =0; i < num; i++ )
            {
                TextBox _text = new TextBox();
                _text.Visible = true;
                _text.ID = string.Format( "TbxInput{0}", i );
                PlaceHolder1.Controls.Add( _text );
            }
        }
    }
    
  • 希望这有帮助

    protected void Page_Load ( object sender, EventArgs e )
    {
        if ( !Page.IsPostBack )
        {
            Session[ "num" ] = 3;
            DataTable tab =   new DataTable();
            Session[ "tab" ] = tab;
            tab.Columns.Add( "Name" );
            tab.Columns.Add( "address" );
            tab.Columns.Add( "num" );
    
            Session[ "shown" ] = false;
        }
    
        GridView1.DataSource = ( DataTable ) Session[ "tab" ];
        CreateTbx();
    }
    
    public void show ( object sender, EventArgs e )
    {
        Session[ "shown" ] = true;
        CreateTbx();
    }
    
    private void CreateTbx ()
    {
        if ( ( bool ) Session[ "shown" ] )
        {
            string s = Session[ "num" ].ToString();
            int num = Int32.Parse( s );
    
            for ( int i =0; i < num; i++ )
            {
                TextBox _text = new TextBox();
                _text.Visible = true;
                _text.ID = string.Format( "TbxInput{0}", i );
                PlaceHolder1.Controls.Add( _text );
            }
        }
    }
    
    public void save ( object sender, EventArgs e )
    {
        DataRow row = (( DataTable ) Session[ "tab" ]).NewRow();
    
        for ( int i = 0; i < PlaceHolder1.Controls.Count; i++ )
        {
            string tbxid = string.Format( "TbxInput{0}", i );
            TextBox txt = ( TextBox ) PlaceHolder1.FindControl( tbxid );
            row[ i ] = txt.Text;
        }
    
        ( ( DataTable ) Session[ "tab" ] ).Rows.Add( row );
    
        GridView1.DataBind();
    }