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.

有帮助吗?

解决方案 3

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!

其他提示

You could change your query to return multiple result sets, and populate the results into a DataSet. Once the DataSet is loaded, you can bind each DataTable in the DataSet to the corresponding DropDown.

You can also cache the results or put them in application state if you need to. I would still look into merging the query though.

You could simply store those values in Application variables and retrieve them only once on Application_Startup. See some simple examples from Microsoft.

Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    DBClass dbc = new DBClass();
    Application["table1"] = dbc.ExecuteQuery<TResult>("SELECT * FROM table1");
    Application["table2"] = dbc.ExecuteQuery<TResult>("SELECT * FROM table2");
    // ...
}

codebehind

protected void btn_Command (object sender, CommandEventArgs e)
{
    if (e.CommandName == "change")
    {
        ddl.DataSource = Application[e.CommandArgument] as IEnumerable<TResult>;
        ddl.DataTextField = "Text";
        ddl.DataValueField = "Value";
        ddl.DataBind();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top