前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="ItemId" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" GridLines="None"CellSpacing="2"OnRowDeleting="GridView1_RowDeleting"OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="部门标志">
<EditItemTemplate>
<asp:TextBox ID="TextBox2"runat="server"Text='<%#Eval("ImageUrl") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server"ImageUrl='<%#"~/Upfile/" + Eval("ImageUrl") %>' Width="30" Height="28"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ExhibitName" HeaderText="部门名称" />
<asp:BoundField DataField="Linkman" HeaderText="联系人" />
<asp:TemplateField HeaderText="项目经验" ShowHeader="False">
<ItemStyle BorderWidth="0px"Wrap="False"/>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"runat="server"CausesValidation="false"CommandName="Select"
Text='<%#DataBinder.Eval(Container,"DataItem.ProjectExperience").ToString().Length > 10 ?DataBinder.Eval(Container, "DataItem.ProjectExperience").ToString().Substring(0,10) + "..." : DataBinder.Eval(Container,"DataItem.ProjectExperience")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgDelete"runat="server" CommandName="Delete" ImageUrl="~/images/delete.gif" OnClientClick="returnconfirm('你确定删除吗?')" style="cursor:hand;"/>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Image" CommandName="Update"
HeaderText="修改" ImageUrl="~/images/edit.gif">
<ItemStyle Width="25px"/>
<HeaderStyle Wrap="False"/>
</asp:ButtonField>
</Columns>
<RowStyle Font-Size="13px" HorizontalAlign="Center" />
<HeaderStyle Font-Size="14px" HorizontalAlign="Center" />
</asp:GridView>
<table width="100%">
<tr id="pagerTr" runat="server">
<td align="right" style="font-size: 12px; height: 28px">
<asp:Label ID="labTotal"runat="server"Text="共0条记录 第0页/共0页"></asp:Label>
<asp:LinkButton ID="linkFirst"runat="server"OnClick="linkFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="linkPrevious"runat="server"OnClick="linkPrevious_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="linkNext"runat="server"OnClick="linkNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="linkLast"runat="server"OnClick="linkLast_Click">尾页</asp:LinkButton>
<asp:TextBox ID="txtRows" runat="server" Width="20px"></asp:TextBox>
<asp:LinkButton ID="linkGO" runat="server" CausesValidation="False" OnClick="linkGO_Click">GO</asp:LinkButton>
</td>
</tr>
</table>
后台:
#region Private Members
VenuesControllervc = new VenuesController();//封转的数据逻辑业务
introwcount = 10; //要显示的行数
#endregion
protected void Page_Load(System.Object sender, System.EventArgse)
{
try
{
if (!Page.IsPostBack)
{
panAdd.Visible =false;
panList.Visible= false;
Label1.Visible =false;
ViewState[this.ID + "startPage"]= 1;
ViewState[this.ID + "totalPage"]= 0;
Binder();
}
}
catch(Exception exc) //Modulefailed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
///<summary>
///首页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkFirst_Click(objectsender, EventArgs e)
{
ViewState[this.ID + "startPage"]= 1;
Binder();
}
///<summary>
///上一页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkPrevious_Click(object sender, EventArgs e)
{
intstartPage = int.Parse(ViewState[this.ID + "startPage"].ToString());
startPage = startPage -1;
startPage = startPage< 1 ? 1 : startPage;
ViewState[this.ID + "startPage"]= startPage;
Binder();
}
///<summary>
///下一页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkNext_Click(objectsender, EventArgs e)
{
intstartPage = int.Parse(ViewState[this.ID + "startPage"].ToString());
inttotalPage = int.Parse(ViewState[this.ID + "totalPage"].ToString());
startPage = startPage +1;
startPage = startPage> totalPage ? totalPage : startPage;
ViewState[this.ID + "startPage"]= startPage;
Binder();
}
///<summary>
///尾页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkLast_Click(objectsender, EventArgs e)
{
ViewState[this.ID + "startPage"]= ViewState[this.ID + "totalPage"];
Binder();
}
///<summary>
///跳转到指定页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkGO_Click(objectsender, EventArgs e)
{
intcurPage = 0;
try
{
curPage = int.Parse(this.txtRows.Text.Trim());
}
catch
{
this.txtRows.Text = "";
Page.ClientScript.RegisterStartupScript(this.GetType(),"prompt", "alert('输入的页码不正确,请重新输入!')", true);
return;
}
inttotalPage = int.Parse(ViewState[this.ID + "totalPage"].ToString());
curPage = curPage >totalPage ? totalPage : curPage;
curPage = curPage < 1? 1 : curPage;
ViewState[this.ID + "startPage"]= curPage;
Binder();
}
///<summary>
///数据绑定
///</summary>
publicvoid Binder()
{
intstart = (int.Parse(ViewState[this.ID + "startPage"].ToString())- 1) * rowcount;
inttotalCount=0;
stringtitle=this.txtSearch.Text.Trim();
List<VenuesInfo> vInfo = vc.GetQueryVenues(start,rowcount, title, out totalCount);
this.GridView1.DataSource=vInfo;
this.GridView1.DataBind();
this.txtRows.Text= "";
ViewState[this.ID + "totalPage"]= (totalCount / rowcount) + ((totalCount % rowcount) > 0 ? 1 : 0);
labTotal.Text = "共" + totalCount.ToString() + "条记录 第" + ViewState[this.ID+ "startPage"].ToString() + "页/共" + ViewState[this.ID + "totalPage"].ToString() + "页";
}
///<summary>
///查看数据
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid GridView1_SelectedIndexChanged(object sender, EventArgse)
{
intItemId = int.Parse(this.GridView1.DataKeys[this.GridView1.SelectedIndex].Value.ToString());
//Response.Redirect(EditUrl((Session["itemId"]= ItemId).ToString(), "Edit"));
ViewState[this.ID + "ItemId"]= ItemId;
VenuesInfov = vc.GetQueryVenuesByItemId(ItemId);
this.lblExhibitName.Text= v.ExhibitName.ToString();
this.lblCreatedDate.Text= v.CreatedDate.ToString("yyyy-MM-dd");
this.lblProjectExperience.Text= v.ProjectExperience.ToString();
this.lblLinkman.Text= v.Linkman.ToString();
this.lblEmail.Text= v.Email.ToString();
this.lblLinkAddress.Text= v.LinkAddress.ToString();
panList.Visible = true;
panAdd.Visible = false;
panView.Visible = false;
}
///<summary>
///删除
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid GridView1_RowDeleting(object sender, GridViewDeleteEventArgse)
{
intItemId = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
vc.GetQueryDeleteVenue(ItemId);
Binder();
}
///<summary>
///绑定修改
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid GridView1_RowUpdating(object sender, GridViewUpdateEventArgse)
{
intItemId = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
panAdd.Visible = true;
linkUpdate.Visible = true;
panView.Visible = false;
panList.Visible = false;
linkSave.Visible = false;
ViewState[this.ID + "ItemId"]= ItemId;
VenuesInfov = vc.GetQueryVenuesByItemId(ItemId);
this.txtExhibitName.Text= v.ExhibitName.ToString();
this.txtLinkMan.Text= v.Linkman.ToString();
this.txtExhibitIndicate.Text= v.ExhibitIndicate.ToString();
this.txtPhone.Text= v.LinkPhone.ToString();
this.txtEmail.Text= v.Email.ToString();
this.txtQQ.Text= v.QQ.ToString();
this.txtAddress.Text= v.LinkAddress.ToString();
this.Label1.Text= v.ImageUrl.ToString();
this.txtProjectExperience.Text= v.ProjectExperience.ToString();
this.txtEquipmentDescription.Text= v.EquipmentDescription.ToString();
}
VenuesController类:
///<summary>
///分页
///</summary>
///<paramname="Start"></param>
///<paramname="Count"></param>
///<paramname="ExhibitName"></param>
///<paramname="TotalCount"></param>
///<returns></returns>
publicList<VenuesInfo>GetQueryVenues(int Start, int Count, stringExhibitName, out intTotalCount)
{
TotalCount = DataProvider.Instance().GetQueryVenuesCount(ExhibitName);
returnCBO.FillCollection<VenuesInfo>(DataProvider.Instance().GetQueryVenues(Start,Count, ExhibitName));
}
///<summary>
///分页记录数量
///</summary>
///<param name="ExhibitName"></param>
///<returns></returns>
publicoverride intGetQueryVenuesCount(string ExhibitName)
{
return(int)SqlHelper.ExecuteScalar(ConnectionString,GetFullyQualifiedName("GetQueryVenuesCount"),ExhibitName);
}
///<summary>
///分页绑定
///</summary>
///<paramname="Start"></param>
///<paramname="Count"></param>
///<paramname="ExhibitName"></param>
///<returns></returns>
publicoverride IDataReaderGetQueryVenues(int Start, int Count, stringExhibitName)
{
return(IDataReader)SqlHelper.ExecuteReader(ConnectionString,GetFullyQualifiedName("GetQueryVenues"),Start, Count, ExhibitName);
}
存储过程:
CREATEPROCEDURE [dbo].[YourCompany_GetQueryVenues]
@Startint ,
@Countint,
@ExhibitName[varchar] (50)
as
selectidentity(int, 1,1) as indexid,ModuleId,ItemId*1ItemId,Content,CreatedByUser,CreatedDate,ExhibitName,Linkman,VisitsTatal,ImageUrl,ExhibitIndicate,LinkPhone,Email,QQ,LinkAddress,ProjectExperience,EquipmentDescriptioninto #newtable from Venues
where ExhibitName like '%' + @ExhibitName+ '%' or ExhibitName is null
declare@sqlstr varchar(500)
set@sqlstr='select top ' + cast( @Count as varchar(20))+ 'ModuleId,ItemId,Content,CreatedByUser,
CreatedDate,ExhibitName,Linkman,VisitsTatal,ImageUrl,ExhibitIndicate,LinkPhone,Email,QQ,LinkAddress,ProjectExperience,EquipmentDescriptionfrom #newtable where indexId not in ( select top ' + cast(@Start as varchar(20))+'
indexId from #newtable order by CreatedDatedesc) order by CreatedDate desc'
execute(@sqlstr)
CREATEPROCEDURE [dbo].[YourCompany_GetQueryVenuesCount]
@ExhibitName[varchar] (50)
as
selectcount(*) from Venues where ExhibitName like '%' + @ExhibitName + '%' orExhibitName is null
GO