GridView is empty

本文关键字:empty is GridView | 更新日期: 2023-09-27 17:49:21

我有一个页面(其中有一个MasterPage)与GridView和SqlDataSource。如果我在标记中设置SelectCommand,则GridView显示良好。但如果我动态生成相同的SelectCommand, GridView是空的。我知道查询被执行了。原因是什么呢?谢谢。

<asp:SqlDataSource ID="UserSqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:BookList %>" DataSourceMode="DataSet" 
    ProviderName="<%$ ConnectionStrings:BookList.ProviderName %>" >
</asp:SqlDataSource>  
        <asp:GridView ID="grdUsers" runat="server" AllowPaging="True" ShowHeader="false" ShowFooter="true" 
            AutoGenerateColumns="false"
            Width="1480px" Height="100%" PageSize="50" DataSourceID="UserSqlDataSource" DataKeyNames="ID">
                <AlternatingRowStyle CssClass="alternatingrowstyle" />
                <Columns>
                     <asp:TemplateField HeaderText="User Name" SortExpression="Name">
                        <ItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
                        <ItemTemplate>
                            <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>                        
                    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
                        <ItemTemplate>
                            <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Email" SortExpression="Email">
                        <ItemTemplate>
                            <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>                        
                </Columns>
            </asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindUsers();
    }
}
protected void BindUsers()
{
    string strSelectClause = 
        @"SELECT ID, Name, FirstName, LastName, EMail, 
      FROM User WHERE Name NOT IN ('*All', 'Admin', 'TPS') ";
    string strQuery = strSelectClause + GenerateWhereClause() + " ORDER BY Name;";
    SqlDataSource UserSqlDataSource = GetControl(this, "UserSqlDataSource") as SqlDataSource;
    UserSqlDataSource.SelectCommand = strQuery;
    grdUsers.DataBind();
}

GridView is empty

用户是保留关键字,您试过了吗:

SELECT ID, Name, FirstName, LastName, EMail, 
      FROM [User] WHERE Name NOT IN ('*All', 'Admin', 'TPS') ";

从SqlDataSource控件的MSDN文档中:

注意
默认情况下,如果在执行Select命令时其中一个参数为null,则不会返回任何数据,也不会抛出异常。您可以通过将CancelSelectOnNullParameter属性设置为false来更改此行为。

当我从GridView的标记中删除DataSourceID并添加grdUsers。DataSource = UserSqlDataSource;在代码后面。

也许您在页面生命周期中设置SelectCommand太晚了?设置SelectCommand后是否调用grdUsers.DataBind() ?