Ways to use JavaScript scripts in asp.net _javascript tips

Source: Internet
Author: User

A simple point of
To add an authentication script to a button, you can

Copy Code code 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>
<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>

Attention to key places BtnSubmit.Attributes.Add ("onclick", "return Fffkkk ();"); This sentence is equivalent to adding "onclick =" return Fffkkk () on the tab of static pages.
II) a bit more complicated
Sometimes we have to add authentication on the Delete column of the DataGrid, so you can
First build a DataGrid and then add a delete column to her

Copy Code code as follows:

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

And then write that in the ItemDataBound event in the DataGrid.

Copy Code code 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 ("Cmddel"), 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.