日期:2014-05-16  浏览次数:20385 次

字符串转换成对象的问题
如在aspx文件中有一个控件<asp:TextBox ID="Text1" runat="server"></TextBox>
一般在后台Text1.Text=“内容”  这样就可以赋值了。但我把ID为“Text1”的字符串存到数据库中后,下次在cs代码中读取数据库中的ID字符串“Text1”的,然后进行赋值,这样怎么转换? 因为有时后台cs代码是不能静态的知道要赋值的对象,所以需要动态的获取ID。换言之,如何把对象存到数据库中,以备以后的其他模块获取。
------解决方案--------------------
反射
typeof(this).GetField("text1").SetValue(this, 值)
------解决方案--------------------
Page.FindControl

但是注意:
The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container.

参考:
http://msdn.microsoft.com/en-us/library/31hxzsdw(v=vs.110).aspx