Page mypage = (Page)System.Web.HttpContext.Current.Handler; string s = (string) mypage.ViewState["Whatever"];
you are breaking the encapsulation, if you need to read something from your Page container, you should expose a specific property in your Page class and shouldn't depend on whether the value is saved in ViewState or not, for example,public string MyID { get {...} set {...} }if you insist, you can always use Reflection to access protected or private properties
现在的原因是,userControl.Page没有VeiwState属性。
page没有viewstate属性,而继承于page的类有viewstate属性。
你说怪不怪?????
希望大侠出现
{
get
{
return ViewState;
}
}然后在用户控件中 Page.ViewState1["name"];
问题2:如何让page页面读出userControl里的viewState?
5027527(Cool_FX) 的方法应该是属于向用户控件传递的方法,和论题有所偏差,论题的目的就是不用传递,如何调用viewstate
string s = (string) mypage.ViewState["Whatever"];
{
get {...}
set {...}
}if you insist, you can always use Reflection to access protected or private properties
通过属性的方法是比较正规的解决方法,但是有时候一个用户控件需要在很多页面中使用,如果每个页面都写几句调用得代码相当麻烦,而且如果后面客户提出修改导致需要一个个文件去修改更是头疼得事情。
我的想法是在用户控件自己去读取page的viewState中的信息,从而做出相对应的修改,从使用方便性来说是不是大大提高了。
不过可能是我学习.net的时间不长,编写过程中经常会带入其他语言的编程思想,而没有使用上.net的正确解决方案。
用 Reflection 来提取类的信息可能是目前能想到的方法了,但是好像有点旁门左道,也有点违背OO思想,呵呵,我考虑一下再决定是否使用,不过还是要谢谢你提供的这个方法。
我在查找和ViewState相关贴子的时候,发现很多问题都是你解决的,对.net有比较深层次的理解,请以后多多指教。