开发者

Hiding/disabling ASP.NET controls when SqlDataSource is empty

开发者 https://www.devze.com 2023-01-20 11:39 出处:网络
I need t开发者_Python百科o 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

I need t开发者_Python百科o 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


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:

<asp:gridView id="control" OnDataBound="DataBound" [...]

protected void DataBound(object sender, EventArgs e)
    {
        control.Visible = control.Rows.Count>0;
    }


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 your DataSourceMode is SqlDataSourceMode.DataSet. If you check the AffectedRows property of the SqlDataSourceStatusEventArgs 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消