Solved

Nested Datalist Visibility in .NET

Posted on 2008-10-31
5
541 Views
Last Modified: 2012-05-05
Hi,
I am trying to hide a datalist (it's DataList3 in the code snippet) if empty. The problem is that it is a nested datalist and I cannot reference it in code behind. I thought to set "visible" property to false in case it is empty....but unsuccessfully.

Any idea?
<asp:DataList ID="DataList2" runat="server"
                CellPadding="4"
                DataSourceID="SqlDataSource2"
                Font-Names="Verdana"
                Font-Size="8pt"
                HeaderStyle-BackColor="#FFFFFF"
                AlternatingItemStyle-BackColor="#FFFFFF" ForeColor="#333333" RepeatDirection="Horizontal"
                >
                  <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                  <AlternatingItemStyle BackColor="#FFFFFF"></AlternatingItemStyle>
 
                  <ItemStyle BackColor="#FFFFFF" />
                  <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 
                  <HeaderStyle BackColor="#FFFFFF"></HeaderStyle>
                    <ItemTemplate>
                      
                             <asp:HyperLink ID="hlUrl" runat="server" NavigateUrl='<%# FormatUrl( (string) Eval("languageVersion"), (int) Eval("reportID") ) %>' Text = '<%# Eval("languageVersion") %>' />
                             <asp:Label ID="sizeLbl" runat="server" Text='<%# Eval("FileSize") + "MB" %>' />
                           
                    </ItemTemplate>
        </asp:DataList>
              
                      <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:eodocumentsConnectionString %>" 
                        SelectCommand='<%# Eval("reportID", "SELECT * FROM dbo.FileTable WHERE reportID = {0} AND relatedToID = 0 AND GEFProjID = 0") %>'>
                        </asp:SqlDataSource>
                        </td></tr>
                        <tr>
              <td valign="bottom">
                        <asp:DataList id="DataList3" runat="server
                                Visible='<%# IfNoRowsSomehowFalse %>'
                                CellPadding="4"
                                DataSourceID="SqlDataSource3"
                                Font-Names="Verdana"
                                Font-Size="8pt"
                                HeaderStyle-BackColor="#FFFFFF"
                                AlternatingItemStyle-BackColor="#FFFFFF" ForeColor="#333333" RepeatDirection="Horizontal"
                                >
                                  <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                                  <AlternatingItemStyle BackColor="#FFFFFF"></AlternatingItemStyle>
 
                                  <ItemStyle BackColor="#FFFFFF" />
                                  <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 
                                  <HeaderStyle BackColor="#FFFFFF" Font-Bold="True"></HeaderStyle>
                                  <HeaderTemplate>
                                    Related Documents
                                  </HeaderTemplate>
                                    <ItemTemplate>
                                      
                                             <asp:HyperLink ID="hlUrl" runat="server" NavigateUrl='<%# FormatUrl( (string) Eval("languageVersion"), (int) Eval("reportID") ) %>' Text = '<%# Eval("FileName") %>' />
                                             <asp:Label ID="sizeLbl" runat="server" Text='<%# Eval("FileSize") + "MB" %>' />
                                           
                                    </ItemTemplate>
                        </asp:DataList>
              
                      <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:eodocumentsConnectionString %>" 
                        SelectCommand='<%# Eval("reportID", "SELECT * FROM dbo.FileTable WHERE relatedToID = {0}") %>'>
                        </asp:SqlDataSource>
                        </td></tr>

Open in new window

0
Comment
Question by:sabritahiri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Assisted Solution

by:lunadl
lunadl earned 75 total points
ID: 22868067
DataList2.FindControl("datalist3")
I'm pretty sure that is how it works for finding a control nested within another  control. From here you should assign it to an object. something like this...
Datalist dtl = (Datalist2.FindControl("Datalist3") as Datalist);

left me know if you have syntax problems.
0
 

Author Comment

by:sabritahiri
ID: 22966250
I tried to use this but it doesn't find the control...at least I cannot use it. I use dtl.Visible = false; but the control still shows up...thanks for your comments, looks like the right path but not a solution yet.
0
 
LVL 2

Expert Comment

by:x0040h
ID: 23026244
Are you sure, that "IfNoRowsSomehowFalse" variable has right value? Code below works. Maybe the problem is in another place.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web.UI._Default" %>
<script type="text/C#" runat="server">
    public bool ShowData {
        get {
            return false;
        }
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
                <asp:DataList 
                    ID="DataList3" runat="server" CellPadding="4" DataSourceID="SqlDataSource4"
                    Visible='<%#ShowData%>'
                    Font-Names="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#FFFFFF" AlternatingItemStyle-BackColor="#FFFFFF"
                    ForeColor="#333333" RepeatDirection="Horizontal">
                    <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                    <AlternatingItemStyle BackColor="#FFFFFF"></AlternatingItemStyle>
                    <ItemStyle BackColor="#FFFFFF" />
                    <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#FFFFFF" Font-Bold="True"></HeaderStyle>
                    <HeaderTemplate>
                        Related Documents
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:HyperLink ID="hlUrl" runat="server"
                            Text='<%# Eval("Publication") %>' />
                        <asp:Label ID="sizeLbl" runat="server" Text='<%# Eval("Publication") + "MB" %>' />
                    </ItemTemplate>
                </asp:DataList>
                
    </div>
    <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PortalConnectionString %>" 
        SelectCommand="SELECT [Publication], [Url], [PreviewUrl], [UID] FROM [ContentImages]">
    </asp:SqlDataSource>
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:sabritahiri
ID: 23223387
x0040h,
My question was about the child datalist. You are using the code on only one datalist (the top level) which is pretty straightforward...I know you have to find the list first and then manipulate it...however, FindControl is not doing it....most probably i am going somewhere wrong...don't know where though.
0
 

Accepted Solution

by:
sabritahiri earned 0 total points
ID: 23234597
Just in case someone else has the same problem. You can refer to the controls inside a datalist, including another datalist using FindContol(). However, you have to account for different cases when ItemDataBound event is rased, by checking e.Item.ItemType. Below is my solution:
<asp:DataList id="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"
                CellPadding="4"
                Font-Names="Verdana"
                Font-Size="8pt"
                HeaderStyle-BackColor="#FFFFFF"
                AlternatingItemStyle-BackColor="#FFFFFF" ForeColor="#333333" 
                >
 
...
...
...
and the code behind:
 
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
 
            DataList dtlistRelated;
 
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                dtlistRelated = (DataList)e.Item.FindControl("DataList3");
                if (dtlistRelated.Items.Count == 0)
                {
                    dtlistRelated.Visible = false;
                }
            }
        }

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question