-2

I have some labels in the Masterpage as well as in the child pages. the values in all these labels come from a single DB Query.I want to run this query once so that the labels in the Master and Child gets populated. Is there a way for this?

This is the most simplified version of my question

Here is how I Initialize the public properties in the page_load of Master page (I use viewstate so that other methods can also use these properties)

 protected void Page_Load(object sender, EventArgs e)
{
Meeting = bal.GetMeetingByID(MeetingID)[0];//DB QUERY

}

The properties in the page load are as follows

 public Meeting1 Meeting
{
    set
    {
        ViewState["Meeting"] = value;
    }
    get
    {
        if (ViewState["Meeting"] != null)
            return (Meeting1)ViewState["Meeting"];
        else
            return null;
    }
} private int meetingID = -1;
public int MeetingID
{
    get
    {
        if (meetingID == -1)
            try
            {
                meetingID = int.Parse(Request.QueryString["meetingID"]);
            }
            catch (Exception)
            {
                Response.Redirect("~/Main.aspx");

            }
        return meetingID;
    }
}

Now in the child page I have

protected void Page_Load(object sender, EventArgs e)
{        
    if (IsPostBack)
        return;       
    DataBindControls();
}
  private void DataBindControls()
{
    tbMeetingTitle.DataBind();            
}

the Meeting.aspx page is like this

  <tr>
        <td>Meeting Title</td>
        <td colspan="3">
            <asp:TextBox ID="tbMeetingTitle" runat="server" Width="714px" Text='<%# GetData("MeetingTitle") %>' AutoPostBack="true" ></asp:TextBox></td>

    </tr>

I hope now my question is clear.

Shomaail
  • 493
  • 9
  • 30
  • The fact that you want to do this implies that you have deeper problems in your design. You should use viewstate only sparingly (if at all). Why do you need to run one query on every page? – mason Mar 17 '15 at 14:38
  • there are many function other than the page_load of masterpage that uses these properties. therefore I hav to use viewstate in the properties. Secondly, I hav a quick jump in the masterpage that loads new meeting on every new selection. there fore I need to run the query Meeting = bal.GetMeetingByID(MeetingID)[0];//DB QUERY on every load of the master page – Shomaail Mar 18 '15 at 07:12
  • This is an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You asked about a solution you had in mind rather than describing the problem. If for some reason you need to load a query for every page, there's better locations for that. You can run a query at the beginning of each request using functions in `global.asax`. Or you could put the query earlier in the lifestyle by putting it in a base page that all your other pages inherit from. – mason Mar 18 '15 at 12:41

2 Answers2

1

No, you can not change the order, but there is a way to specify a strongly typed model in your child pages, or you can do a cast in your child pages to reference any public properties of the master page.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • if I include Meeting = bal.GetMeetingByID(MeetingID)[0];//DB QUERY in the page load of the child pages it works but then this query is called twice ... – Shomaail Mar 18 '15 at 07:16
  • Are you sure that you don't have an image tag or script tag or something similar on the page that you didn't set the src attribute on that is making the page try and render itself multiple times? An img, script, and I believe link tags will cause the client to request the page again to use as it's source, which will fail, but you might not notice it failing if it's in an obscure place. – Robert McKee Mar 18 '15 at 17:02
  • the query is called twice once in the child page and once in the master page. since I have to set some labels in the master page using this query. What I was asking was that is there a way If I can run this query once and both the labels in the master page and child pages gets the values. – Shomaail Mar 19 '15 at 07:46
  • 1
    Of course you can. Make a property in the master page, and always call that from both places. In the property get, if the backing field is null, run the query and set the backing field to the result. Then return the backing property. – Robert McKee Mar 19 '15 at 16:38
0

No it cannot be done. However on a side note, your question is not clear. From what i understand, you are facing some performance issues. I would suggest reviewing your application design. Applying practices like catching. the below link helped when i was working on asp.net applications

http://www.codeproject.com/Articles/23306/ASP-NET-Performance-and-Scalability-Secrets

user3545750
  • 51
  • 12