在ASP.NET中使用JavaScript腳本添加認(rèn)證
發(fā)表時(shí)間:2023-08-20 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]一)簡(jiǎn)單點(diǎn)的如要在一個(gè)按鈕上增加認(rèn)證腳本,可以這樣<%@ Page Language="C#" %><SCRIPT language="javascr...
一)簡(jiǎn)單點(diǎn)的
如要在一個(gè)按鈕上增加認(rèn)證腳本,可以這樣
<%@ 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>
注意關(guān)鍵的地方btnSubmit.Attributes.Add("onclick","return fffkkk();");這句話,就相當(dāng)于在靜態(tài)頁(yè)面標(biāo)簽上添加 “onclick = “return fffkkk();” 一樣
二)復(fù)雜一點(diǎn)的
有的時(shí)候我們要在DataGrid的刪除列上添加認(rèn)證,可以這樣
首先建一個(gè)DataGrid,然后給她增加一個(gè)刪除列
<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()函數(shù)和第一個(gè)是一樣的
function getconfirm()
{
if (confirm("Do you want to delete record?")==true)
return true;
else
return false;
}
這樣就可以了.