ASP.. NET数据源选择命令

本文关键字:命令 选择 数据源 NET ASP | 更新日期: 2023-09-27 18:03:48

我有一个显示在GridView中的本地DB表。在这个表中,我有一个列叫做:"Completed"。

当我第一次显示该表时,我显示它没有完成的记录(completed =false)。下面是SqlDataSource select命令:

"SELECT * FROM [CERecord] WHERE [Completed]='false' ORDER BY [Priority]";

我有一个复选框导致回发。我想通过勾选/取消勾选来切换已完成记录的显示:

protected void cbShowCompletedRecords_CheckedChanged(object sender, EventArgs e)
    {
        if (cbShowCompletedRecords.Checked)
            CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] ORDER BY [Priority]";
        else
        {
            CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] WHERE [Completed]='false' ORDER BY [Priority]";
        }
    }

当前,当我选中复选框时,我将获得所有记录。但是当我取消选中它时,即使上面的代码执行,GridView也不会更新。我错过了什么?

ASP.. NET数据源选择命令

if...else块之后尝试使用GridViewDataBind方法

protected void cbShowCompletedRecords_CheckedChanged(object sender, EventArgs e)
    {
        if (cbShowCompletedRecords.Checked)
            CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] ORDER BY [Priority]";
        else
        {
            CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] WHERE [Completed]='false' ORDER BY [Priority]";
        }
        yourGrid.DataBind();
    }

这将确保网格中显示的结果反映了您对select语句所做的更改。

SqlDataSource允许您绑定参数来控制属性:

<asp:SqlDataSource ID="myds" runat="server" 
  SelectCommand="SELECT * FROM [CERecord] WHERE [Completed]=:COMPLETED ORDER BY [Priority]">
  <SelectParameters>
    <asp:ControlParameter ControlID="MyCheckbox" Name="COMPLETED" PropertyName="Checked" Type="Boolean" />
  </SelectParameters>
</asp:SqlDataSource>

这将根据你的复选框的"Checked"属性修改你的select命令

如果Completed是位类型在DB更改您的查询:

"SELECT * FROM [CERecord] WHERE [Completed]=0 ORDER BY [Priority]";