Search code examples
c#asp.netgridviewtextboxfindcontrol

_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


Solution

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