c# - 使用gridview在itemtemplate上自定义分页
问题描述
我想对我的 gridview 应用自定义分页,其中包含 itemtemplate 的图像来自数据库
我尝试过使用静态客户端 ID 模式,但没有成功
我的程序:-
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)
{
}
}
}
这里 b_click 应该可以工作,但它不工作任何帮助将不胜感激,因为我是这个自定义分页的新手
解决方案
我自己通过使用通用处理程序解决了这个问题
<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;
}
}
}
}
推荐阅读
- bash - bash cut 命令:为什么我的脚本没有剪切?
- powerbi - 在 PowerBI 报告中显示一小时的总分钟利用率
- algorithm - 如何采用在 2D 空间中运行良好的算法并使其适应 3D 环境?
- idris - Prelude 如何允许 Nat 使用数字文字?
- chart.js - 当用户调整页面大小时图表变得混乱
- c - 程序内存中的 utf-8 字符串 (__flash)
- integration-testing - xUnit IClassFixture 构造函数被多次调用
- javascript - 如何在 HTML 中创建无限 JavaScript 循环?
- java - 在 for 循环期间无法附加到我的数组
- javascript - 你如何过滤没有键的javascript嵌套数组