如何仅在 SQLDataSource 有行时显示元素
我正在创建一个 ASP.NET/C# 页面,用户在页面顶部输入 ID,当他们按“提交”时,页面会发回并显示数据。现在,数据字段始终显示 - 即使在页面加载且未进行搜索时也是如此。这并不理想,因为当他们使用我制作的多视图导航菜单时,会出现一些错误。而且这显然不是一个好的做法。
如果 SQLDataSource 为空,有没有办法隐藏所有这些元素(它们是DetailsViews 和GridViews,加上MultiView 和Menu)?因此,如果搜索没有返回结果,或者尚未执行任何查询。
谢谢
I'm creating an ASP.NET/C# page where a user enters an ID at the top of the page, and when they press Submit, the page posts back and displays the data. Right now, the data fields are displayed at all times - even when the page loads and no search has taken place. This is not ideal as when they navigate a Menu with a MultiView I have made, some errors will come up. Plus it's obviously not good practise.
Is there a way I can hide all these elements (they are DetailsViews and GridViews, plus the MultiView and Menu) if the SQLDataSource is blank? So if the search returned nor esults, or no query has been executed yet.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以用占位符控件包围这些元素,然后根据是否有要显示的结果设置占位符的可见性。
我以前没有使用过 SqlDataSource 对象,但为了满足您的要求,我建议检查页面是否已在 PageLoad 方法中回发,以及是否隐藏数据控件。然后,为了处理没有返回结果的情况,向 SqlDataSource.Selected 事件添加一个方法:
ASPX:
隐藏代码:
You could surround these elements is a placeholder control and then set the placeholders visibility depending on whether there are result to display.
I haven't used the SqlDataSource object much before but to meet you requirements I would suggest checking to see if the page has been posted back in the PageLoad method and if not hiding the data controls. Then to handle the case of no results being returned adding a method to the SqlDataSource.Selected event:
ASPX:
Code behind: