在ASP.NET中使用JavaScript脚本添加认证
【打印文章】
一)简单点的
如要在一个按钮上增加认证脚本,可以这样
<%@ Page Language="C#" %>
<SCRIPT language="javascript">
function getconfirm ()
{
if (confirm("Do you want to delete record?")==true)
return true;
else
return false;
}
</SCRIPT>
<script runat="server">
public void Page_Load(Object sender, EventArgs E) {
btnSubmit.Attributes.Add("onclick","return getconfirm ();");
}
void btnSubmit_Click(object sender, EventArgs e) {
Message.Text = "You entered your name as: " + txtName.Text;
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
Name: <asp:Textbox id="txtName" runat="server"/>
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button><br/>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
注意关键的地方btnSubmit.Attributes.Add("onclick","return fffkkk();");这句话,就相当于在静态页面标签上添加 “onclick = “return fffkkk();” 一样
二)复杂一点的
有的时候我们要在DataGrid的删除列上添加认证,可以这样
首先建一个DataGrid,然后给她增加一个删除列
<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>
然后在DataGrid 的ItemDataBound事件中这样写
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 Sub
Getconfirm()函数和第一个是一样的
function getconfirm()
{
if (confirm("Do you want to delete record?")==true)
return true;
else
return false;
}
这样就可以了.
如要在一个按钮上增加认证脚本,可以这样
<%@ Page Language="C#" %>
<SCRIPT language="javascript">
function getconfirm ()
{
if (confirm("Do you want to delete record?")==true)
return true;
else
return false;
}
</SCRIPT>
<script runat="server">
public void Page_Load(Object sender, EventArgs E) {
btnSubmit.Attributes.Add("onclick","return getconfirm ();");
}
void btnSubmit_Click(object sender, EventArgs e) {
Message.Text = "You entered your name as: " + txtName.Text;
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
Name: <asp:Textbox id="txtName" runat="server"/>
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button><br/>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
注意关键的地方btnSubmit.Attributes.Add("onclick","return fffkkk();");这句话,就相当于在静态页面标签上添加 “onclick = “return fffkkk();” 一样
二)复杂一点的
有的时候我们要在DataGrid的删除列上添加认证,可以这样
首先建一个DataGrid,然后给她增加一个删除列
<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>
然后在DataGrid 的ItemDataBound事件中这样写
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 Sub
Getconfirm()函数和第一个是一样的
function getconfirm()
{
if (confirm("Do you want to delete record?")==true)
return true;
else
return false;
}
这样就可以了.
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】