C# 显示完整数据表而不是仅搜索输入

发布于 2024-12-13 07:20:41 字数 3505 浏览 0 评论 0原文

我使用的是 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>

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

加载页面时的图像,即使我单击按钮,也没有显示任何内容。 在此处输入图像描述

I am using VS2005 C# and SQL Server 2005.

Currently I am able to display data using datasource from the sql statement SELECT * from table.

Now I have implemented a search function on my table which will display search results as of user's input.

However, I am not able to set the default display of the datatable to list all data by default or when the search textbox is empty.

I am following this guide: http://www.asp.net/data-access/tutorials/displaying-data-with-the-objectdatasource-cs and I am basically stuck at the final part where he puts in an if-else statement for his datatable listng, which I have no idea where to change mine :(

Below are the codes and screenshots:

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>

enter image description here
enter image description here
enter image description here

What do I need to do so that when my page loads or when the search box is empty, it will show the full datatable by default?


As suggested by Politia, I have tried changing my query to SELECT [columns] FROM RolesProcess WHERE (Role = @Role) OR (LEN(@Role) = 0), however, as what I've commented, it doesn't seem to work as smoothly as i thought. Below are the screenshots.

enter image description here
enter image description here


Behind code for page after changing sql query:

<%@ 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>

The queries all works during configuration. Now when I load my page, the tables doesn't appear, and even if I search for 'blank' values, the table does not show any data as well.

Image of page when it loads, and even if I clicked on the button, nothing shows out.
enter image description here

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

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

发布评论

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

评论(1

坚持沉默 2024-12-20 07:20:41

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

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

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

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

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

That's why i mostly recommend using ObjectDataSource and your own custom adaptor which can contain this logic and is more easy to test.

Nontheless, with this method, you do have to reflect your whishes in your query. You can either set the default value of the parameter to % like:

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

Or update your query to reflect empty strings and/or null values.

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