【发布时间】: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