【问题标题】:Rehash of a dynamic Data source for a gridview网格视图的动态数据源的重新散列
【发布时间】:2016-06-23 20:51:42
【问题描述】:

我很高兴看到有人在哪里用 sqldatasource 做我想做的事,所以我根据我的目的对其进行了调整,但是虽然我可以毫无错误地运行它,但无论是否在文本中输入数据,我也没有得到任何数据盒子。

这是一个链接到启发我的线程 Reference link for what I used: 你会注意到巴黎的答案是我改编的:

我已经尝试了两种技术,结果相同,所以我真的很困惑。 这是我的aspx代码:

     <asp:SqlDataSource ID="InventoryList" runat="server" ConnectionString='<%$ ConnectionStrings:CMDB_testConnectionString %>' SelectCommand="SELECT [AssetID], [AssetType], [AssetName], [AssetShortDesc], [AssetLongDesc], [AssetAddNotes], [AssetManuf], [AssetModel], [AssetTag], [AssetSerialNum], [AssetAcqDate], [AssetDTAssetID], [AssetLocGrp], [AssetLoc1], [AssetLoc2], [AssetLoc3], [AssetParent], [AssetStatus], [AssetPropType], [AssetPrimUser], [AssetEntered], [AssetEnteredBy], [AssetOwner], [AssetCompany], [AssetPriIPAddr], [AssetPriMACAddr], [AssetPriOS], [AssetPriOSSP], [AssetNotes], [AssetAdminGrp], [AssetOrgID], [AssetOperType], [AssetOperStatus] FROM [cmdb_assets] 
     WHERE [AssetName] = CASE @AssetName THEN [AssetName] END AND [AssetType] = CASE @AssetType THEN [AssetType] END AND [AssetManuf] = CASE @AssetManuf THEN [AssetManuf END AND  [AssetModel] = CASE @AssetModel THEN [AssetModel] END">

接着是:

        <SelectParameters>
        <asp:ControlParameter Name="AssetName" ControlID="AssetNameTbx" Type="String" />
       <asp:ControlParameter Name="AssetType" ControlID="AssetTypeTbx" Type="String" />
       <asp:ControlParameter Name="AssetManuf" ControlID="AssetManufTbx" Type="String" />
       <asp:ControlParameter Name="AssetModel" ControlID="AssetModelTbx" Type="String" />
    </SelectParameters> 
    </asp:SqlDataSource>

正如第一个线程响应者所追求的那样,我想要做的是允许在一个或多个文本框中输入信息,然后通过此选择将结果过滤到我的网格视图中。 想法? 肯...

我想我会提供我上面使用的两个示例的结果的输出,以及我刚刚尝试由另一个论坛成员在 2014 年测试提供的最新结果,该成员也做了相同的 nodata 返回结果。只是为了看结果。真的没有价值,但仍然。 肯... 数据源最终是什么:

  <asp:SqlDataSource ID="InventoryList" runat="server" ConnectionString='<%$ ConnectionStrings:CMDB_testConnectionString %>' SelectCommand="SELECT [AssetID], [AssetType], [AssetName], [AssetShortDesc], [AssetLongDesc], [AssetAddNotes], [AssetManuf], [AssetModel], [AssetTag], [AssetSerialNum], [AssetAcqDate], [AssetDTAssetID], [AssetLocGrp], [AssetLoc1], [AssetLoc2], [AssetLoc3], [AssetParent], [AssetStatus], [AssetPropType], [AssetPrimUser], [AssetEntered], [AssetEnteredBy], [AssetOwner], [AssetCompany], [AssetPriIPAddr], [AssetPriMACAddr], [AssetPriOS], [AssetPriOSSP], [AssetNotes], [AssetAdminGrp], [AssetOrgID], [AssetOperType], [AssetOperStatus] FROM [cmdbv_Assets_CInTrac] where AssetID=isnull(@AssetID,AssetID) and AssetName=isnull(@AssetName,AssetName) and AssetType=isnull(@AssetType,AssetType) and AssetManuf=isnull(@AssetManuf,AssetManuf) and AssetModel=isnull(@AssetModel,AssetModel) and AssetTag=isnull(@AssetTag,AssetTag) and AssetSerialNum=isnull(@AssetSerialNum,AssetSerialNum) and AssetDTAssetID=isnull(@AssetDTAssetID,AssetDTAssetID) and AssetLocGrp=isnull(@AssetLocGrp,AssetLocGrp) and AssetLongDesc=isnull(@AssetLongDesc,AssetLongDesc) and AssetOrgID=isnull(@AssetOrgID,AssetOrgID) and AssetPriIPAddr=isnull(@AssetPriIPAddr,AssetPriIPAddr) and AssetStatus=isnull(@AssetStatus,AssetStatus)" CancelSelectOnNullParameter="false">

【问题讨论】:

  • 所以,我很清楚,您基本上有一个包含多个字段的网格,并且您有一些文本框,如果它们有值则应该过滤,如果没有则不过滤。对吗?
  • 您在[AssetManuf 中没有];是复制粘贴错误吗?
  • @techspider 复制粘贴错误更正 ] 在代码中。是的。
  • 是的,目标是提供一系列文本框,这些文本框可能会或可能不会填充各种字段的过滤文本。如果留空,则应将其视为有效 * 对于该列允许任何内容通过而不是过滤任何内容。因此,如果您在没有文本的情况下将其拉出,您将看到表中的所有记录。如果您在“资产类型”中输入笔记本电脑,您将只获得笔记本电脑的列表。如果您随后将 HP 添加到 Asset Mfg 框中,您将只能获得 HP 笔记本电脑。

标签: c# asp.net gridview


【解决方案1】:

如果我了解您的需求,这似乎可行。使用 Adventureworks 2014 (sql server) 测试。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorks2014ConnectionString %>"
        SelectCommand="SELECT [Name], [ProductID], [Color], [Size], [ProductNumber], [Style] FROM Production.[Product]
        where name=isnull(@ProductName,name) and Color=isnull(@Color,Color) and ProductNumber=isnull(@ProductNumber,ProductNumber)" CancelSelectOnNullParameter="false">
        <SelectParameters>
            <asp:ControlParameter Name="ProductName" ControlID="ProductName" Type="String" ConvertEmptyStringToNull="true" />
            <asp:ControlParameter Name="Color" ControlID="Color" Type="String" ConvertEmptyStringToNull="true" />
            <asp:ControlParameter Name="ProductNumber" ControlID="ProductNumber" Type="String" ConvertEmptyStringToNull="true" />
        </SelectParameters>
    </asp:SqlDataSource>
    <form id="form1" runat="server">
        <div>
            Product Name:
            <asp:TextBox ID="ProductName" runat="server"></asp:TextBox>
        </div>
        <div>
            Color:
            <asp:TextBox ID="Color" runat="server"></asp:TextBox>
        </div>
        <div>
            Product Number:
            <asp:TextBox ID="ProductNumber" runat="server"></asp:TextBox>
        </div>
        <div><asp:Button ID="submit" runat="server" Text="filter" /></div>
        <div>
            <asp:GridView runat="server" DataSourceID="SqlDataSource1"></asp:GridView>
        </div>
    </form>
</body>
</html>

【讨论】:

  • 我在 2008 年,但会试一试,看看进展如何......我有机会测试后会报告。
  • 完全没有错误,但没有返回任何数据,就像其他两个示例一样。几乎就像没有从文本框到查询一样。将对此进行一些调试,看看是否有什么东西阻止了它。
  • 请确保您检查是否有 isnull 检查以及控件设置为 ConvertEmptyStringToNull (true) 和查询设置为 CancelSelectOnNullParameter (false)
  • 在 where 和 中使用 AssetModel=isnull(@AssetModel,AssetModel) selectParameters .... 之后我还需要添加 CancelSelectOnNullParameter="false" 吗?
  • 可以,否则在参数为空时不会实际运行查询(它与selectCommand相关联,而不是与参数相关联)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 2014-10-11
相关资源
最近更新 更多