如何在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;
 

posted @ 2006-12-19 11:43  '.Elvis.'  阅读(113)  评论(0编辑  收藏  举报