The Artima Developer Community
Sponsored Link

.NET Buzz Forum
A simple collapsable list user control for list output

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Steve Hebert

Posts: 218
Nickname: sdhebert
Registered: Apr, 2005

Steve Hebert is a .NET developer who has created the .Math compiler library.
A simple collapsable list user control for list output Posted: Apr 17, 2006 2:39 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Steve Hebert.
Original Post: A simple collapsable list user control for list output
Feed Title: Steve Hebert's Development Blog
Feed URL: /error.htm?aspxerrorpath=/blogs/steve.hebert/rss.aspx
Feed Description: .Steve's .Blog - Including .Net, SQL Server, .Math and everything in between
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Steve Hebert
Latest Posts From Steve Hebert's Development Blog

Advertisement
I decided to blog this collapsable list after digging it up recently for another project.  The idea is very simple and cleans up processing lists that may result in long/unsightly output on a web page. The code is very simple and straight-forward, no AJAX here - just complete client-side management of the list display.  I've removed all style references - apply the styles as you see fit. The summary list is printed using a count parameter, therefore the following style string can be handed to it:

ListDrillDown1.DescriptionText = "{0} Processing Errors";

This results in the following style of output:


Upon clicking the [+] sign, the actual messages are revealed. 



The [-] sign can then be clicked again to hide the content.

Note that the control is coded to allow 1..n instances on a given page.  If there aren't any items on the list, nothing is shown on the resulting .aspx page.

Now for the code...

First, we have the HTML for the User Control.  Note the use of the server side <DIVs> that enable multiple collapsable list controls per page.


<table>
    <tr>
        <td>
            <div id="fullcontent" style="DISPLAY: none" runat="server"><IMG src="minus.png"></div>
            <div id="mincontent" style="DISPLAY: block" runat="server"><IMG src="plus.png"></div>
       
        </td>
        <TD>
            <asp:Label id="lblDescription" runat="server">Description</asp:Label></TD>
    </tr>
</table>
<DIV id="tableContent" style="DISPLAY: none" runat="server">
    <asp:Table id="tblList" runat="server"></asp:Table></DIV>


And here is the code-behind...

public class ListDrillDown : System.Web.UI.UserControl

{

    protected System.Web.UI.WebControls.Label lblDescription;

    protected System.Web.UI.WebControls.Table tblList;

    protected System.Web.UI.HtmlControls.HtmlGenericControl fullcontent;

    protected System.Web.UI.HtmlControls.HtmlGenericControl mincontent;

 

    protected System.Web.UI.HtmlControls.HtmlGenericControl tableContent;

    public string DescriptionText;

 

 

    private void Page_Load(object sender, System.EventArgs e)

    {

        if( tblList.Rows.Count == 0 )

        {

            Visible = false;

            return;

        }

 

        Visible = true;

        SetupScript();

 

        lblDescription.Text = string.Format( DescriptionText, tblList.Rows.Count);

 

        fullcontent.Attributes.Add("onclick", "javascript:ShowContent(false, " + fullcontent.ClientID+", "+mincontent.ClientID+", "+tableContent.ClientID+");");

        mincontent.Attributes.Add("onclick", "javascript:ShowContent(true, " + fullcontent.ClientID +", "+ mincontent.ClientID+", "+tableContent.ClientID+");");

    }

 

    private void SetupScript()

    {

        if( ! this.Page.IsClientScriptBlockRegistered("ListDrillDown.ascx"))

            Page.RegisterClientScriptBlock("ListDrillDown.ascx", GenerateScript());

 

    }

 

    private string GenerateScript()

    {

        StringBuilder sb = new StringBuilder();

 

        sb.Append("<SCRIPT language='javascript'>");

        sb.Append("function ShowContent( show, fullId, minId, tableContentId )");

        sb.Append("{");

        sb.Append("if( show == true )");

        sb.Append("{");

        sb.Append("fullId.style.display ='block';");

        sb.Append("minId.style.display='none';");

        sb.Append("}");

        sb.Append("else");

        sb.Append("{");

        sb.Append("fullId.style.display='none';");

        sb.Append("minId.style.display='block';");

        sb.Append("}");

        sb.Append("tableContentId.style.display = fullId.style.display;");

        sb.Append("}");

        sb.Append("</SCRIPT>");

   

        return sb.ToString();

    }

 

    public void AddLine( string line )

    {

        TableRow tr = new TableRow();

        TableCell tc = new TableCell();

        tc.Text = line;

        tr.Cells.Add( tc);

        tblList.Rows.Add( tr );

 

    }

   

}

I've always wished that ASP.NET would allow for a <SCRIPT> attribute directly in the ASCX file that would allow you to register it as a once/page scriptlet on a web user control.  That would certainly beat spewing the script in the page-behind and using the RegisterClientScriptBlock(...) call.





Share this post: Email it! | bookmark it! | digg it! | reddit!

Read: A simple collapsable list user control for list output

Topic: Contract first > WSDL first Previous Topic   Next Topic Topic: XML and LINQ papers by Erik

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use