How to retrieve data from tablecell ASP.NET

て烟熏妆下的殇ゞ 提交于 2019-12-25 03:52:58

问题


When I load the page I send the number of rows the table will have:

protected void Page_Load(object sender, EventArgs e)
{
    string numFilas = Request.QueryString["filas"];
    tblAdd.Visible = true;
    for (int i = 0; i < int.Parse(numFilas); i++)
    {
        TableRow NewRow1 = new TableRow();
        TableCell NewCell1 = new TableCell();
        TableCell NewCell2 = new TableCell();

        TextBox txtBox1 = new TextBox();
        txtBox1.Width = 200;
        TextBox txtBox2 = new TextBox();
        txtBox2.Width = 200;
        // adding lebel into cell
        NewCell1.Controls.Add(txtBox1);
        NewCell2.Controls.Add(txtBox2);
        // adding cells to row
        NewRow1.Cells.Add(NewCell1);
        NewRow1.Cells.Add(NewCell2);

        tblAdd.Rows.Add(NewRow1);
    }
}

now when I click submit I'd like to retrieve the data of the textbox inside the table, what I've been capable of is this:

  public void submit(Object sender, EventArgs e)
{
    for (Int32 i = 0; i < tblAdd.Rows.Count; i++)
    {
        TableRow dr = tblAdd.Rows[i];
        TableCell hhh = dr.Cells[0];
        String textCell = hhh.Text();

    }
}

However, the text in the cells is empty because the text the user writes is IN the textbox, which I don't know how to get it.


回答1:


Try this

  1. While creating your text boxes add an ID

E.g.

txtBox1.ID = "txtBox1";
  1. Then you can easily find this TextBox control from the current Cell's Controls collection as follows.

    string textCell = ((TextBox)dr.Cells[0].FindControl("txtBox1")).Text;

Hope you understood what I'm trying to say. Basically, you need to find your control in the Cell.

Vote and accept the answer if it solved your issue.

Cheers!



来源:https://stackoverflow.com/questions/26090996/how-to-retrieve-data-from-tablecell-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!