asp.net的GridView控件使用方法大全
前台.aspx
<asp:Label ID="tplb" runat="server" Text="总页数"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>
<asp:Label ID="curLabel" runat="server" Text="当前页"></asp:Label>
<asp:Label ID="lblPage" Text="1" runat="server"></asp:Label>
<asp:LinkButton ID="lblFirstButton" runat="server" OnClick="lblFirstButton_Click" >|<</asp:LinkButton>
<asp:LinkButton ID="lblPreButton" runat="server" OnClick="lblPreButton_Click" ><</asp:LinkButton>
<asp:LinkButton ID="lblNextButton" runat="server" OnClick="lblNextButton_Click" >></asp:LinkButton>
<asp:LinkButton ID="lblLastButton" runat="server" OnClick="lblLastButton_Click" >>|</asp:LinkButton>
<asp:DropDownList ID="ddlPage" runat="server" Width="40px" AutoPostBack="True"
OnSelectedIndexChanged="ddlPage_SelectedIndexChanged">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="PageSizeLabel" runat="server" Text="条/页"></asp:Label>
后台
#region分页
protected void BindFollowExamInfoGridView(int PersonID)
{
int currentpage = Convert.ToInt32(lblPage.Text);
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //查询指定人的随访信息记录
if (dt.Rows.Count > 0)
{
FollowExamInfoGridView.DataSource = dt;
FollowExamInfoGridView.DataBind();
PagedDataSource ps = new PagedDataSource();
ps.DataSource = dt.DefaultView;
ps.AllowPaging = true;
ps.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
lblPageCount.Text = ps.PageCount.ToString();
this.lblPreButton.Enabled = true;
this.lblNextButton.Enabled = true;
ps.CurrentPageIndex = currentpage - 1;
if (currentpage == 1)
{
this.lblPreButton.Enabled = false;
this.lblFirstButton.Enabled = false;
}
else
{
this.lblPreButton.Enabled = true;
this.lblFirstButton.Enabled = true;
}
if (currentpage == ps.PageCount)
{
this.lblNextButton.Enabled = false;
this.lblLastButton.Enabled = false;
}
else
{
this.lblNextButton.Enabled = true;
this.lblLastButton.Enabled = true;
}
FollowExamInfoGridView.DataSource = ps;
FollowExamInfoGridView.DataBind();
}
}
protected void lblPreButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) - 1);
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblNextButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) + 1);
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblFirstButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = "1";
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblLastButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = lblPageCount.Text;
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
{
lblPage.Text = "1";
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
#endregion
排序
Allowsort = "true"
sortExpression = "ID"
DataView dv = SortBindGrid(dt);
#region排序
protected void FollowExamInfoGridView_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = "asc";
}
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
}
public DataView SortBindGrid(DataTable table)
{
if (table != null)
{
DataView dv = table.DefaultView;
if (ViewState["sortexpression"] != null && ViewState["sortdirection"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
return dv;
}
else
{
return null;
}
}
#endregion
=======自带分页
#region自带分页
protected void FollowExamInfoGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
FollowExamInfoGridView.PageIndex = e.NewPageIndex;
BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
}
#endregion
选中Grid View 的实现
#region实现选中行
<SelectedRowStyle BackColor="AliceBlue" ForeColor="Gray" />
<asp:CommandField ShowSelectButton="True"/>
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click()");
}
protected void GridViewRegiment_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridViewRegiment.SelectedRow;
int RegimentID = Convert.ToInt32(row.Cells[1].Text);
Response.Redirect("UpdateRegimentation.aspx?RegimentID=" + RegimentID);
}
#endregion
显示颜色和删除
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int i;
//for (i = 0; i < GridViewRegiment.Rows.Count; i++)
//{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当有编辑列时,避免出错,要加的RowState判断
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((ImageButton)e.Row.Cells[2].FindControl("IBtndelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除"" + e.Row.Cells[0].Text + ""吗?')");
}
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E6F5FA'");
}
/
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程