Chris
Chris

Reputation: 7611

_Setting_ textbox's text using FindControl

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

Upvotes: 0

Views: 6377

Answers (2)

Jamie
Jamie

Reputation: 988

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.

Upvotes: 0

AGoodDisplayName
AGoodDisplayName

Reputation: 5653

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");

Upvotes: 2

Related Questions