aspx页面中的DataGrid
<asp:DataGrid ID="dg" runat="server" DataKeyField="Id" AutoGenerateColumns="False" OnItemDataBound="dg_ItemDataBound" OnItemCommand="dg_ItemCommand" ShowHeader="False" Width="100%" >
<Columns>
<asp:TemplateColumn HeaderText="序号">
<ItemStyle Width="10%" HorizontalAlign="Center" Wrap="False" />
<ItemTemplate>
<asp:Label ID="lblIndex" runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblIndex2" runat="server"></asp:Label>
</EditItemTemplate>
<FooterStyle Width="10%" HorizontalAlign="Center" Wrap="False" />
<FooterTemplate>
<asp:Label ID="lblIndex" Text="*" ForeColor="red" runat="server">
</asp:Label>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemStyle Width="50%" HorizontalAlign="Left" Wrap="False" />
<ItemTemplate>
<asp:HyperLink ID="hlName" runat="server">
</asp:HyperLink>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbName" Width="100%" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterStyle Width="50%" HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="Name" Width="100%" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn FooterText="操作">
<ItemStyle Width="20%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lbEdit" CommandName="Edit" runat="server" Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="lbUpdate" CommandName="Update" runat="server" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="lbCancel" CommandName="Cancel" runat="server" Text="取消"></asp:LinkButton>
</ItemTemplate>
<FooterStyle Width="20%" HorizontalAlign="Center" />
<FooterTemplate>
<asp:LinkButton ID="Cancel" Text="取消" CommandName="Cancel" Width="100%" runat="server"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle Width="20%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lbDel" CommandName="Delete" runat="server" Text="删除"></asp:LinkButton>
</ItemTemplate>
<FooterStyle Width="20%" HorizontalAlign="Center" />
<FooterTemplate>
<asp:LinkButton ID="Save" Text="添加" CommandName="Add" Width="100%" runat="server"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
cs中的ItemDataBound和ItemCommand处理函数 #region 执行ItemDataBound事件
/// <summary>
/// ItemDataBound事件处理函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
if (e.Item.ItemType == ListItemType.EditItem) //编辑模式
{
(e.Item.FindControl("lblIndex2") as Label).Text = i.ToString("00");
(e.Item.FindControl("lblIndex2") as Label).ForeColor = System.Drawing.Color.Red;
(e.Item.FindControl("lbEdit") as LinkButton).Visible = false;
(e.Item.FindControl("tbName") as TextBox).Text = drv["Name"].ToString();
}
else
{
(e.Item.FindControl("lbUpdate") as LinkButton).Visible = false;
(e.Item.FindControl("lbCancel") as LinkButton).Visible = false;
(e.Item.FindControl("lblIndex") as Label).Text = i.ToString("00");
(e.Item.FindControl("lbDel") as LinkButton).Attributes.Add("onclick", "return confirm('确实要删除该条记录吗?')");
(e.Item.FindControl("hlName") as HyperLink).Text = drv["Name"].ToString();
(e.Item.FindControl("hlName") as HyperLink).NavigateUrl = "AreaSmallList.aspx?Area="+drv["Name"].ToString()+"&Parent_ID="+dg.DataKeys[e.Item.ItemIndex].ToString();
}
}
}
#endregion
/// <summary>
/// ItemCommand事件处理函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
protected void dg_ItemCommand(object sender, DataGridCommandEventArgs e)
{
string commandName = e.CommandName;
//根据CommandName 执行不同的操作
switch (commandName)
{
case "Add":
AddData(e);
break;
case "Edit":
dg.EditItemIndex = e.Item.ItemIndex;
break;
case "Update":
UpdateData(e);
dg.EditItemIndex = -1;
break;
case "Cancel":
dg.EditItemIndex = -1;
break;
case "Delete":
DeleteData(e);
break;
}
this.dg.ShowFooter = false;
}
#region 相关Command处理函数
/// <summary>
/// 添加数据
/// </summary>
/// <param name="e"></param>
protected void AddData(DataGridCommandEventArgs e)
{
//相关处理
}
/// <summary>
/// 删除指定记录行数据
/// </summary>
/// <param name="e"></param>
private void DeleteData(DataGridCommandEventArgs e)
{
//相关处理
}
/// <summary>
/// 更新数据
/// </summary>
/// <param name="e"></param>
private void UpdateData(DataGridCommandEventArgs e)
{
//相关处理
}
#endregion