当 SqlDataSource 为空时隐藏/禁用 ASP.NET 控件
当 SqlDataSource 不返回行时,我需要能够隐藏或禁用多个项目(详细信息视图、网格视图)。因此,如果重新发布页面并且未选择任何行,则所有控件都将被禁用。
我该怎么做?
谢谢
I need to be able to hide or disable a multitude of items (detailsviews, gridviews) when an SqlDataSource returns no rows. So if the page is reposted and no rows are selected, all the controls would be disabled.
How would I do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试挂钩GridView的DataBound事件。如果没有行,请将 Visible=false 设置为需要隐藏的所有控件。例子:
You can try to hook to the DataBound event of GridView. If there are no rows, set Visible=false to all the controls you need to hide. Example:
我通常使用 LINQ 并将结果包装在集合或字典中,但据我所知,您可以通过向
.Selected
事件(前提是您的DataSourceMode
是SqlDataSourceMode.DataSet
。如果您在该事件中检查SqlDataSourceStatusEventArgs
的AffectedRows
属性,它应该告诉您返回了多少行(位于至少,文档暗示了这一点)。一旦您知道了这一点,您就可以继续禁用或启用其他页面控件。
I normally use LINQ and wrap my results in a Collection or Dictionary, but as best as I can tell, you can do this by adding a new handler to the
.Selected
event (provided that yourDataSourceMode
isSqlDataSourceMode.DataSet
. If you check theAffectedRows
property of theSqlDataSourceStatusEventArgs
in that event, it should tell you how many rows were returned (at least, the documentation implies as much).Once you know that, you can proceed to disable or enable your other page controls.