Display PDF inside Ajax Accordion inside ASP.Net Repeater control using C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

is it possible to use ajax accordian pane inside repeater control and load the PDF document file inside accordian with literal control.

Please suggest.

You are viewing reply posted by: pandeyism 3 months ago.
Posted 3 months ago Modified on 3 months ago

Hi Amol111,

Refer below sample.

According to your logic you can use and write your pdf code.

Database

For this sample I have used of NorthWind database that you can download using the link given below.

Download Northwind Database

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .header_selected
        {
            border: 1px solid #FF8C00;
            color: #FFF;
            background-color: #FF8C00;
            font-weight: bold;
            padding: 5px;
            margin-top: 5px;
            cursor: pointer;
        }
        .header
        {
            border: 1px solid #89BA06;
            color: #fff;
            background-color: #89BA06;
            font-weight: bold;
            padding: 5px;
            margin-top: 5px;
            cursor: pointer;
        }
        .header a, .header_selected a
        {
            color: #fff;
            text-decoration: none;
        }
        .content
        {
            background-color: #F6F6F6;
        }
        .content a, .content a:visited
        {
            color: #000;
            margin-left: 20px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Repeater ID="rptMenu" runat="server" OnItemDataBound="rptMenu_OnItemDataBound">
            <ItemTemplate>
                <asp:HiddenField ID="hfCountry" Value='<%# Eval("Country") %>' runat="server" />
                <h3>
                    <%#DataBinder.Eval(Container.DataItem, "Country")%>
                    Employees</h3>
                <cc1:Accordion ID="AccordionMenu" runat="server" HeaderCssClass="header" HeaderSelectedCssClass="header_selected"
                    ContentCssClass="content" OnItemDataBound="OnItemDataBound">
                    <HeaderTemplate>
                        <asp:HyperLink ID="lnkMenu" Text='<%# Eval("Name") %>' runat="server" />
                    </HeaderTemplate>
                    <ContentTemplate>
                        <asp:HyperLink ID="HyperLink2" Text='<%# Eval("Address") %>' runat="server" />
                        <hr />
                        <asp:Literal ID="ltEmbed" runat="server" />
                    </ContentTemplate>
                </cc1:Accordion>
                <br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Namespaces

C#

using System.Data;
using AjaxControlToolkit;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient
Imports System.Data
Imports AjaxControlToolkit

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Employees", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    rptMenu.DataSource = dt;
                    rptMenu.DataBind();
                }
            }
        }
    }
}

protected void rptMenu_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HiddenField country = e.Item.FindControl("hfCountry") as HiddenField;

        Accordion accordionMenu = e.Item.FindControl("AccordionMenu") as Accordion;
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "sqlDataSource1";
        e.Item.Controls.Add(SqlDataSource1);
        SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlDataSource1.SelectCommand = "SELECT FirstName + ' ' + LastName Name,Address FROM Employees WHERE Country='" + country.Value + "'";
        accordionMenu.DataSourceID = "SqlDataSource1";
    }
}

protected void OnItemDataBound(object sender, AccordionItemEventArgs e)
{
    if (e.ItemType == AccordionItemType.Content)
    {
        AccordionContentPanel cPanel = e.AccordionItem;
        Literal ltEmbed = (Literal)cPanel.FindControl("ltEmbed");
        string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"200px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
        embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
        embed += "</object>";
        ltEmbed.Text = string.Format(embed, ResolveUrl("~/Files/Test.pdf"));
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Employees", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    rptMenu.DataSource = dt
                    rptMenu.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub

Protected Sub rptMenu_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim country As HiddenField = TryCast(e.Item.FindControl("hfCountry"), HiddenField)
        Dim accordionMenu As Accordion = TryCast(e.Item.FindControl("AccordionMenu"), Accordion)
        Dim SqlDataSource1 As SqlDataSource = New SqlDataSource()
        SqlDataSource1.ID = "sqlDataSource1"
        e.Item.Controls.Add(SqlDataSource1)
        SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
        SqlDataSource1.SelectCommand = "SELECT FirstName + ' ' + LastName Name,Notes FROM Employees WHERE Country='" & country.Value & "'"
        accordionMenu.DataSourceID = "SqlDataSource1"
    End If
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As AccordionItemEventArgs)
    If e.ItemType = AccordionItemType.Content Then
        Dim cPanel As AccordionContentPanel = e.AccordionItem
        Dim ltEmbed As Literal = CType(cPanel.FindControl("ltEmbed"), Literal)
        Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""500px"" height=""200px"">"
        embed += "If you are unable to view file, you can download from <a href = ""{0}"">here</a>"
        embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
        embed += "</object>"
        ltEmbed.Text = String.Format(embed, ResolveUrl("~/Files/Test.pdf"))
    End If
End Sub

Screenshot