Use JavaScript scripting methods in ASP.NET

  • 2020-03-29 23:42:19
  • OfStack

A) something simpler
To add an authentication script to a button, do this

<%@ Page Language="C#" %>
<SCRIPT language="javascript">

function getconfirm ()
    if (confirm("Do you want to delete record?")==true) 
    return true;
    return false; 


<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;
<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"/>

Note the key btnsubmit.attributes.Add("onclick","return FFFKKK ();" ); This is equivalent to adding "onclick =" return FFFKKK (); "to a static page tag. The same
B) a little more complicated
  Sometimes we want to add authentication to the deleted column of the DataGrid, which can be done
  First build a DataGrid, then add a delete column to her


<asp:DataGrid id="DataGrid1" runat="server">
<asp:LinkButton id="cmdDel" 
runat="server" Text="Delete" 
CommandName="Delete" CausesValidation="false">

Then write this in the ItemDataBound event of the DataGrid

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() The function is the same as the first one 
function getconfirm() 
if (confirm("Do you want to delete record?")==true) 
return true; 
return false; 

Related articles: