尽管 sqldatasource 有值,但 gridview 为空 [英] empty gridview although the sqldatasource has values

查看:20
本文介绍了尽管 sqldatasource 有值,但 gridview 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况真的很奇怪.我创建了新的 aspx 页面,并且没有使用任何自定义逻辑对象(使用 Visual Studios 向导创建的所有内容)尝试从 sqldatasource 创建网格视图.

数据来自存储过程,带有默认值的单个参数.当我刷新架构或单击测试查询"时,我看到正确创建了结果行和 GridViews 字段.但是当我运行页面时没有网格视图(它只是空的 - 当我添加 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="服务器"><title>无标题页</title><身体><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>无可用数据</EmptyDataTemplate></asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure"><选择参数><asp:Parameter DefaultValue="val1" Name="par1" Type="String"/><asp:Parameter Name="val2" Type="Int32"/></选择参数></asp:SqlDataSource>

</表单>

和代码隐藏

使用系统;使用 System.Collections;使用 System.Configuration;使用 System.Data;使用 System.Linq;使用 System.Web;使用 System.Web.Security;使用 System.Web.UI;使用 System.Web.UI.HtmlControls;使用 System.Web.UI.WebControls;使用 System.Web.UI.WebControls.WebParts;使用 System.Xml.Linq;命名空间站点{公共部分类测试:System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){//在这里刹车}protected void GridView1_RowCreated(对象发送者,GridViewRowEventArgs e){//在这里刹车}protected void GridView1_DataBinding(对象发送者,EventArgs e){//在这里刹车}protected void GridView1_DataBound(object sender, EventArgs e){//在这里刹车}}}

解决方案

从您最近的评论看来,当从 发出 SelectCommand 时,SQL Profiler 似乎没有显示任何活动SqlDataSource.这可能是因为 ConvertEmptyStringToNullSelectParameters 集合中包含的参数上默认设置为 true.此外,默认情况下,SqlDataSource 上的 CancelSelectOnNullParameter 设置为 true.这意味着您的 'val2' 参数可能正在传递一个 NULL 值,这反过来会取消数据检索操作.这就是您在 SQL Profiler 中看不到任何活动的原因.

尝试将 SqlDataSource 上的 CancelSelectOnNullParameter 设置为 false.

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

I have really weird situation. I've created new aspx page, and without using ANY custom logic objects (everything created with visual studios wizards) tried to create grid view from sqldatasource.

The data comes from stored procedure, with single parameter which has default value. when I refresh the schema or click "test query", I see result rows and GridViews fields are corectly created. But when I run the page there is no grid view (it's simply empty - when I add EmptyDataTemplate it is shown) . I've added custom (empty) function and DataBind, DataBinded and RowCreted event, and only databind and datavound events are fired (although, as I wrote - the stored procedure with its default parameter return rows and .net can read them in design mode)

There isn't anything "fancy" in the procedure, I've done this more than once with no problem. I've tried another stored procedure wich works in our production env and still got the same emty gridview

here is the code

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

and the codebehind

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

        }
    }
}

解决方案

From your recent comments it looks as though SQL Profiler isn't showing any activity when the SelectCommand is issued from the SqlDataSource. This could be due to the fact that ConvertEmptyStringToNull is set to true by default on Parameters contained in the SelectParameters collection. Also, by default, the CancelSelectOnNullParameter on the SqlDataSource is set to true. This means that your 'val2' parameter is probably passing a NULL value, which in turn cancels the data retrieval operation. This is why you don't see any activity within SQL Profiler.

Try setting CancelSelectOnNullParameter to false on the SqlDataSource.

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

这篇关于尽管 sqldatasource 有值,但 gridview 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆