TextBox inside GridView

I want to get the Text property from a TextBox inside a GridView. This TextBox has some data that comes from my database. When I change this data, I want to do an update in my database.

But when I look for the text of my TextBox, it gets the old value that comes from my database, and not the value that I set now.

How can I do to get my actual data that I write in my TextBox, and not what comes from my database?

protected void VerifyPriority() { for (int i = 0; i < GridView1.Rows.Count; i++) { GridViewRow RowView = (GridViewRow)GridView1.Rows[i]; TextBox txt = (TextBox)GridView1.Rows[i].FindControl("txtPriority"); if (txt != null) { if (txt.Text != this.changes[i]) { this.UpdatePriority(this.codigo[i], txt.Text); } } } } 
+4
source share
1 answer

Most likely, you overwrite the GridView after each postback, rather than binding it once and letting ViewState save the data. If you anchor the GridView every time the page is sent back, any changes you make will be destroyed and replaced with information from the database.

Only bind to load the first page (in this case):

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = GetSomeData(); GridView1.DataBind(); } } 

After installing the above code, you can get the correct data from the TextBox:

 foreach (GridViewRow row in GridView1.Rows) { TextBox txt = row.FindControl("TextBox1") as TextBox; if (txt != null) { string value = txt.Text; } } 
+5
source

All Articles