如何在使用循环动态生成的代码隐藏和文本框中获取文本框值?ASP.NET C#

时间:2016-06-17 14:54:19

标签: c# asp.net .net gridview textbox

我如何通过使用循环来获取代码背后的textboxtextbox通过我们生成的textboxes'.Here is my。我想使用这些<form id="form" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" onrowdatabound="GridView1_RowDataBound"> </asp:GridView> <asp:Button ID="Button3" runat="server" OnClick="save" Text="Save" /> <asp:Button ID="Button2" runat="server" OnClick="show" Text="Button" /> </form> </asp:Content> gridview`图像LINK在gridview中插入值

ASPX:

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

ASPX.CS

show

当我点击textboxes按钮,然后3 Save生成(显示),我希望当我在textboxex中插入值并按gridview按钮时,值显示在Nameaddressnumprivate void LogToForm(object line, string eventTime, Log.Severity severity) { if (dataGridView_LogInfo.InvokeRequired) { dataGridView_LogInfo.Invoke ( new Action<object, string, Log.Severity>(LogtoFormCallback), new object[] { line, eventTime, severity } ); } else { LogtoFormCallback(line, eventTime, severity); } }

1 个答案:

答案 0 :(得分:0)

您需要解决许多问题

ASPX

请注意添加PlaceHolder控件。这用于简化动态控件的管理。

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

代码隐藏:

以下是我对Page_Load事件中需要进行的事情的假设

  1. 您创建一个DataTable 一次并将其存储在Session中。
  2. 您必须在每次回发时设置gridview数据源。但只有在进行保存时重新绑定
  3. 您必须每次都重新创建动态字段(CreateTbx()),但由于您只希望它们最初通过按钮控件显示,因此您需要一种机制来确定该按钮何时被点击,因此会话[“显示“]布尔标志并初始化一次。

    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();
    }
    
    1. 此处单击“显示”按钮,通过调用CreateTbx()

      创建动态字段
      public void show ( object sender, EventArgs e )
      {
          Session[ "shown" ] = true;
          CreateTbx();
      }
      
      1. 在这里,我使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 );
                }
            }
        }
        
      2. 此处导入更改与原始代码相比:

        1. 我正在使用PlaceHolder计数与Session["num"]。创建需要Session["num"],而不是处理。
        2. 使用对Session表的直接引用,而不是赋值给局部变量。
        3. 您不应该通过for循环遍历各个列并指定列值购买列名。
        4. 仅向表中添加一行。你循环遍历列而不是行
        5. 请注意,我们只需要重新绑定而不是重新分配GridView数据源

          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();
          }
          
        6. 希望这有帮助

相关问题