GridView控件是空的 [英] GridView is empty
问题描述
我有一个网页(其中有一个母版)与GridView和将SqlDataSource。如果我在标记的SelectCommand设置,在GridView显示的罚款。但是,如果我动态生成相同的SelectCommand,GridView控件是空的。我知道,执行查询。
什么都可以的原因吧?
谢谢你。
< ASP:SqlDataSource的ID =UserSqlDataSource=服务器
的ConnectionString =<%$的ConnectionStrings:书目%>中DataSourceMode =数据集
的ProviderName =下;%$的ConnectionStrings:BookList.ProviderName%>中>
< / ASP:SqlDataSource的> < ASP:GridView控件ID =grdUsers=服务器AllowPaging =真ShowHeader =假ShowFooter =真
的AutoGenerateColumns =假
WIDTH =1480pxHEIGHT =100%每页=50的DataSourceID =UserSqlDataSource的DataKeyNames =ID>
< AlternatingRowStyle的CssClass =alternatingrowstyle/>
<柱体和GT;
< ASP:的TemplateField的HeaderText =用户名SORTEX pression =名称>
<&ItemTemplate中GT;
< ASP:标签ID =lblName=服务器文本='<%#的eval(姓名)%>'>< / ASP:标签>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =姓SORTEX pression =姓氏>
<&ItemTemplate中GT;
< ASP:标签ID =lblLastName=服务器文本='<%#的eval(姓氏)%>'>< / ASP:标签>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =名SORTEX pression =名字>
<&ItemTemplate中GT;
< ASP:标签ID =lblFirstName=服务器文本='<%#的eval(名字)%>'>< / ASP:标签>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =电子邮件SORTEX pression =电子邮件>
<&ItemTemplate中GT;
< ASP:标签ID =lblEmail=服务器文本='<%#的eval(电子邮件)%>'>< / ASP:标签>
< / ItemTemplate中>
< / ASP:的TemplateField> < /专栏> < / ASP:GridView的>保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(!的IsPostBack)
{
BindUsers();
}
}保护无效BindUsers()
{
串strSelectClause =
@SELECT ID,名称,名字,姓氏,电子邮件,
FROM用户WHERE名称NOT IN('*所有','管理','TPS'); 字符串strQuery = strSelectClause + GenerateWhereClause()+ORDER BY名称;; SqlDataSource的UserSqlDataSource = GetControl(这一点,UserSqlDataSource)作为SqlDataSource的;
UserSqlDataSource.SelectCommand = strQuery;
grdUsers.DataBind();
}
当我从GridView的标记去掉了DataSourceID它的工作,并添加
grdUsers.DataSource = UserSqlDataSource;在code后面。
我不知道为什么它没有工作,这是从前的模样......
I have a page (which has a MasterPage) with a GridView and an SqlDataSource. If I set SelectCommand in the markup, the GridView is displayed fine. But if I generate the same SelectCommand dynamically, the GridView is empty. And I know that the query is executed. What can be the reason for it? Thanks.
<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();
}
It worked when I removed DataSourceID from GridView's markup and added grdUsers.DataSource = UserSqlDataSource; in code behind. I wonder why it didn't work the way it was before...
这篇关于GridView控件是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!