views:

395

answers:

3

I am generating some head html in page load and because of that I query database once. in the page I query database again and put data into html with inline code.

my question is is there better way to do this? I dont want to query database everytime and reach out those filled variables from inline code. something like page.addVariable in page_load and reach those at inline like page.variables["variablename"]

thanks in advance

+3  A: 

If I understand what you are asking, you can make an accessor and set it to Protected. That will allow you to access it from the page.

If you want to prevent calling the database on callbacks, you could always add the information to the view state on the page.

Information on the view state, hidden fields, and cookies: http://www.csharphelp.com/archives/archive207.html

Kevin
I think viewstate is the best way to go because its all about that page not session or applicaion. when I tried to add and get ProfileCommon from viewstate it throws System.Runtime.Serialization.SerializationException: Type 'ProfileCommon' in Assembly 'App_Code._-aako5a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. exception?
özkan pakdil
it's a serialization issue, I'll try and find a link that talks about it and have it up in a little while.
Kevin
+1  A: 

I'm not sure if this is what you're after, but you can use a HiddenField to store any data you want on the page.

Also, if you don't need it to be on the page, you can use Session or ViewState.

Here's an example of using ViewState as a property (NB. you can interchange ViewState with Session, look at the links I gave you for an explanation between the two):

public string YourProperty
{
    get
    {
        object content = ViewState["YourProperty"];
        if (content == null)
        {
            return string.Empty;
        }
        return content.ToString();
    }
    set
    {
        ViewState["YourProperty"] = value;
    }
}

Note, that anything you put into ViewState or SessionState must be marked as Serializable.

If it's quite a simple class, just mark the class with the [Serializable] tag.

GenericTypeTea
This won't work as object != string. Better do something like 'return (string)(ViewState["YourProperty"] ?? "")'
Jan Jongboom
Yes it will. ViewState is returned as an object. I prefer to do a comparison on the object returned first to ensure there's a value. That way I never have to rely on setting the value before I can use it/check it. Perhaps using the ?? symbol would clean up the code slightly, but it's one of those symbols I've never been a fan of.
GenericTypeTea
@Jan, every object has a ToString method.
Jim Schubert
A: 

Is the data you retrieve from the database page specific, user specific or global to the entire application?

If the data is user specific you can use Session State.

If the data is global to the entire application you could use Application State.

Whichever you use, you can implement the data retrieval in the Session_Start (will be called only once for each user) or Application_Start (will be called only once when the web app starts) events in a Global.asax file.

Marc
its about just that page and viewstate is the best way to go. but now I get exception about ProfileCommon is not serializable :)
özkan pakdil