使用 FindControl 设置文本框的文本

发布于 2024-09-28 20:21:37 字数 248 浏览 0 评论 0原文

我需要能够在运行时将文本框(位于 gridview 行内)文本设置为某个字符串。我以前使用过 FindControl,但无法弄清楚实际设置文本框的值而不仅仅是获取的语法。这是我所拥有的,但无法编译:

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

如果有任何帮助,我将不胜感激,

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-10-05 20:21:37

这行得通吗?

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

编辑:实际上我喜欢这比我原来的帖子更好:

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

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

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");
猫烠⑼条掵仅有一顆心 2024-10-05 20:21:37

它无法编译的原因是因为看起来 GetString() 函数的末尾有一个额外的右括号。

试试这个:

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

最好的做法是检查 TextBox 是否不为空,但这不是必需的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文