代码搜索名称为“TextBoxName”的文本框。并检索其中包含的文本值 [英] the code searches for a Textbox of name "TextBoxName" and retrieves the text value contained in it

查看:273
本文介绍了代码搜索名称为“TextBoxName”的文本框。并检索其中包含的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this.GetType()。GetField(TextBoxName)。GetValue(this).ToString();



使用此代码我尝试检索TextBoxName。当用户提供字符串TextBoxName但我得到nullreferencexception时的文本。

this.GetType().GetField("TextBoxName").GetValue(this).ToString();

Using this code i try to Retrieve TextBoxName.Text when user supplies the string "TextBoxName" but i get a nullreferencexception.

推荐答案

您的问题很可能是被搜索的控件是私有的还是非公开的。在GetField调用中,您应该添加Binding Flags以允许搜索私有/受保护字段,如果这是您真正想要做的事情。



Your issue is that most likely the control being searched for is private or simply non-public. In the GetField call you should add Binding Flags to allow searching for the private / protected fields, if that's what you really want to do.

// replace "txtBoxName" with the actual text form the text box.
FieldInfo info = this.GetType().GetField("txtBoxName", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

if (null != info) {
    // Here get the value as an object or string if you 'know' it will always be a string
    object val = info.GetValue(this);
}


试试吧......

Try it...
<pre lang="c#">

(this.FindControl(textboxname)as TextBox).Text

(this.FindControl("textboxname") as TextBox ).Text


这篇关于代码搜索名称为“TextBoxName”的文本框。并检索其中包含的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆