Solved

Nested Datalist Visibility in .NET

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
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 …
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now