0

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

1
  • Can you get a code snippet for us? Commented Nov 11, 2009 at 13:28

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
it's a serialization issue, I'll try and find a link that talks about it and have it up in a little while.
1

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.

3 Comments

This won't work as object != string. Better do something like 'return (string)(ViewState["YourProperty"] ?? "")'
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.
@Jan, every object has a ToString method.
0

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.

1 Comment

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

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.