DataList.Visible property

D

Daves

now if I in my codebehind set the Visible property to false for a DataList
control which is bound to a database query (AccessDataSource control to be
exact) - will it be skipped completely in runtime or is the database still
accessed and the results iterated although nothing is visible?
 
J

Johann MacDonagh

I assume you're referring to the Whidbey CTP drops. One downside to the RAD
that these DataSource controls bring is lack of complete control. The
DataSource controls will pull the data from the source no matter if it will
be displayed or not. This is useful, because you may set the Visible
property to True again in the code-beside file.

However, if you're sure that you won't, here's a solution that you can try:

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
Sub AccessDataSource1_Selecting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
If Not DataList1.Visible Then
e.Cancel = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" Runat="server" DataKeyField="ID"
DataSourceID="AccessDataSource1">
<ItemTemplate>
ID:
<asp:Label Text='<%# Eval("ID") %>' Runat="server"
ID="IDLabel">
</asp:Label><br />
Test:
<asp:Label Text='<%# Eval("Test") %>' Runat="server"
ID="TestLabel">
</asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" Runat="server"
DataFile="~/Data/Data.mdb" SelectCommand="SELECT * FROM [Table1]"
OnSelecting="AccessDataSource1_Selecting">
</asp:AccessDataSource>
</div>
</form>
</body>
</html>

Basically, before the AccessDataSource control gets the data, it checks to
see if the control is visible. If not, then it sets the Cancel property of e
to True. This means the AccessDataSource control won't pull any data.

You can also use this for other situations such as not wanted the data to
bind on the initial page load, or not wanted it to bind on each post back.
Simply change the condition in the If statement.

Happy coding,
Johann MacDonagh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top