c# - 空 gridview 虽然 sqldatasource 有值

标签 c# asp.net gridview sqldatasource

我的情况真的很奇怪。 我创建了新的 aspx 页面,并且在不使用任何自定义逻辑对象(使用 visual studios 向导创建的所有内容)的情况下尝试从 sqldatasource 创建 GridView 。

数据来自存储过程,参数单一,有默认值。当我刷新架构或单击“测试查询”时,我看到结果行和 GridViews 字段已正确创建。但是当我运行该页面时,没有 GridView (它只是空的 - 当我添加 EmptyDataTemplate 时它会显示)。我添加了自定义(空)函数和 DataBind、DataBinded 和 RowCreted 事件,并且只触发了 databind 和 datavound 事件(尽管,正如我所写的那样——带有默认参数返回行的存储过程和 .net 可以在设计模式下读取它们)

程序中没有任何“花哨”的东西,我已经不止一次这样做了,没有任何问题。我已经尝试了另一个在我们的生产环境中工作的存储过程,但仍然得到相同的 emty gridview

这是代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TEST.aspx.cs" Inherits="site.TEST" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
            AllowSorting="True" OnDataBinding="GridView1_DataBinding" OnDataBound="GridView1_DataBound"
            OnRowCreated="GridView1_RowCreated">
            <EmptyDataTemplate>
                No Data Available
            </EmptyDataTemplate>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
            SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DefaultValue="val1" Name="par1" Type="String" />
                <asp:Parameter Name="val2" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

和代码隐藏

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace site
{
    public partial class TEST : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {//brake here

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {//brake here

        }

        protected void GridView1_DataBinding(object sender, EventArgs e)
        {//brake here

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {//brake here

        }
    }
}

最佳答案

从您最近的评论来看,当 SelectCommand 出现时,SQL Profiler 似乎没有显示任何事件。从 SqlDataSource 发出.这可能是因为 ConvertEmptyStringToNull SelectParameters 中包含的参数默认设置为 true收藏。此外,默认情况下,CancelSelectOnNullParameter SqlDataSource 上的设置为true。这意味着您的“val2”参数可能传递了一个 NULL 值,这反过来又取消了数据检索操作。这就是您在 SQL 事件探查器中看不到任何事件的原因。

尝试设置 CancelSelectOnNullParameterSqlDataSource上为假.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"> </asp:SqlDataSource>

关于c# - 空 gridview 虽然 sqldatasource 有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1749728/

相关文章:

c# - 将 IEnumerable 分组为一个字符串

c# - 客户关系管理 2011 : How to find out the origin of a trigger for a plugin?

javascript - 如何将文本框值传递给 JavaScript?

asp.net - 无论如何告诉.net 缓存使用应用程序结构服务器而不是本地内存?

c# - 不会调用 HttpPost

c# - 如何在gridview的rowcommand事件中在新标签中打开一个页面?

Android Espresso 测试卡在 perform(click());

java - Android/Java - 寻求带有textView的拖放GridView示例

c# - 如何为 .NET HttpClient 禁用流水线

C# 普通随机数