我想对包含itemtemplate的gridview应用自定义分页 其中包含来自数据库的图像
我尝试使用clientid模式为static,但是没有解决
我的程序:-
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可以正常工作,但不能正常工作,因为我是这个自定义分页的新手,所以不胜感激
答案 0 :(得分:0)
我本人通过使用通用处理程序解决了这个问题
<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;
}
}
}
}