Looping Grid

P

Paulo

Hi, I have a grid with a template column wich is a TextBox, how can I
retrieve the values typed by the user? I tried the code below but it always
returns empty string:

for (int i = 0; i < grdDocs.Rows.Count; i++)
{
Response.Write(grdDocs.Rows.Cells[2].Text.ToString() + "<br>");
}

Thanks in advance!
 
M

Mark Fitzpatrick

Well, the main problem here is you're not actually accessing the textbox,
you're accessing the text property of the cell which won't get you anything
unless you set the text of that cell. You need to find the control within
the cell

TextBox tb = (TextBox)grdDocs.Rows.Cells[2].FindControl("name of your
textbox");
if(text != null)
Response.Write(tb.Text + "<br />");

I may be off a tad as I usually don't fetch data out of grid controls, but
this is a fairly standard ASP.Net concept. Also, make sure you really have
the right cell. It's easy to forget about a spacer cell and you end up
trying to get values out of the wrong cell.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
S

seigo

Hi, I have a grid with a template column wich is a TextBox, how can I
retrieve the values typed by the user? I tried the code below but it always
returns empty string:

for (int i = 0; i < grdDocs.Rows.Count; i++)
{
Response.Write(grdDocs.Rows.Cells[2].Text.ToString() + "<br>");

}

Thanks in advance!


Hi Paulo,

You should use FindControl() method of GridViewRow object to find
TextBox:

foreach (GridViewRow row in GridView1.Rows)
{
TextBox textbox = (TextBox)row.FindControl("TextBox1");
}

Instead of "TextBox1" as a parameter you should pass the name of
TextBox control that you placed in Grid template.
After this you can access Text property of textbox object.

Regards,
Alexander Kleshchevnikov
MCP
www.klalex.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top