1.前台设计:同DataList不同的是,它的分页全写在<asp:GridView>的内部</asp:GridView>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging"
PageSize="7" onpageindexchanged="GridView1_PageIndexChanged"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="user_ID" HeaderText="编号" />
<asp:BoundField DataField="user_Name" HeaderText="用户名" />
<asp:BoundField DataField="user_RealName" HeaderText="真实姓名" />
<asp:BoundField DataField="user_Theme" HeaderText="主题" />
<asp:CheckBoxField DataField="user_IsEnable" HeaderText="启用" />
<asp:BoundField DataField="user_birthday" HeaderText="生日" />
<asp:ImageField DataImageUrlField="user_Img" HeaderText="照片">
</asp:ImageField>
<asp:HyperLinkField DataTextField="user_PersonalWeb" HeaderText="个人主页"
DataNavigateUrlFields="user_PersonalWeb" />
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:Button ID="Button1" runat="server" Height="21px" Text="编辑"
Width="35px" CommandName="Edit" />
</AlternatingItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton7" runat="server" CommandName="Update">跟新</asp:LinkButton>
<asp:LinkButton ID="LinkButton8" runat="server" CommandName="Cancel">取消</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton6" runat="server" CommandName="Edit">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<!--注意分页属性写在</Columns>下方,其原理是使用了CommandArgument和CommandName属性;-->
<PagerTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="First"
CommandName="Page">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Prev"
CommandName="Page">上一页</asp:LinkButton>
<asp:TextBox ID="TextBox1" runat="server" Height="16px" Width="12px"></asp:TextBox>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="Page">跳转</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Next" CommandName="Page">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument="Last" CommandName="Page">末页</asp:LinkButton>
<asp:Label ID="Label1" runat="server" Text="<%# this.GridView1.PageIndex+1 %>"></asp:Label> //当前页
/<asp:Label ID="Label2" runat="server" Text="<%# this.GridView1.PageCount %>"></asp:Label> //总页数
</PagerTemplate>
</asp:GridView>
2. 后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}
//TextBox 内输入所要跳转的页:
TextBox tx = (TextBox)(this.GridView1.BottomPagerRow.FindControl("TextBox1"));
LinkButton tiaoZhuan = (LinkButton)(this.GridView1.BottomPagerRow.FindControl("LinkButton5")); //点击跳转,跳转到某一页
tiaoZhuan.CommandArgument = tx.Text.Trim();
}
protected void bind()
{
string sql = "select * from sys_User";
DataSet ds = DbHelperSQL.Query(sql);
this.GridView1.DataSource = ds;
this.GridView1.DataMember = ds.Tables[0].TableName; //多页显示,即分页时使用的。
this.GridView1.DataKeyNames =new string[] {"user_ID"};
GridView1.DataBind();
}
//当前页索引“正在更改”时激发
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Label 显示当前页索引
this.GridView1.PageIndex = e.NewPageIndex;
this.bind();
}
//当前页索引“已经更改”时激发
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
((TextBox)(this.GridView1.BottomPagerRow.FindControl("TextBox1"))).Text = (this.GridView1.PageIndex + 1).ToString();
//GridView1.PageIndex 默认从0开始,即第一页为"第0页"
//if ((this.GridView1.BottomPagerRow.FindControl("TextBox1")).Focus())
//{
// ((TextBox)(this.GridView1.BottomPagerRow.FindControl("TextBox1"))).Text == "";
//}
}
//当点击“编辑”时,系统必须要触发的事件:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
//当点击“修改”时,系统必须要触发的事件:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string userName = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
string realName = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
string theme = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
bool isEnable = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked;
string birthday = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
string img = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim();
string sqlStr = "Update sys_user set user_Name='" + userName + "',user_RealName='" + realName + "',user_Theme='" + theme + "',user_IsEnable='" + isEnable + "',user_Birthday='" + birthday + "',user_Img='" + img + "' where user_ID=" + userID;
int result = DbHelperSQL.ExecuteSql(sqlStr);
this.GridView1.EditIndex = -1;
this.bind();
}
//当点击“取消”时,系统必须要触发的事件:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.bind();
}