1) Simple
To add an authentication script to a button, you can
Copy codeThe Code is as follows:
<% @ 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>
Note the key points btnSubmit. attributes. add ("onclick", "return fffkkk ();"); this statement is equivalent to adding "onclick =" return fffkkk (); "to the static page tag.
2) more complex
Sometimes we need to add authentication in the delete column of the DataGrid.
First, create a DataGrid and add a delete column to her.
Copy codeThe Code is as follows:
<Asp: DataGrid id = "DataGrid1" runat = "server">
<Columns>
<Asp: TemplateColumn>
<ItemTemplate>
<Asp: LinkButton id = "mongodel"
Runat = "server" Text = "Delete"
CommandName = "Delete" CausesValidation = "false">
</Asp: LinkButton>
</ItemTemplate>
</Asp: TemplateColumn>
</Columns>
</Asp: DataGrid>
Then write in the ItemDataBound event of the DataGrid as follows:
Copy codeThe Code is as follows:
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 ("mongodel"), LinkButton)
L. Attributes. Add ("onclick", "return getconfirm ();")
End If
End Sub
The Getconfirm () function is the same as the first one.
Function getconfirm ()
{
If (confirm ("Do you want to delete record? ") = True)
Return true;
Else
Return false;
}