如何仅在 SQLDataSource 有行时显示元素

发布于 2024-09-26 07:17:38 字数 269 浏览 4 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2024-10-03 07:17:38

您可以用占位符控件包围这些元素,然后根据是否有要显示的结果设置占位符的可见性。
我以前没有使用过 SqlDataSource 对象,但为了满足您的要求,我建议检查页面是否已在 PageLoad 方法中回发,以及是否隐藏数据控件。然后,为了处理没有返回结果的情况,向 SqlDataSource.Selected 事件添加一个方法:
ASPX:

<asp:PlaceHolder ID="myPlaceholder" runat="server">
     ....Data controlds
</asp:PlaceHolder>

隐藏代码:

protected void Page_Load(object sender, EventArgs e)
{
    if(!isPostBack)
    {     
        myPlaceholder.Visible = false;
    }
}

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    myPlaceholder.Visible = e.AffectedRows > 0;
}

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:

<asp:PlaceHolder ID="myPlaceholder" runat="server">
     ....Data controlds
</asp:PlaceHolder>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if(!isPostBack)
    {     
        myPlaceholder.Visible = false;
    }
}

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    myPlaceholder.Visible = e.AffectedRows > 0;
}
困倦 2024-10-03 07:17:38
If GridView1.Rows.Count > 0 // then do stuff
If GridView1.Rows.Count > 0 // then do stuff
谁与争疯 2024-10-03 07:17:38
DataView dv = (DataView)SqlDSourse.Select(DataSourceSelectArguments.Empty);

if(dv.count>0)

    {
     ...
    }
DataView dv = (DataView)SqlDSourse.Select(DataSourceSelectArguments.Empty);

if(dv.count>0)

    {
     ...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文