Tuesday, May 8

Repeater control with paging and Dynamic LinkButton to show pdf file

Here is my Source Code:-

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" CodeFile="index.aspx.cs"
    Inherits="index" %>

<!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>Om Namah shivaya </title>
    <h1>
        Shibashish mohanty</h1>
</head>
<body onload="show()">
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    <asp:Repeater ID="result" runat="server" OnItemCreated="result_ItemCreated">
        <HeaderTemplate>
            <table>
                <tr>
                    <td>
                        <asp:LinkButton ID="previous" runat="server" Font-Names="Verdana" Font-Size="8pt"
                            OnClick="previous_Click">Previous</asp:LinkButton>
                    </td>
                    <td>
                        <asp:LinkButton ID="next" runat="server" Font-Names="Verdana" Font-Size="8pt" OnClick="next_Click">next</asp:LinkButton>
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="Label1" Text='<%#Eval("CreatedDate")%>' runat="server" Font-Names="Verdana"
                        Font-Size="8pt"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label2" Text='<%#Eval("MenuName")%>' runat="server" Font-Names="Verdana"
                        Font-Size="8pt"></asp:Label>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <br />
    </form>
</body>
</html>


 Here is my Code Behind:-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;

using System.Data.SqlClient;
using AjaxControlToolkit;
using System.Net;
using System.Collections.Generic;
using System.Linq;

public partial class index : System.Web.UI.Page
{
  
    protected void Page_Load(object sender, EventArgs e)
    {

        
        if (!IsPostBack)
        {
            Application["index"] = 0;
            display();

        }
 LinkButton lKbt = new LinkButton();
                lKbt.ID = "shibashish";
                lKbt.CommandArgument = lKbt.ID;
                lKbt.Command += new CommandEventHandler(lKbt_Command);
                lKbt.Text = "Click here To View Pdf file";
                PlaceHolder1.Controls.Add(lKbt);
    }
    protected void previous_Click(object sender, EventArgs e)
    {


        if ( System.Convert.ToInt32(Application["index"]) >= 1)
        {


            Application["index"] = System.Convert.ToInt32(Application["index"]) - 1;



        }

        display();       

    }
    protected void next_Click(object sender, EventArgs e)
    {
        if (System.Convert.ToInt32(Application["index"]) < System.Convert.ToInt32(Application["count"]))
        {

            Application["index"] = System.Convert.ToInt32(Application["index"]) + 1;


        }
        display();

    }
    public void display()
    {
     
 string connecStr = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connecStr);
        SqlDataAdapter da = new SqlDataAdapter("select * from ProjectMenu  where PrID =1", con);
        DataSet data = new DataSet();
        da.Fill(data);
        PagedDataSource paging = new PagedDataSource();
        paging.DataSource = data.Tables[0].DefaultView;
        paging.AllowPaging = true;
        paging.PageSize = 2;
        Application["count"] = paging.PageCount;

        paging.CurrentPageIndex = System.Convert.ToInt32(Application["index"]);
        Application["current"] = paging.CurrentPageIndex;
        result.DataSource = paging;
        result.DataBind();
    

    }
    protected void result_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
            if (e.Item.ItemType == ListItemType.Header)
            {
                if (System.Convert.ToInt32(Application["current"]) == 0)
                {
       

                ((LinkButton)e.Item.FindControl("previous")).Enabled = false;


                }
                else
                {

                    ((LinkButton)e.Item.FindControl("previous")).Enabled = true;

                }
            }


        if (e.Item.ItemType == ListItemType.Header)
        {
                if (System.Convert.ToInt32(Application["current"]) == System.Convert.ToInt32(Application["count"]) - 1)
                {

                    ((LinkButton)e.Item.FindControl("next")).Enabled = false;


                }
                else
                {

                    ((LinkButton)e.Item.FindControl("next")).Enabled = true;

                }
        }


    }
  
    void lKbt_Command(object sender, CommandEventArgs e)
    {
        //Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('" + e.CommandArgument.ToString() +" "+ "Shibashish Mohanty');</script>");
        //it will show you shibashish.pdf which is stored in DownLoad folder in your solution.
        // Response.Redirect("~/DownLoad/" + e.CommandArgument.ToString() + ".pdf");
        string pdfPath = Server.MapPath("~/DownLoad/" + e.CommandArgument.ToString() + ".pdf");
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(pdfPath);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }
}



 Here is my Solution View:-




Thanks shibashish mohanty


No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty