1

In a parentclass I call a function defined in the child class and parse the values I need through.

ParentClass.ascx

protected void Page_Load 
{       
  if(info != null) 
    ControlIWantToGetInformationTo.SetInfo(info);  
}  

ChildClass.ascx

public void SetInfo(Info info)  
{  
  someTextBox.Text = info.TheVariableWithin.ToString(); 
}  

What I can gather is that that ParentClass(control) loads and does the method, but when the ChildClass(control) page loads it resets the previously set variable to null how can I work around this?

4
  • How are you setting the text value inside the control? You have to make sure the code is run in the correct order, but this can be very complex in ASP.Net webforms due to the complex page lifecycle rules. Commented Aug 19, 2011 at 10:41
  • Ahh sounds like the issue I am having :'( are you saying I have to break down everything??? Commented Aug 19, 2011 at 10:43
  • 1
    A quick fix could be to move the code you currently have in Page_Load to the PreRender event, which occurs very late in the lifecycle. But this is not a very good solution, as it breaks down as soon as you have to rely on using PreRender inside the control too. Commented Aug 19, 2011 at 10:47
  • Would like to avoid +1 because solution does work. Commented Aug 19, 2011 at 11:14

2 Answers 2

2

Use Session. In your method, instead of setting the values of your controls, use an object and fill the properties of your object and save it to Session when you are done. In your childclass, load your values from the object which you saved into Session.

//Parentclass
protected void Page_Load 
{       
  if(info != null) 
  {
    MyControlObject myObj = new MyControlObject();
    myObj.prop1 = txt1.Text;
    myObj.prop2 = txt2.Text;
    Session["myObj"] = myObj;
  }
} 

//Childclass
public void SetInfo(Info info)  
{  
  MyControlObject myObj = Session["myObj"] as MyControlObject;
  if(myObj != null)
  {
    //assign the values to your controls
    Session["myObj"] = null; //when you are done, clear the session.
  }
}  
Sign up to request clarification or add additional context in comments.

3 Comments

How do I destroy session once its been created and used??
Thanks +1 because solution works, will wait till GMT +0 midnight time before I accept just incase someone answer with an answer avoiding sessions and PreRender Event.
I had the same problem and would love to see a better solution than this one. This was what I did.
0

I think you are facing problem for case sensitivity.

try this

someTextBox.Text = info.TheVariableWithin.ToString(); 

1 Comment

Thats just a typo sorry fixing in question now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.