views:

25

answers:

2

Hi all, I need to be able to set a textbox's (which is inside a gridview row) text to a certain string in runtime. I have used FindControl before, but can't figure out the syntax for actually setting the value of the textbox rather than just getting. Here's what I have, which doesn't compile:

((TextBox)e.Row.FindControl("txtPath")).Text = dataMap.GetString("targetPath"));

I'd be grateful for any help

Thanks

+1  A: 

Will this work?

(e.Row.FindControl("txtPath") as TextBox).Text = dataMap.GetString("targetPath");

EDIT: Actually I like this is better than my orginal post:

TextBox txtPath = (TextBox)e.Row.FindControl("txtPath");

if(txtPath != null) 
    txtPath.Text = dataMap.GetString("targetPath");
AGoodDisplayName
A: 

The reason it doesn't compile is because it looks like you have a extra closing bracket on the end of the GetString() function.

Try this:

((TextBox)e.Row.FindControl("txtPath")).Text = dataMap.GetString("targetPath"); 

It is best practice to check that the TextBox is not null, but is not required.

Jamie