开发者

_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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜