【问题标题】:C# Display full data table instead of only search inputC# 显示完整的数据表而不是仅搜索输入
【发布时间】:2011-12-20 20:10:47
【问题描述】:

我正在使用 VS2005 C# 和 SQL Server 2005。

目前我可以使用 sql 语句 SELECT * from table 中的数据源来显示数据。

现在我已经在我的表上实现了一个搜索功能,它将根据用户的输入显示搜索结果。

但是,我无法将数据表的默认显示设置为默认或搜索文本框为空时列出所有数据。

我正在关注本指南:http://www.asp.net/data-access/tutorials/displaying-data-with-the-objectdatasource-cs,我基本上被困在他为他的数据表列表添加 if-else 语句的最后部分,我不知道在哪里更改我的:(

下面是代码和截图:

RPList.aspx.cs:

<%@ Page Language="C#" MasterPageFile="~/MainPage.master" AutoEventWireup="true" CodeFile="RPList.aspx.cs" Inherits="SimpleDisplay" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    Role: <asp:TextBox ID="RPbyRoleTB" runat="server"></asp:TextBox>
    <asp:Button ID="RPbyRoleBtn" runat="server" Text="Show Roles & Processes" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:<connection> %>" SelectCommand="SELECT * FROM [RolesProcess] WHERE ([Role] = @Role)">
        <SelectParameters>
            <asp:ControlParameter ControlID="RPbyRoleTB" Name="Role" PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

<script language="javascript" type="text/javascript">
// <!CDATA[

// ]]>
</script>

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" DataSourceID="SqlDataSource1">
    </asp:GridView>
</asp:Content>

我需要怎么做才能在我的页面加载或搜索框为空时默认显示完整的数据表?


根据 Politia 的建议,我尝试将查询更改为 SELECT [columns] FROM RolesProcess WHERE (Role = @Role) OR (LEN(@Role) = 0),但是,正如我所评论的那样,它没有似乎没有我想象的那么顺利。以下是截图。


更改sql查询后页面的代码:

<%@ Page Language="C#" MasterPageFile="~/MainPage.master" AutoEventWireup="true" CodeFile="RPList.aspx.cs" Inherits="SimpleDisplay" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SODConnectionString %>" SelectCommand="SELECT [columns] FROM RolesProcess WHERE (Role = @Role) OR (LEN(@Role) = 0) OR (@Role IS NULL)">
        <SelectParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="Role" PropertyName="Text" Type="String" DefaultValue="" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:TextBox ID="TextBox1"  runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
<script language="javascript" type="text/javascript">
// <!CDATA[

// ]]>
</script>

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" DataSourceID="SqlDataSource1">
    </asp:GridView>
</asp:Content>

查询在配置期间全部工作。现在,当我加载页面时,表格不会出现,即使我搜索“空白”值,表格也不会显示任何数据。

页面加载时的图像,即使我点击了按钮,也没有任何显示。

【问题讨论】:

    标签: c# sql-server visual-studio sql-server-2005 visual-studio-2005


    【解决方案1】:

    这就是为什么我主要推荐使用 ObjectDataSource 和您自己的自定义适配器,它们可以包含此逻辑并且更易于测试。

    尽管如此,使用这种方法,您必须在查询中反映您的愿望。您可以将参数的默认值设置为 %,例如:

    <asp:ControlParameter ControlID="RPbyRoleTB" Name="Role" PropertyName="Text" DefaultValue="%" Type="String" />
    

    或者更新您的查询以反映空字符串和/或空值。

    SELECT *
    FROM table
    WHERE Role = @role
    OR LEN(@role) = 0
    

    【讨论】:

    • 嗨,Polity,我已经尝试了您的第一个解决方案并且没有工作。对于第二种解决方案,我遇到了一些问题。当我在“定义自定义语句或存储过程”的“查询生成器”中配置数据源时,我可以在查询
    • @RUiHAO - 我现在无法测试这个问题,所以有点猜测:我认为 (param)Role 可以是 NULL 而不是空字符串。请将 OR ((param)Role IS NULL) 添加到您的查询中
    • 我已经添加了 OR (@Role IS NULL),即使输入是现在,“测试查询”现在也能够显示数据。但是,当我的 web 加载时,该表仍然无法从中显示默认的完整数据库。我在上面插入了我的最新代码。非常感谢
    • @RUiHAO - 该死的,要是我能自己测试一下就好了:)无论如何。由于测试查询执行良好,我们可以假设,这解决了问题。现在为什么它在 ASP.NET 中不起作用,我不完全确定。你能分析一下 SqlDataSource 生成的查询吗(提示:code.google.com/p/sqlexpressprofiler
    • 如何分析 SqlDataSource 生成的查询? o.o
    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2020-06-06
    相关资源
    最近更新 更多