页面加载时无需数据库调用即可绑定数据

发布于 2024-12-02 06:04:36 字数 1305 浏览 1 评论 0原文

我正在尝试寻找获取数据绑定控件值的最佳实践。我的应用程序现在(通常)的工作方式是在页面加载时我调用 getSettings() 或其他方法,并对每个 DropDownLists 或 Repeaters 进行数据库调用。

在这种特殊情况下,我根据单击的按钮将数据绑定到 DropDownList。当应用程序访问数据库并获取所需信息时,数据显示之前会出现延迟。

我想知道是否有更好的方法来处理获取值,也许不需要每次加载页面时都进行数据库调用。我已经包含了一些示例代码以防万一。

.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" />

代码隐藏:

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();
    }
}

有什么方法可以缓存结果吗?我知道这个很粗糙,因为它只有一个 DropDownList,但即使每个按钮都有一个,是否有某种方法可以在不进行 4 次数据库调用的情况下获取数据?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月光色 2024-12-09 06:04:36

您可以更改查询以返回多个结果集,并将结果填充到数据集中。一旦DataSet加载完毕,就可以将DataSet中的每个DataTable绑定到对应的DropDown上。

如果需要,您还可以缓存结果或将它们置于应用程序状态。不过,我仍然会考虑合并查询。

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.

救赎№ 2024-12-09 06:04:36

您可以简单地将这些值存储在应用程序变量中,并仅在 Application_Startup 上检索它们一次。请参阅 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");
    // ...
}

代码隐藏

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();
    }
}

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();
    }
}
无人接听 2024-12-09 06:04:36

找到了一种使用 jQuery ajax 来做到这一点的方法。我单击客户端按钮,它会输出我需要使用 WebMethod 中的某些 html 服务器端格式化的值。多做一点工作,但删除所有回发并加载我不需要或使用的回发。

谢谢大家!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文