8. 为 DataGrid 控件添加分页事件
在 DataGrid 控件标签中加入如下代码
OnPageIndexChanged="DataGrid1_PageIndexChanged"
在后台中加入如下代码
/// <summary>
/// 响应分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void DataGrid1_Page(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataBind();
}
9. 为 DataGrid 控件添加绑定事件,即在 DataGrid绑定时发生的事件处理
一般用些事件来,做一些页面上的效果.如更改背景色,文本框大小等.
OnItemDataBound="DataGrid1_ItemDataBound"
/// <summary>
/// 响应DataGrid绑定事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='white'");
}
else if (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"
在后台中加入如下代码
public void ItemsGrid_Command(Object sender, DataGridCommandEventArgs e)
{
switch(((LinkButton)e.CommandSource).CommandName)
{
case "Delete":
int classID = Int32.Parse((e.Item.Cells[0].Text).ToString());
ActorClass.DeleteActorClass(classID);
if (Request.QueryString.Get("classID") != null)
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
else
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
break;
// Add other cases here, if there are multiple ButtonColumns in
// the DataGrid control.
case "hidden":
int actorID = Int32.Parse((e.Item.Cells[0].Text).ToString());
ActorClass.HiddenActorClass(actorID);
if (Request.QueryString.Get("classID") != null)
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
else
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
break;
case "MoveUp":
int actorclassID = Int32.Parse((e.Item.Cells[0].Text).ToString());
string orderID = (e.Item.Cells[5].Text).ToString();
ActorClass.MoveUp(orderID,actorclassID);
if (Request.QueryString.Get("classID") != null)
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
else
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
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 * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
else
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by orderID"));
break;
default:
// Do nothing.
break;
}
}
11. 为 DataGrid添加模板列,但是内容根据字段值来显示"链接",还是文本
以下三个都是,根据字段列不同,而显示内容及显示控件不同的处理代码.
<asp:TemplateColumn HeaderText="子菜单">
<ItemTemplate>
<%# ActorClassManage.hasLeaf(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),DataBinder.Eval(Container.DataItem,"child").ToString())%>
</ItemTemplate>
</asp:TemplateColumn>
public static string hasLeaf(string id,string child)
{
string lRtn = "";
if (Int32.Parse(child) > 0)
lRtn="<a href='Actorclassmanage.aspx?classID="+id+"'><font color=blue>子菜单(" + child + ")</font></a>";
else
lRtn = "无子菜单";
return lRtn;
}
<asp:TemplateColumn HeaderText="属性">
<ItemTemplate>
<asp:LinkButton Text='<%# IsHidden(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),(bool)DataBinder.Eval(Container.DataItem,"Enabled")) %>' runat="server" CommandName="hidden" ID="Linkbutton1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
public static string IsHidden(string id,bool enabled)
{
string lRtn = "";
if (enabled == true)
{
lRtn = "[显示]";
}
else
{
lRtn = "隐藏";
}
return lRtn;
}
public static void Sort(string actorclassID, string orderID)
{
string temp = "";
if (Int32.Parse(BgPicManage.GetMaxCode("actorclass","orderID")) == Int32.Parse(orderID))
{
temp += "<ipnut type='submit' value='向下移'>";
}
if (Int32.Parse(orderID) == 0)
{
temp += "<ipnut type='submit' value='向上移'>";
}
}
12. DataGrid 控件自定义分页代码
将下列代码放于包含<DataGrid>的form中去,
<p style="FONT-SIZE:9pt" align="center">
<asp:label id="lblPageCount" runat="server"></asp:label>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
</p>
后台代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
//绑定数据源
if (!Page.IsPostBack)
{
OpenDatabase();
BindGrid();
}
}
//用于显示"第几页,总*页"
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}
//响应分页按钮
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((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();
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1466288