Hiding/disabling ASP.NET controls when SqlDataSource is empty
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.
精彩评论