Asp.Net:GridView 编辑、删除、自定义分页以后备用
页面
GridView 绑定:在<asp:BoundField/>
和 <asp:TemplateField><ItemTemplate>嵌套服务器控件 </ItemTemplate></asp:TemplateField>
2种方式
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Info_RegisteredPopulationList.aspx.cs"
Inherits="UI_Person_Info_RegisteredPopulationList" %>
<%@ Register Assembly="WebPageContorl" Namespace="WebPageContorl" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:ImageButton ID="ibtnAdd" runat="server" ImageUrl="~/image/button_07.jpg" OnClick="ibtnAdd_Click" />
</div>
<asp:GridView ID="GridViewRegisteredPopulation" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1" Width="100%"
OnRowDataBound="GridViewRegisteredPopulation_RowDataBound"
onrowcommand="GridViewRegisteredPopulation_RowCommand">
<Columns>
<asp:BoundField DataField="PopulationID" HeaderText="PopulationID" SortExpression="PopulationID"
Visible="False" />
<asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" />
<asp:BoundField DataField="PerIdNumber" HeaderText="PerIdNumber" SortExpression="PerIdNumber"
Visible="False" />
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="LblSex" Text='<%#Eval("Sex") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="民族">
<ItemTemplate>
<asp:Label ID="LblNationality" Text='<%#Eval("Nationality") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="婚姻状况">
<ItemTemplate>
<asp:Label ID="LblMaritalStatus" Text='<%#Eval("MaritalStatus") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="宗教信仰">
<ItemTemplate>
<asp:Label ID="LblReligion" Text='<%#Eval("Religion") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="职业类别">
<ItemTemplate>
<asp:Label ID="LblOccGenre" Text='<%#Eval("OccGenre") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Occupation" HeaderText="职业" SortExpression="Occupation" />
<asp:BoundField DataField="UnitService" HeaderText="服务处所" SortExpression="UnitService" />
<asp:BoundField DataField="Contact" HeaderText="联系方式" SortExpression="Contact" />
<asp:TemplateField HeaderText="户籍地">
<ItemTemplate>
<asp:Label ID="LblLiveAddress" Text='<%#Eval("LiveAddress") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="现在地">
<ItemTemplate>
<asp:Label ID="LblNowAddress" Text='<%#Eval("NowAddress") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="与户主关系">
<ItemTemplate>
<asp:Label ID="LblMasterRelation" Text='<%#Eval("MasterRelation") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MasterContact" HeaderText="户主联系方式" SortExpression="MasterContact" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:ImageButton ID="ibtnModify" runat="server" ImageUrl="~/image/page_bj.png" CommandName="Modify"
CommandArgument='<%# Eval("PopulationID") %>' />
<asp:ImageButton ID="ibtnRemove" runat="server" OnClientClick="javascript:if(!confirm('确定删除此项?')){return false;}" ImageUrl="~/image/top_11.png" CommandName="Remove"
CommandArgument='<%# Eval("PopulationID") %>' />
<asp:ImageButton ID="ibtnSearch" runat="server" ImageUrl="~/image/page_ck.png" CommandName="Search"
CommandArgument='<%# Eval("PopulationID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetPageList"
TypeName="BLL.Info_RegisteredPopulationBLL">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="pageIndex" Type="Int32" />
<asp:Parameter DefaultValue="12" Name="pageSize" Type="Int32" />
<asp:Parameter DefaultValue="" Direction="Output" Name="pageCount" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
<div id="mypage" runat="server">
<cc1:WebPager ID="WebPager2" runat="server" AlwaysShow="True" BorderStyle="None"
CssClass="paginator" CurrentPageButtonClass="cpb" CustomInfoHTML="当前第%CurrentPageIndex%页,共%PageCount%页/每页%PageSize%条,总记录数:%RecordCount%"
CustomInfoTextAlign="Right" FirstPageText="首页" HorizontalAlign="Left" LastPageText="尾页"
NextPageText="下一页" PrevPageText="上一页" ShowPageIndexBox="Never" Wrap="False" ShowCustomInfoSection="Left"
OnPageChanged="WebPager2_PageChanged" PageSize="12">
</cc1:WebPager>
</div>
<div style="display: none;">
<asp:Label ID="lPageIndex" runat="server" Text="0"></asp:Label>
<asp:Label ID="lPageSize" runat="server" Text="0"></asp:Label>
</div>
</form>
</body>
</html>
public partial class UI_Person_Info_RegisteredPopulationList : System.Web.UI.Page
{
//int pageIndex = 1, pageSize = 12, pageCount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetPagerCounts();
}
}
public void SetPagerCounts()
{
WebPager2.RecordCount = Info_RegisteredPopulationBLL.GetTotalCount();
}
/// <summary>
/// 页码条变换事件方法 李可
/// </summary>
protected void WebPager2_PageChanged(object sender, EventArgs e)
{
bind();
}
public void bind() {
lPageIndex.Text = (WebPager2.CurrentPageIndex).ToString();
int pageIndex = Convert.ToInt32(lPageIndex.Text);
lPageSize.Text = (WebPager2.PageSize).ToString();
int pageSize = Convert.ToInt32(lPageSize.Text);
//复制SelectParameters
Parameter pageIndexPara = ObjectDataSource1.SelectParameters["pageIndex"];
Parameter pageSizePara = ObjectDataSource1.SelectParameters["pageSize"];
Parameter pageCountPara = ObjectDataSource1.SelectParameters["pageCount"];
//清除原有的SelectParameter
ObjectDataSource1.SelectParameters.Clear();
//重新给数据源赋值参数
pageIndexPara.DefaultValue = lPageIndex.Text;
pageSizePara.DefaultValue = lPageSize.Text;
//重新加入新的SelectParameter
ObjectDataSource1.SelectParameters.Add(pageIndexPara);
ObjectDataSource1.SelectParameters.Add(pageSizePara);
ObjectDataSource1.SelectParameters.Add(pageCountPara);
GridViewRegisteredPopulation.DataBind();
}
protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("EditRegisteredPopulation.aspx?Id=0&Motion=" + Request.QueryString["Motion"]);
}
//数据绑定时触发的事件,区分repeator的ItemDataBound
protected void GridViewRegisteredPopulation_RowDataBound(object sender, GridViewRowEventArgs e)
{
//e.row排除其他模板类型生成的lblSex 对应的html 为null 未将引用~~实例
if (e.Row.RowType == DataControlRowType.DataRow)
{
/************性别*****************/
Label lblSex = e.Row.FindControl("lblSex") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblSex.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblSex.Text = ds.Dic_Name;
else
lblSex.Text = "未知";
}
/************民族*****************/
Label lblNationality = e.Row.FindControl("lblNationality") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblNationality.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblNationality.Text = ds.Dic_Name;
else
lblNationality.Text = "未知";
}
/************婚姻状况*****************/
Label lblMaritalStatus = e.Row.FindControl("lblMaritalStatus") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblMaritalStatus.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblMaritalStatus.Text = ds.Dic_Name;
else
lblMaritalStatus.Text = "未知";
}
/************宗教信仰*****************/
Label lblReligion = e.Row.FindControl("lblReligion") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblReligion.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblReligion.Text = ds.Dic_Name;
else
lblReligion.Text = "未知";
}
/************职业类别*****************/
Label lblOccGenre = e.Row.FindControl("lblOccGenre") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblOccGenre.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblOccGenre.Text = ds.Dic_Name;
else
lblOccGenre.Text = "未知";
}
/************户籍地*****************/
Label lblLiveAddress = e.Row.FindControl("lblLiveAddress") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblLiveAddress.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblLiveAddress.Text = ds.Dic_Name;
else
lblLiveAddress.Text = "未知";
}
/************现在地*****************/
Label lblNowAddress = e.Row.FindControl("lblNowAddress") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(lblNowAddress.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
lblNowAddress.Text = ds.Dic_Name;
else
lblNowAddress.Text = "未知";
}
/************与户主关系*****************/
Label LblMasterRelation = e.Row.FindControl("LblMasterRelation") as Label;
if (!string.IsNullOrEmpty(lblSex.Text))
{
Dic_Sex ds = Dic_SexBLL.GetDic_Sex(LblMasterRelation.Text);
if (!string.IsNullOrEmpty(ds.Dic_Code))
LblMasterRelation.Text = ds.Dic_Name;
else
LblMasterRelation.Text = "未知";
}
}
}
//区分repeator的ItemDataCommand
protected void GridViewRegisteredPopulation_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName;
long ID = Convert.ToInt64(e.CommandArgument);
switch (commandName)
{
case "Modify":
Response.Redirect("EditRegisteredPopulation.aspx?Id=" + ID + "&Motion=" + Request.QueryString["Motion"]);
break;
case "Remove":
Info_RegisteredPopulationBLL.DeleteRegister(ID);
//重新绑定
SetPagerCounts();
bind();
break;
case "Search":
Response.Redirect("EditRegisteredPopulation.aspx?Id=" + ID + "&Motion=" + Request.QueryString["Motion"]);
break;
default: break;
}
}
}
知识没有高低贵贱之分。