gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:02:38 25 4
gpt4 key购买 nike

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

目前我可以使用来自表的 SELECT * 语句的数据源来显示数据。

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

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

我正在遵循本指南: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>

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

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


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

enter image description here enter image description here


更改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>

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

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

最佳答案

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

不过,使用这种方法,您必须在查询中反射(reflect)您的意愿。您可以将参数的默认值设置为 % like:

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

或者更新您的查询以反射(reflect)空字符串和/或空值。

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

关于C# 显示完整数据表而不是仅搜索输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7989694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com