1

我有一个本地数据库表,它显示在GridView. 在该表中,我有一列名为:“已完成”。

当我第一次显示表格时,我在显示它时没有完成记录(Completed=false)。这是SqlDataSource选择命令:

"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,即使上面的代码执行,它也不会更新。我错过了什么?

4

3 回答 3

2

尝试在块后使用您GridView的 sDataBind方法if...else

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 语句所做的更改。

于 2013-03-14T14:24:32.197 回答
1

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>

这将根据复选框的“选中”属性修改您的选择命令。

于 2013-03-14T14:22:52.567 回答
-1

如果 Completed 是 DB 中的位类型,请将您的查询更改为:

"SELECT * FROM [CERecord] WHERE [Completed]=0 ORDER BY [Priority]";
于 2013-03-14T14:23:44.693 回答