views:

184

answers:

1

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?

A: 

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 Name is a public property any control that has access to it could set it to null at any point prior to your call to GetCalendarString. Also if you are calling GetCalendarString before ViewState is loaded this value will not be available. I suspect that you are trying to get Name from ViewState before 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 ViewState before it is loaded from the Request.

Andrew Hare
Strange. I've just debugged it and Name field inside a function wasn't null.Then I rendered the Name property, and it wasn't null either.Don't know what happened.Anyway, thank you all for your help and time.
lopkiju