C# MultiView SetActiveView by ID 无法正常工作

发布于 2024-11-02 16:35:12 字数 418 浏览 1 评论 0原文

我目前正在尝试找出我的代码有什么问题:

不起作用 工作

if(...){  
    ...  
}else{  
    someVariableAsString = "myValue123";  
    MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);  
}

..

if(...){  
    ...  
}else{  
    //someVariableAsString = "myValue123";  
    MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);  
}

为什么以及对此有什么解决方案吗?

I'm currently trying to figure out what's wrong with my code:

Doesn't work

if(...){  
    ...  
}else{  
    someVariableAsString = "myValue123";  
    MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);  
}

Works

if(...){  
    ...  
}else{  
    //someVariableAsString = "myValue123";  
    MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);  
}

.. why and any solutions for this?

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

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

发布评论

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

评论(3

长发绾君心 2024-11-09 16:35:12

由于您尝试对 INIT 而不是负载进行操作,因此数据尚未附加到服务器。

您应该会发现 ASP.NET 中 Web 请求生命周期的回顾很有用: MSDN ASP.NET 页面生命周期

以下是相关摘录:

初始化

 页面初始化期间,页面上的控件可用并且

每个控件的 UniqueID 属性是
放。母版页和主题也是
应用于页面(如果适用)。如果
当前请求是回发,
回发数据尚未加载
和控制属性值还没有
已恢复为视图中的值
状态。

<前><代码>加载

加载期间,如果当前请求是回发,则控制

属性加载了信息
从视图状态和控制中恢复
状态。

将您尝试执行的代码移至页面加载处理程序中(或之后)(请记住测试 IsPostBack),然后查看是否没有得到您想要的结果。

尝试新事物:

尝试将您的不起作用更改为:

if(...){  
    ...  
}else{  
    string someVariableAsString = "myValue123";  
    MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);  
}

Because you are attempting to act on the INIT rather than the load, the data has not yet been attached at the server.

You should find this review of the life cycle of a web request in ASP.NET useful: MSDN ASP.NET Page Life Cycle

Here is the relevant extract:

Initialization

 During page initialization, controls on the page are available and

each control's UniqueID property is
set. A master page and themes are also
applied to the page if applicable. If
the current request is a postback, the
postback data has not yet been loaded
and control property values have not
been restored to the values from view
state.

 Load

 During load, if the current request is a postback, control 

properties are loaded with information
recovered from view state and control
state.

Move the code you are trying to execute into (or after) the page load handler (remember to test for IsPostBack) and see if that doesn't get what you want.

Something New to try:

try changing your doesn't work to:

if(...){  
    ...  
}else{  
    string someVariableAsString = "myValue123";  
    MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);  
}
地狱即天堂 2024-11-09 16:35:12

听起来 someVariableAsString 可能会引发异常,导致代码无法到达下一行。检查你的变量类型。

It sounds like someVariableAsString is possibly throwing an exception to cause the code not to reach the next line. Check your variable type.

紫南 2024-11-09 16:35:12

我能够为我的案例找到解决方案:
我将 someVariableAsString 更改为属性作为视图。
创建了一个 Gobal.asax 会话变量,现在我得到了正确的结果(稍后加载一页)。 :-)

但就我而言这就可以了。

问题解决了。

onInit{  
m_myVariable;  
myFunction();  
... 
}  

void myFunction(){   
// if clause described up  
}  

public View myVariable  
{  
get { return m_myVariable = Session["myVariableAtSession"] as View; }  
set { m_myVariable = value;  
 Session["myVariableAtSession"] = m_myVariable;  
 }  
} 

I was able to get a solution to my case:
I changed someVariableAsString to a Property as View.
Created a session variable to Gobal.asax and now I get correct result (one page load later). :-)

but in my case this will do.

Problem solved.

onInit{  
m_myVariable;  
myFunction();  
... 
}  

void myFunction(){   
// if clause described up  
}  

public View myVariable  
{  
get { return m_myVariable = Session["myVariableAtSession"] as View; }  
set { m_myVariable = value;  
 Session["myVariableAtSession"] = m_myVariable;  
 }  
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文