简体   繁体   English

使用GridView对ItemTemplate进行自定义分页

[英]Custom pagination on itemtemplate using gridview

I want to apply custom pagination to my gridview which contains itemtemplate which has a image that comes from the database 我想对自定义分页应用到我的gridview,其中包含itemtemplate,该模板具有来自数据库的图像

I have tried with clientid mode static but that didnt work out 我已经尝试使用clientid模式为static,但是没有解决

My Procedure:- 我的程序:

   create procedure sp_BookingByPageSize
@pageNo int,
@NoOfRecord int,
@TotalRecord int output
as 
select @TotalRecord =
count(*) from tblDiscount
select * from 
(
select 
Row_number() over
(order by r.bookingid asc)
as RowNo,
r.BookingId,
u.Fullname,
h.hotelName,
case when r.bookingstatus=1 then 'Confirmed'
when r.BookingStatus=0 then 'Cancelled'
end as Booking_Status
,r.Check_In,r.Check_Out,r.NoOfGuests,ro.RoomTypeName,r.NoOfRoomsBooked,r.NationalID,r.Amount from tblReservation as r inner join
tblHotel as h on r.HotelId=h.HotelID 
inner join  tblUser as u 
on u.UId=r.UId inner join tblRooms as ro on ro.RoomsID=r.RoomType)
AS Tab
where tab.RowNo between((@PageNo-1)*@NoOfRecord)+1 and (@PageNo*@NoOfRecord)
order  by 2 asc
return

My aspx file:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" CssClass="table table-condensed" runat="server" OnRowCommand="GridView1_RowCommand" DataKeyNames="bookingid" AutoGenerateColumns="false">

                <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
                <Columns>

                    <asp:ButtonField CommandName="download"
                        ControlStyle-CssClass="btn btn-info" ButtonType="Button"
                        Text="Download Invoice" HeaderText="Invoice" />
                    <asp:BoundField DataField="bookingid" HeaderText="Booking ID" />
                    <asp:BoundField DataField="hotelName" HeaderText="Hotel Name" />
                    <asp:BoundField DataField="Booking_Status" HeaderText="Booking Status" />
                    <asp:BoundField DataField="Check_In" HeaderText="Check In" />
                    <asp:BoundField DataField="Check_Out" HeaderText="Check Out" />
                    <asp:BoundField DataField="RoomTypeName" HeaderText="Room Type" />
                    <asp:BoundField DataField="NoOfRoomsBooked" HeaderText="No of Rooms Booked" />

                    <asp:BoundField DataField="Amount" HeaderText="Amount" />
                    <asp:TemplateField HeaderText="NationalID">
                        <ItemTemplate>
                            <asp:Image ID="Image1" CssClass="default"  runat="server" 
                                ImageUrl='<%# "data:Image/jpg;base64,"
                    + Convert.ToBase64String((byte[])Eval("NationalID")) %>' />
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>
            <asp:HiddenField runat="server" ID="hiddenprimary" />
            <div style="margin-left: 1500px;">
                <asp:Panel ID="Panel1" runat="server"></asp:Panel>
            </div>



MyCode behinde:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HotelBAL;
namespace HotelReservation.Views
{
    public partial class AdminViewBooking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateData(1, 5);
            }
            AddpaggingButton();

        }

        private void PopulateData(int pageNo, int noOfRecord)
        {


            GridView1.DataSource = BookingBal.PopulateData(pageNo, noOfRecord);
            GridView1.DataBind();
            ViewState["TotalRecord"] = BookingBal.getTotalRecord1();
            ViewState["NoOfRecord"] = BookingBal.getNoOfRecord1();

        }

        private void AddpaggingButton()
        {
            int totalRecord = 0;
            int noOfRecord = 0;
            totalRecord = ViewState["TotalRecord"] != null ? (int)ViewState["TotalRecord"] : 0;
            noOfRecord = ViewState["NoOfRecord"] != null ? (int)ViewState["NoOfRecord"] : 0;
            int pages = 0;
            if (totalRecord > 0 && noOfRecord > 0)
            {
                pages = (totalRecord / noOfRecord) + ((totalRecord % noOfRecord) > 0 ? 1 : 0);
                for (int i = 0; i < pages; i++)
                {
                    Button b = new Button();
                    b.Text = (i + 1).ToString();
                    b.CommandArgument = (i + 1).ToString();
                    b.ID = "Button_" + (i + 1).ToString();
                    b.CssClass = "btn btn-outline-warning";

                    b.Click += new EventHandler(this.b_click);
                    Panel1.Controls.Add(b);
                }
            }
        }
        private void b_click(object sender, EventArgs e)
        {
            //this is for get data from database from clicking button
            string pageNo = ((Button)sender).CommandArgument;
            PopulateData(Convert.ToInt32(pageNo), 5);

        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }
    }
}

Here b_click should work but it is not working any help would be appreciated as I am new to this custom paging 在这里b_click应该可以工作,但是不能工作,因为我是这个自定义分页的新手,不胜感激

I myself solved the problem by using a generic handler 我自己通过使用通用处理程序解决了问题

<ContentTemplate>
            <asp:GridView ID="GridView1" CssClass="table table-condensed" runat="server" OnRowCommand="GridView1_RowCommand" DataKeyNames="bookingid" AutoGenerateColumns="false">

                <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
                <Columns>

                    <asp:ButtonField CommandName="download"
                        ControlStyle-CssClass="btn btn-info" ButtonType="Button"
                        Text="Download Invoice" HeaderText="Invoice" />
                    <asp:BoundField DataField="bookingid" HeaderText="Booking ID" />
                    <asp:BoundField DataField="hotelName" HeaderText="Hotel Name" />
                    <asp:BoundField DataField="Booking_Status" HeaderText="Booking Status" />
                    <asp:BoundField DataField="Check_In" HeaderText="Check In" />
                    <asp:BoundField DataField="Check_Out" HeaderText="Check Out" />
                    <asp:BoundField DataField="RoomTypeName" HeaderText="Room Type" />
                    <asp:BoundField DataField="NoOfRoomsBooked" HeaderText="No of Rooms Booked" />

                    <asp:BoundField DataField="Amount" HeaderText="Amount" />
                     <asp:BoundField HeaderText="Image" DataField="NationalID" Visible="false" />
                    <asp:TemplateField HeaderText="NationalID" >
                        <ItemTemplate>
                            <asp:Image ID="Image1"  runat="server" ImageUrl='<%# "imageHandler.ashx?bookingId="+ Eval("bookingId") %>'  
                            Height="150px" Width="150px" />  
                         <%--   <asp:Image ID="Image1" CssClass="default"  runat="server" 
                                ImageUrl='<%# "data:Image/jpg;base64,"
                    + Convert.ToBase64String((byte[])Eval("NationalID")) %>' />--%>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>
            <asp:HiddenField runat="server" ID="hiddenprimary" />
            <div style="margin-left: 1500px;">
                <asp:Panel ID="Panel1" runat="server"></asp:Panel>
            </div>

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace HotelReservation.Views
{
    /// <summary>
    /// Summary description for ImageHandler
    /// </summary>
    public class ImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string imageid = context.Request.QueryString["bookingId"];
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyProjectConnection"].ConnectionString))
            {

                connection.Open();

                SqlCommand command = new SqlCommand("select NationalID from tblReservation where bookingid=" + imageid, connection);
                command.CommandType = CommandType.Text;
                SqlDataReader dr = command.ExecuteReader();
                dr.Read();
                context.Response.BinaryWrite((Byte[])dr[0]);
                connection.Close();
                context.Response.End();

            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM