扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年10月1日
关键字:
在本页阅读全文(共2页)
8.为DataGrid控件添加分页事件
在DataGrid控件标签中加入如下代码
OnPageIndexChanged="DataGrid1_PageIndexChanged"
在后台中加入如下代码
///<summary>
///响应分页事件
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
publicvoidDataGrid1_Page(Objectsender,DataGridPageChangedEventArgse)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataBind();
}
9.为DataGrid控件添加绑定事件,即在DataGrid绑定时发生的事件处理
一般用些事件来,做一些页面上的效果.如更改背景色,文本框大小等.
OnItemDataBound="DataGrid1_ItemDataBound"
///<summary>
///响应DataGrid绑定事件
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
publicvoidDataGrid1_ItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
{
if(e.Item.ItemType==ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='white'");
}
elseif(e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#f6f6f6'");
}
}
10.为DataGrid控件添加接钮处理事件程序
在DataGrid控件标签中加入如下代码
OnItemCommand="ItemsGrid_Command"
在后台中加入如下代码
publicvoidItemsGrid_Command(Objectsender,DataGridCommandEventArgse)
{
switch(((LinkButton)e.CommandSource).CommandName)
{
case"Delete":
intclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
ActorClass.DeleteActorClass(classID);
if(Request.QueryString.Get("classID")!=null)
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
else
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
break;
//Addothercaseshere,iftherearemultipleButtonColumnsin
//theDataGridcontrol.
case"hidden":
intactorID=Int32.Parse((e.Item.Cells[0].Text).ToString());
ActorClass.HiddenActorClass(actorID);
if(Request.QueryString.Get("classID")!=null)
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
else
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
break;
case"MoveUp":
intactorclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
stringorderID=(e.Item.Cells[5].Text).ToString();
ActorClass.MoveUp(orderID,actorclassID);
if(Request.QueryString.Get("classID")!=null)
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
else
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
break;
case"MoveDown":
actorclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
orderID=(e.Item.Cells[5].Text).ToString();
ActorClass.MoveDown(orderID,actorclassID);
if(Request.QueryString.Get("classID")!=null)
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
else
Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbyorderID"));
break;
default:
//Donothing.
break;
}
}
11.为DataGrid添加模板列,但是内容根据字段值来显示"链接",还是文本
以下三个都是,根据字段列不同,而显示内容及显示控件不同的处理代码.
<asp:TemplateColumnHeaderText="子菜单">
<ItemTemplate>
<%#ActorClassManage.hasLeaf(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),DataBinder.Eval(Container.DataItem,"child").ToString())%>
</ItemTemplate>
</asp:TemplateColumn>
publicstaticstringhasLeaf(stringid,stringchild)
{
stringlRtn="";
if(Int32.Parse(child)>0)
lRtn="<ahref='Actorclassmanage.aspx?classID="+id+"'><fontcolor=blue>子菜单("+child+")</font></a>";
else
lRtn="无子菜单";
returnlRtn;
}
<asp:TemplateColumnHeaderText="属性">
<ItemTemplate>
<asp:LinkButtonText='<%#IsHidden(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),(bool)DataBinder.Eval(Container.DataItem,"Enabled"))%>'runat="server"CommandName="hidden"ID="Linkbutton1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
publicstaticstringIsHidden(stringid,boolenabled)
{
stringlRtn="";
if(enabled==true)
{
lRtn="[显示]";
}
else
{
lRtn="隐藏";
}
returnlRtn;
}
publicstaticvoidSort(stringactorclassID,stringorderID)
{
stringtemp="";
if(Int32.Parse(BgPicManage.GetMaxCode("actorclass","orderID"))==Int32.Parse(orderID))
{
temp+="<ipnuttype='submit'value='向下移'>";
}
if(Int32.Parse(orderID)==0)
{
temp+="<ipnuttype='submit'value='向上移'>";
}
}
12.DataGrid控件自定义分页代码
将下列代码放于包含<DataGrid>的form中去,
<pstyle="FONT-SIZE:9pt"align="center">
<asp:labelid="lblPageCount"runat="server"></asp:label>
<asp:labelid="lblCurrentIndex"runat="server"></asp:label>
<asp:linkbuttonid="btnFirst"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
Font-size="8pt"ForeColor="navy"CommandArgument="0"></asp:linkbutton>
<asp:linkbuttonid="btnPrev"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
Font-size="8pt"ForeColor="navy"CommandArgument="prev"></asp:linkbutton>
<asp:linkbuttonid="btnNext"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
Font-size="8pt"ForeColor="navy"CommandArgument="next"></asp:linkbutton>
<asp:linkbuttonid="btnLast"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
Font-size="8pt"ForeColor="navy"CommandArgument="last"></asp:linkbutton>
</p>
后台代码
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处放置用户代码以初始化页面
btnFirst.Text="最首页";
btnPrev.Text="前一页";
btnNext.Text="下一页";
btnLast.Text="最后页";
//绑定数据源
if(!Page.IsPostBack)
{
OpenDatabase();
BindGrid();
}
}
//用于显示"第几页,总*页"
privatevoidShowStats()
{
lblCurrentIndex.Text="第"+(MyDataGrid.CurrentPageIndex+1).ToString()+"页";
lblPageCount.Text="总共"+MyDataGrid.PageCount.ToString()+"页";
}
//响应分页按钮
publicvoidPagerButtonClick(objectsender,EventArgse)
{
stringarg=((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case"next":
if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1))
{
MyDataGrid.CurrentPageIndex+=1;
}
break;
case"prev":
if(MyDataGrid.CurrentPageIndex>0)
{
MyDataGrid.CurrentPageIndex-=1;
}
break;
case"last":
MyDataGrid.CurrentPageIndex=(MyDataGrid.PageCount-1);
break;
default:
MyDataGrid.CurrentPageIndex=System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者