熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

如何在ASP.NET中使用JavaScript腳本

2022-06-13   來源: .NET編程 

  一簡單點的
  如要在一個按鈕上增加認證腳本可以這樣
  
  <%@ 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) {
  
  btnSubmitAttributesAdd(onclickreturn getconfirm (););
  
  }
  
  void btnSubmit_Click(object sender EventArgs e) {
  
      MessageText = You entered your name as: + txtNameText;
  
  }
  
  </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>
  
  注意關鍵的地方btnSubmitAttributesAdd(onclickreturn fffkkk(););這句話就相當於在靜態頁面標簽上添加 onclick = return fffkkk(); 一樣
  
  二 復雜一點的
  有的時候我們要在DataGrid的刪除列上添加認證可以這樣
  
  首先建一個DataGrid然後給她增加一個刪除列
  
  <asp:DataGrid id=DataGrid 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>
  
  然後在DataGrid 的ItemDataBound事件中這樣寫
  
  Private Sub DataGrid_ItemDataBound
  
  (ByVal sender As Object ByVal e As DataGridItemEventArgs)
  
  Handles DataGridItemDataBound
  
    Dim l As LinkButton
  
    If eItemItemType = ListItemTypeItem Or
  
    eItemItemType = ListItemTypeAlternatingItem Then
  
    l = CType(eItemCells()FindControl(cmdDel) LinkButton)
  
    lAttributesAdd(onclick return getconfirm();)
  
    End If
  
  End Sub
  
  Getconfirm()函數和第一個是一樣的
  
  function getconfirm()
  
  {
  
  if (confirm(Do you want to delete record?)==true)
  
  return true;
  
  else
  
  return false;
  
  }
From:http://tw.wingwit.com/Article/program/net/201311/13814.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.