Solved

Nested Datalist Visibility in .NET

Posted on 2008-10-31
5
537 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

776 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