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;
    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 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">
<Columns>
<asp:TemplateColumn>
  <ItemTemplate>
<asp:LinkButton id="cmdDel" 
runat="server" Text="Delete" 
CommandName="Delete" CausesValidation="false">
</asp:LinkButton>
  </ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

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; 
else 
return false; 
}


Related articles: