Using JavaScript scripts in ASP. NET

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.