在html中
<HTML>
<HEAD>
<script language="javascript">
function delete_confirm(e) {
if (event.srcElement.outerText == "删除")
event.returnValue =confirm("您确认要删除?");
}
document.onclick=delete_confirm;
</script>
....
....
....
<HTML>
<HEAD>
<script language="javascript">
function delete_confirm(e) {
if (event.srcElement.outerText == "删除")
event.returnValue =confirm("您确认要删除?");
}
document.onclick=delete_confirm;
</script>
....
....
....
解决方案 »
- No persister for: test.model.Person
- asp.net问题
- VS2010没有SVN的功能
- 请问如何控制textbox的visible属性,style.displye吗??
- 能不能查看到showModalDialog 后面的Html代码
- 使用treeview老出错!急等大侠救命啊!
- DotNet调试错误:试图运行项目时出错: 无法在 web 服务器上启动调试.访问被拒绝
- 那位大侠有在datagrid中有一个按钮,点击这个按钮,就弹出一个窗口,在这个窗口中
- 虚拟主机.net4.0 发布MVC3问题!
- 怎么样在datalist绑定的时候来判断当数据库是某字段为1 时,datalist控件中的RadioButton checked为true 否则为false
- 人 员 招 聘
- 急!!!在TreeView的oncheck事件中怎样得到checkbox被选中的节点?
Introduction
ASP.NET DataGrid allows you to provide select, edit, cancel and delete buttons. These buttons trigger corresponding events on the server side. These buttons are nothing but button controls (link button or push button) with CommandName property set to select, update, cancel or delete. In this article we will focus on delete command alone. Many times we need to get confirmation from the user about deletion of the record. DataGrid do not provide any inbuilt way to do that. We will see how to prompt the user to confirm the delete using JavaScript.
The web form
We will create a simple web form that contains a DataGrid which in turn is bound to the Employee table from Northwind database. The grid has a column Delete that triggers the DeleteCommand event handler. When the user clicks on the Delete button he will be prompted for confirmation. If he clicks on ok the form is posted as usual else the form will not be posted. The task of prompting the user is carried out via client side JavaScript.
The Delete button
If you are using VS.NET you can easily add Delete button using the property builder. Note however that this adds a ButtonColumn to the grid. In order to add javascript to a client side event you have to use Attributes collection of the control. The button column do dot have an ID attribute and hence can not be used for this purpose. Hence, we need to use template column as shown below.
<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="cmdDel"
runat="server" Text="Delete"
CommandName="Delete" CausesValidation="false">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>Next we need to add the javascript to each and every Delete link button (one per row). We will do that in ItemDataBound event handler.
Private Sub DataGrid1_ItemDataBound
(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Handles DataGrid1.ItemDataBound
Dim l As LinkButton
If e.Item.ItemType = ListItemType.Item Or
e.Item.ItemType = ListItemType.AlternatingItem Then
l = CType(e.Item.Cells(0).FindControl("cmdDel"), LinkButton)
l.Attributes.Add("onclick", "return getconfirm();")
End If
End SubHere we check whether the item (row) is of type Item or AlternatingItem. We then add DHTML OnClick event handler that calls a client side function getconfirm().
The Confirmation JavaScript function
The getconfirm function looks like this:
function getconfirm()
{
if (confirm("Do you want to delete record?")==true)
return true;
else
return false;
}This function displays a javascript confirmation dialog to the user. If user clicks on Ok the function returns true and the form is posted back as usual. If user clicks on cancel it returns false indicating event cancellation. This will cancel the postback of the form.
I hope it was interesting. See you soon.
请问mbm兄:对话框中的“你确认要删除?”前面的符号“?”,但如何做到的“!”和“X”的符号
js还有其他的两个函数,alert和prompt,它们各自有自己的图标,同样不能更改。
不要和window编程下的messagebox()函数拿参数控制图标混淆
结帖!!!!!!!!!!