list view pagging and condtrol dynamicaly

//Put into body of the page



    <tr class="dividerLine">
        <td height="299" colspan="2" align="center" valign="top">
            <br />
            <table width='930' height='232' border='0' align='center' class='border'>

            <tr>
            <td align="right" style="padding-right: 20px;" class="smallTxt" colspan="2">
            <table > <tr>
            <td  > <asp:LinkButton ID="lnkbtnPrevious" runat="server" OnClick="lnkbtnPrevious_Click"><b>«</b></asp:LinkButton></td>
            <td >
            <asp:DataList ID="DataList2" runat="server"   CellPadding="1" CellSpacing="5" OnItemCommand="DataList2_ItemCommand"
                                            OnItemDataBound="DataList2_ItemDataBound" RepeatDirection="Horizontal">
                                            <ItemTemplate>
                                           
                                                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                                    CommandName="lnkbtnPaging" CssClass="page_index" Text='<%# Eval("PageText") %>'></asp:LinkButton>
                                          
                                            </ItemTemplate>
                                        </asp:DataList></td>
            <td><asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click"><b>»</b></asp:LinkButton></td>
            </tr></table>
           
            </td>
            </tr>

                <tr>
                    <td height='20' valign='bottom'>
                        <div align='left' class='bodyTxt' style="padding-left: 25px;">
                            <asp:Label ID="lblname" runat="server" Text="By Artist" ></asp:Label>
                        </div>
                    </td>
                </tr>
              
                <tr>
                    <td>
                        <!-- DataList with images -->
                        <asp:DataList ID="dtlArtistGallery" RepeatDirection="Horizontal" RepeatColumns="5"
                            runat="server" onitemdatabound="dtlArtistGallery_ItemDataBound"
                            CssClass="photolist" >
                            <ItemTemplate>

                                <div class="smallTxt">
                                    <asp:LinkButton  runat="server" id="imgTitle" CommandName="linkPhoto" ToolTip='<%# Eval("title") +"(©"+Eval("copyright")+")"%>' class="smallTxt" PostBackUrl='<%#"by_artist.aspx?id="+ Eval("id") %>'>
                                        <img height="130" runat="server" id="img" width="130" alt='<%# Eval("title") %>' src='<%# "~/i/thumb/" + Eval("thumb_image") %>'
                                             class="border"/></asp:LinkButton>
                                             <p>
                                             <asp:LinkButton runat="server" id="lblTitle" CommandName="linkPhoto" ToolTip='<%# Eval("title") +"(©"+Eval("copyright")+")"%>'  PostBackUrl='<%#"by_artist.aspx?id="+ Eval("id") %>'>
                                        <asp:Label ID="lbltitlebottom" Text='<%# Eval("copyright") %>' runat="server"></asp:Label></asp:LinkButton>
                                             </p>
                                             <asp:HiddenField ID="hdnTitle" Value='<%#Eval("title") %>' runat="server" />
                                      </div>
                            </ItemTemplate>
                        </asp:DataList>
                   

                    </td>
                </tr>
                <tr>
                    <td height='20' valign='middle'>
                        &nbsp;
                    </td>
                </tr>
                 <tr>
            <td align="right" style="padding-right: 20px;" class="smallTxt" colspan="5">
            <table > <tr>
            <td> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="lnkbtnPrevious_Click">«</asp:LinkButton></td>
            <td> <asp:DataList ID="DataList1" runat="server"   CellPadding="1" CellSpacing="5" OnItemCommand="DataList2_ItemCommand"
                                            OnItemDataBound="DataList2_ItemDataBound" RepeatDirection="Horizontal">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                                    CommandName="lnkbtnPaging" CssClass="page_index" Text='<%# Eval("PageText") %>'></asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:DataList></td>
            <td><asp:LinkButton ID="LinkButton2" runat="server" OnClick="lnkbtnNext_Click"><b>»</b></asp:LinkButton></td>
            </tr></table>
           
            </td>
            </tr>

           

                <tr>
                    <td width="80%" align="center" class="my-data-list">

                   
                                   
                        <!-- DataList with Names -->
                        <asp:DataList ID="dtltitle" RepeatDirection="Horizontal" RepeatColumns="3" CellPadding="6" CellSpacing="1"
                            runat="server">
                            <ItemTemplate>

                                <div style="height:10px;">
                                    <a title='<%# Eval("fname") %>' class="bodyTxt" href='<%#"by_artist.aspx?id="+ Eval("id") %>'>
                                        <asp:Label ID="lbltitlebottom" Text='<%#Eval("fname")+" "+Eval("lname") %>' runat="server"></asp:Label></a></div>
                            </ItemTemplate>
                        </asp:DataList>
                       

                    </td>
                </tr>

               


            </table>
        </td>
    </tr>




//Source code use



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
public partial class by_artist1 : System.Web.UI.Page
{
    DataSet dsFeaturedArtists = new DataSet();
    DataTable dtArtistTitle = new DataTable();
    static int count = 0;
    int count1 = 0;
   
    PagedDataSource pds = new PagedDataSource();

    public int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                dsFeaturedArtists = Artists.GetFeaturedArtists();
                dtArtistTitle = Artists.GetArtists();
                dtlArtistGallery.DataSource = dsFeaturedArtists.Tables[0];
                dtlArtistGallery.DataBind();
                dtltitle.DataSource = dtArtistTitle;
                dtltitle.DataBind();
                lnkbtnNext.Visible = false;
                lnkbtnPrevious.Visible = false;
                LinkButton1.Visible = false;
                LinkButton2.Visible = false;
            }
        }

        if (!string.IsNullOrEmpty(Request.QueryString["id"])) 
          
       {
          
               Session["last_page"] = "by_artist";
               Session["last_id"] = Request.QueryString["id"];
                dtArtistTitle = Artists.GetArtist(long.Parse(Request.QueryString["id"]));
                dsFeaturedArtists = Artists.GetActiveDisplayArtistPhotos(long.Parse(Request.QueryString["id"]));
                dtltitle.Visible = false;
                if(dtArtistTitle.Rows.Count!=0)
                lblname.Text = Convert.ToString(dtArtistTitle.Rows[0]["display_name"]);
                Bindlist();
           
       }

          
    

       
       
    }


   
    protected void Bindlist()
    {
       
       



        count = dsFeaturedArtists.Tables[1].Rows.Count;
        DataView dv = dsFeaturedArtists.Tables[1].DefaultView;
       
        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.PageSize = 40;
        pds.CurrentPageIndex = CurrentPage;
        lnkbtnNext.Enabled = !pds.IsLastPage;
        lnkbtnPrevious.Enabled = !pds.IsFirstPage;
        LinkButton1.Enabled = !pds.IsLastPage;
        LinkButton2.Enabled = !pds.IsFirstPage;
       
        lnkbtnNext.Visible = !pds.IsLastPage;
        lnkbtnPrevious.Visible = !pds.IsFirstPage;
        LinkButton2.Visible = !pds.IsLastPage;
        LinkButton1.Visible = !pds.IsFirstPage;
        dtlArtistGallery.DataSource = pds;
        dtlArtistGallery.DataBind();

      
        doPaging();

    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");

        for (int i = 0; i < pds.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        DataList2.DataSource = dt;
        DataList2.DataBind();
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }

    protected void dtlArtistGallery_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if(!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
        Label lblimgTitle = (Label)e.Item.FindControl("lbltitlebottom");
        HiddenField hdnimgTitle = (HiddenField)e.Item.FindControl("hdnTitle");
        LinkButton lnkbtnimage = (LinkButton)e.Item.FindControl("imgTitle");
        LinkButton lnkbtntitle = (LinkButton)e.Item.FindControl("lblTitle");
        string strlnkimage = lnkbtnimage.PostBackUrl;
        lnkbtnimage.PostBackUrl=strlnkimage.Replace("by_artist.aspx?id=", "photo.aspx?id=");
        lnkbtntitle.PostBackUrl=strlnkimage.Replace("by_artist.aspx?id=", "photo.aspx?id=");
        hdnimgTitle.Value = hdnimgTitle.Value.Replace("&", "");
        hdnimgTitle.Value = hdnimgTitle.Value.Replace("#", "");

        int strLen2 = hdnimgTitle.Value.Length;
        if (strLen2 > 30)
        {
            hdnimgTitle.Value = hdnimgTitle.Value.Substring(0, 30);
            lblimgTitle.Text = hdnimgTitle.Value + "...[ ]";
        }
        else
        {
            lblimgTitle.Text = hdnimgTitle.Value;
        }
       
        }

        LinkButton lblimgTitle1 = (LinkButton)e.Item.FindControl("imgTitle");
        string strimgToolTip = Convert.ToString(lblimgTitle1.ToolTip);

        strimgToolTip = strimgToolTip.Replace("&", "");
        strimgToolTip = strimgToolTip.Replace("#", "");

        int strLen = strimgToolTip.Length;
        if (strLen > 50)
        {
            strimgToolTip = strimgToolTip.Substring(0, 50);
            lblimgTitle1.ToolTip = strimgToolTip.ToString() + "...[ ]";
        }
        else
        {
            lblimgTitle1.ToolTip = strimgToolTip.ToString();
        }

        LinkButton lbltxtTitle = (LinkButton)e.Item.FindControl("lblTitle");
        string strimgToolTip1 = Convert.ToString(lbltxtTitle.ToolTip);

        strimgToolTip1 = strimgToolTip1.Replace("&", "");
        strimgToolTip1 = strimgToolTip1.Replace("#", "");

        int strLen1 = strimgToolTip1.Length;
        if (strLen1 > 50)
        {
            strimgToolTip1 = strimgToolTip1.Substring(0, 50);
            lbltxtTitle.ToolTip = strimgToolTip1.ToString() + "...[ ]";
        }
        else
        {
            lbltxtTitle.ToolTip = strimgToolTip1.ToString();
        }

    }


    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        Bindlist();
    }
    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        Bindlist();
    }
    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            Bindlist();
        }
    }
    protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemIndex == CurrentPage)
        {

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


        }
   
    }

  

}


No comments:

Post a Comment