Search code examples
asp.netdatabound-controls

Bind data without database call on page load


I'm trying to find a best practice for getting values for a databound control. The way my application works now (and usually) is on page load I call a getSettings() or something and that makes a database call for each of the DropDownLists or Repeaters.

In this particular occasion I bind data to a DropDownList depending on which Button is clicked. There is a delay before the data shows up while the app goes to the database and gets the required information.

I'm wondering if there is a better way to handle getting the values, perhaps without making a database call each time the page is loaded. I've included some example code just in case.

.aspx:

<asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>
<asp:Button ID="btn1" runat="server" OnCommand="btn_Command" CommandName="change" CommandArgument="table1" />
<asp:Button ID="btn2" runat="server" OnCommand="btn_Command" CommandName="change" CommandArgument="table2" />
<asp:Button ID="btn3" runat="server" OnCommand="btn_Command" CommandName="change" CommandArgument="table3" />
<asp:Button ID="btn4" runat="server" OnCommand="btn_Command" CommandName="change" CommandArgument="table4" />

codebehind:

DBClass dbc = new DBClass();

protected void btn_Command (object sender, CommandEventArgs e){
    if (e.CommandName == "change"){
        ddl.DataSource = dbc.ExecuteQuery("SELECT * FROM " + e.CommandArgument);
        ddl.DataTextField = "Text";
        ddl.DataValueField = "Value";
        ddl.DataBind();
    }
}

Is there any way to cache the results? I know this one is rough since it's only one DropDownList, but even if there is one for each button, is there some way I could get the data without making 4 database calls?

Thanks in advance.


Solution

  • Found a way to do this using jQuery ajax. I click a button client side and it spits out the values I need formatted with some html server side in a WebMethod. A little more work, but removes all postbacks and loading up the ones I won't need or use.

    Thanks everyone!