实现步骤:
1.在datagrid中创建模板列
2.在模板列中加入imagebutton并为其指定ImageUrl和CommandName,代码如下:
aspx代码
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="../Pic/delete.gif" CommandName="Del"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
3.在datagrid的ItemCommand事件中添加如下代码<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="../Pic/delete.gif" CommandName="Del"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
单击删除图标时触发
private void DG_userinfo_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Del")
{
string uid = e.Item.Cells[0].Text.ToString();
string [] values = {uid};
OperateXmlByDataSet.DeleteXmlRows(ViewState["filename"].ToString(),"userid",values);
BindDG();
}
if(e.CommandName=="UpDt")
{
string userid = e.Item.Cells[0].Text.ToString();
ViewState["uid"] = userid;
this.Txt_id.Text = userid;
this.Txt_name.Text = e.Item.Cells[1].Text.ToString();
this.Txt_pass.Text = e.Item.Cells[2].Text.ToString();
this.Txt_mail.Text = e.Item.Cells[3].Text.ToString();
this.Txt_site.Text = e.Item.Cells[4].Text.ToString();
this.Txt_msn.Text = e.Item.Cells[5].Text.ToString();
this.Panel1.Visible=true;
}
}
private void DG_userinfo_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Del")
{
string uid = e.Item.Cells[0].Text.ToString();
string [] values = {uid};
OperateXmlByDataSet.DeleteXmlRows(ViewState["filename"].ToString(),"userid",values);
BindDG();
}
if(e.CommandName=="UpDt")
{
string userid = e.Item.Cells[0].Text.ToString();
ViewState["uid"] = userid;
this.Txt_id.Text = userid;
this.Txt_name.Text = e.Item.Cells[1].Text.ToString();
this.Txt_pass.Text = e.Item.Cells[2].Text.ToString();
this.Txt_mail.Text = e.Item.Cells[3].Text.ToString();
this.Txt_site.Text = e.Item.Cells[4].Text.ToString();
this.Txt_msn.Text = e.Item.Cells[5].Text.ToString();
this.Panel1.Visible=true;
}
}
4.在datagrid的ItemCreated事件中添加如下代码
.cs代码
private void DG_userinfo_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
System.Web.UI.WebControls.ImageButton ib;
foreach(DataGridItem item in this.DG_userinfo.Items)
{
ib = (ImageButton)item.FindControl("ImageButton1");
ib.Attributes.Add("onclick","return confirm('您确定要删除?');");
}
}
private void DG_userinfo_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
System.Web.UI.WebControls.ImageButton ib;
foreach(DataGridItem item in this.DG_userinfo.Items)
{
ib = (ImageButton)item.FindControl("ImageButton1");
ib.Attributes.Add("onclick","return confirm('您确定要删除?');");
}
}