Thursday, December 29, 2011

How to create CollapsiblePanelExtender Dynamically in asp.net

In Default.aspx page



 <style type="text/css">
        .cpHeader
        {
            color: white;
            background-color: #719DDB;
            font: bold 11px auto "Trebuchet MS" , Verdana;
            font-size: 12px;
            cursor: pointer;
            width: 450px;
            height: 18px;
            padding: 4px;
        }
        .cpBody
        {
            background-color: #DCE4F9;
            font: normal 11px auto Verdana, Arial;
            border: 1px gray;
            width: 450px;
            padding: 4px;
            padding-top: 7px;
        }
    </style>





c#



using AjaxControlToolkit;
    protected void Page_Load(object sender, EventArgs e)
    {        
        // Create Header Panel
        Panel panelHead = new Panel();
        panelHead.ID = "pH";
        panelHead.CssClass = "cpHeader";
        // Add Label inside header panel to display text
        Label lblHead = new Label();
        lblHead.ID = "lblHeader";
        panelHead.Controls.Add(lblHead);
        //Create Body Panel
        Panel panelBody = new Panel();
        panelBody.ID = "pB";
        panelBody.CssClass = "cpBody";
        // Add Label inside body Panel to display text
        Label lblB = new Label();
        lblB.ID = "lblBody";
        lblB.Text = "This panel was added dynamically";
        panelBody.Controls.Add(lblB);
        // Create CollapsiblePanelExtender
        CollapsiblePanelExtender cpe =
            new CollapsiblePanelExtender();
        cpe.TargetControlID = panelBody.ID;
        cpe.ExpandControlID = panelHead.ID;
        cpe.CollapseControlID = panelHead.ID;
        cpe.ScrollContents = false;
        cpe.Collapsed = true;
        cpe.ExpandDirection =
        CollapsiblePanelExpandDirection.Vertical;
        cpe.SuppressPostBack = true;
        cpe.TextLabelID = lblHead.ID;
        cpe.CollapsedText = "Click to Show Content..";
        cpe.ExpandedText = "Click to Hide Content..";
        this.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelHead);
        this.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelBody);
        this.UpdatePanel1.ContentTemplateContainer.Controls.Add(cpe);
    }
VB.NET
Imports AjaxControlToolkit
      Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs)
            ' Create Header Panel
            Dim panelHead As New Panel()
            panelHead.ID = "pH"
            panelHead.CssClass = "cpHeader"
            ' Add Label inside header panel to display text
            Dim lblHead As New Label()
            lblHead.ID = "lblHeader"
            panelHead.Controls.Add(lblHead)
            'Create Body Panel
            Dim panelBody As New Panel()
            panelBody.ID = "pB"
            panelBody.CssClass = "cpBody"
            ' Add Label inside body Panel to display text
            Dim lblB As New Label()
            lblB.ID = "lblBody"
            lblB.Text = "This panel was added dynamically"
            panelBody.Controls.Add(lblB)
            ' Create CollapsiblePanelExtender
            Dim cpe As New CollapsiblePanelExtender()
            cpe.TargetControlID = panelBody.ID
            cpe.ExpandControlID = panelHead.ID
            cpe.CollapseControlID = panelHead.ID
            cpe.ScrollContents = False
            cpe.Collapsed = True
            cpe.ExpandDirection = CollapsiblePanelExpandDirection.Vertical
            cpe.SuppressPostBack = True
            cpe.TextLabelID = lblHead.ID
            cpe.CollapsedText = "Click to Show Content.."
            cpe.ExpandedText = "Click to Hide Content.."
            Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelHead)
            Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelBody)
            Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(cpe)
      End Sub

No comments:

Post a Comment