I am working on some ASP.NET Server Control, and I have an issue. Maybe I oversee something, I don’t know.
Anyway:
public string Name { get { String s = (String)ViewState['name']; return ((s == null) ? String.Empty : s); } set { ViewState['name'] = value; } } protected override void RenderContents(HtmlTextWriter output) { txt.ID = Name; // Name here exists txt.Text = DateTime.Now.ToShortDateString(); txt.RenderControl(output); output.Write(someName(someValue)); } public string GetCalendarString(string date) { some code... // Name property is null }
‘RenderContents’ uses property ‘Name’ to set the control name and then calls ‘someName’ function and ‘someName’ function also uses property ‘Name’, but when I run it, property ‘Name’ inside function ‘someName’ is empty, although in ‘RenderContents’ it is not.
Gremlins, or I’m missing something?
This is going to be tough to answer because without debugging there will be no way to tell what outside forces have manipulated the data.
Since
Nameis a public property any control that has access to it could set it tonullat any point prior to your call toGetCalendarString. Also if you are callingGetCalendarStringbeforeViewStateis loaded this value will not be available. I suspect that you are trying to getNamefromViewStatebefore it has loaded but again this is a problem best solved by debugging.By the way – here is a great image that shows the ASP.NET life cycle and will help you figure out if you are trying to use
ViewStatebefore it is loaded from theRequest.