C# MultiView SetActiveView by ID not working properly
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";
开发者_StackOverflow社区 MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);
}
.. why and any solutions for this?
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);
}
It sounds like someVariableAsString is possibly throwing an exception to cause the code not to reach the next line. Check your variable type.
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; } }
精彩评论