Gridview Get Textbox Value

Asked

Viewed 3,772 times

1

I intend to obtain the value of a textbox of a gridview. This Textbox is created by RowDataBound. As follows:

TextBox txtValor = new TextBox();
e.Row.Cells[i].Controls.Add(txtValor);

For the gridview is dynamic and cannot use the templatefield.

I’ve tried the following methods:

TextBox textBox = row.Cells[j].FindControl("txtValor") as TextBox;
string quantidade = ((TextBox)(GridView1.Rows[i].Cells[j].Controls[1])).Text.ToString();
  • When going through gridview I want to store the value in a list.

  • I need more details... do the following, try setting the Textbox ID property before adding it to the cells, and then just search with Findcontrol for that ID, since the name txtValue is the name of the variable that is holding a reference to the object, but the object itself did not have its ID filled. Besides, in this attempt: string quantidade = ((TextBox)(GridView1.Rows[i].Cells[j].Controls[1])).Text.ToString(); you are searching for the second control within the cell, is that correct? You have other controls within the cell?

  • tried this way: Textbox txtValor = new Textbox(); txtValor.Width = 15; txtValor.ID = "txt" then to read: Textbox myTextBox = (Textbox)(Gridview1.Rows[i].Cells[j].Findcontrol("txt"); a mytextbox da null;

2 answers

1

I created an example because, the way you are doing right after page reload it will lose all references from the added controls:

Create only 1 TemplateField and place the component TextBox with Id TxtValor.

<asp:GridView runat="server" ID="GridDados" ClientIDMode="Static" ViewStateMode="Enabled" ValidateRequestMode="Enabled">
    <Columns>               
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TxtValor" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="ButPesquisar" runat="server" Text="Pesquisar" OnClick="ButPesquisar_Click" />
<asp:Label Text="" ID="LblResultado" runat="server" />

Loading this Gridview:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Grid();

    }
}
private void Grid()
{   
    GridDados.DataSource = (new object[]
    {
        new { id = 1, nome = "teste 1"},
        new { id = 2, nome = "teste 2"}
    })
    .ToArray();

    GridDados.DataBind();

}

On the button ButPesquisar in his method ButPesquisar_Click place:

protected void ButPesquisar_Click(object sender, EventArgs e)
{

       LblResultado.Text = string.Empty;
       foreach (GridViewRow item in GridDados.Rows)
       {
           TextBox txtValor = item.FindControl("TxtValor") as TextBox;
           if (txtValor != null && !string.IsNullOrEmpty(txtValor.Text))
           {
               LblResultado.Text += txtValor.Text;
               LblResultado.Text += "<br>";
           }
      }
}

Upshot:

inserir a descrição da imagem aqui

That is, the data is loaded dynamically, but, the TextBox within the TemplateField is fixed.

Recommending

If you can make Gridview formatted, with the DataField already stipulated with TemplateField if need be, these dynamic creations bring more problems than solution.

  • The problem is that both rows, columns are dynamic there is no certain number. I do not know if this method serves

  • @user6018 in your question you should be more specific and show what you are doing are little information ...

0

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

e.NewValues["email"]=((TextBox)((GridView)sender).Rows[e.RowIndex].Cells[0].FindControl("MeuTextBox")).Text;

}

this in rowupdating!!! if not has how to know which line you changed the mail!

but I think the question in the case of Bind

goes on the templatecolumn right click on the textbox and place Bind("Email")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.