在DataGrid控件,用了模板列,模板列里头有个按钮(button),如何在后台方法中获取到该按钮,并把该按钮的单击事件设置为提示"是否要删除?"谢谢!
解决方案 »
- 解释下这段ASP.NET程序
- 购物网站的产品分类目录的实现?
- 标准表达式中数据类型不匹配!!!请各位帮忙啊!我看不出来
- 请问ds.Tables[0].Rows.Count是什么意思?
- FreeTextBox html代码存储,再输出的问题(问题不难,解决是关键,求解....)
- js如何取checkboxlist的值
- asp.net小问题
- 小问题。各位,看看这个关于回传服务器的问题怎么解决?
- 如何动态改变Datagrid中的LinkButton列的文本。
- 变量Session["username"]用于sql查询语句该怎样写
- System.Data.SqlClient.SqlException: 对象名 'tc_advertise' 无效。
- 寻求每个节点的Path的页面缓存的问题
Html Code
<asp:TemplateField HeaderText="编辑">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="MyCommand" CausesValidation="False">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="lbtnDel" runat="server" CommandName="Delete" OnClientClick="return confirm('确认要删除吗?');" CausesValidation="False">删除</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
-------------
C# Code
//Gridview的删除方法
protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string empId = gvUser.DataKeys[e.RowIndex].Values["empId"].ToString().Trim();
//删除方法
}
//编辑
//protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
//{
// if (e.CommandName == "MyCommand")
// {
// Button button = (Button)e.CommandSource;
// GridViewRow row = (GridViewRow)button.Parent.Parent;
// string a = row.Cells[1].Text.ToString();//获得第一个单元格的值
// string b = this.GridView1.DataKeys[row.DataItemIndex].Values[0];//获得DataKeys的值
// }
//}
private void mydg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
Button delBttn =(Button) e.Item.findcontrol("按钮ID");
delBttn.Attributes.Add("onclick","return confirm('你确认要删除?')"); }
}
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("您确定要删除这条记录吗?")' Text="删除"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="30px" />
</asp:TemplateField>==================================
绑定的事件为:
protected void gdvPublishList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strtmp = this.gdvPublishList.DataKeys[e.RowIndex].Value.ToString(); int intBookNum = Convert.ToInt32(this.gdvPublishList.DataKeys[e.RowIndex].Value.ToString());
string[] pathValues = new string[2] { "", "" };
pathValues = publish.getBookToPic(intBookNum);
string strCoverPicPath = pathValues[0].ToString().Trim(); //取得封面图片相对路径
string strOtherPicPath = pathValues[1].ToString().Trim(); //取得插图图片相对路径 if (strCoverPicPath != "")
{
strCoverPicPath = Server.MapPath(strCoverPicPath);
if (File.Exists(strCoverPicPath)) //使用File.Exists判断某文件是否存在
{
File.Delete(strCoverPicPath);//删除实际封面图片
}
}
if (strOtherPicPath != "")
{
strOtherPicPath = Server.MapPath(strOtherPicPath);
if (File.Exists(strOtherPicPath)) //使用File.Exists判断某文件是否存在
{
File.Delete(strOtherPicPath);//删除实际插图图片
}
} string strFlag = publish.delBookInfo(Convert.ToInt32(Session["user_id"]), Convert.ToInt32(strtmp));
if (strFlag == "1")
{
lblMsg.Text = " 提示:图书信息删除成功";
}
else
lblMsg.Text = " 错误:图书信息删除失败!请稍后再试...";
BindPage(strCondition);
BindPublishList(strCondition);
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
{
TableCell myTableCell = new TableCell();
myTableCell = e.Item.Cells[8];
LinkButton myDeleteButton = new LinkButton();
// int count = myTableCell.Controls.Count;
myDeleteButton = (LinkButton)myTableCell.Controls[0];
myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除吗?');");
} }
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button myBtnDel = (Button)e.Item.FindControl("BtnDelete"); myBtnDel.Attributes.Add("onclick", "return confirm('您真的要删除?');");
}
}楼上的,我这样写,单击BtnDelete后并没有提示啊?哪里出错了?