簡體   English   中英

ASP.net Javascript在asp:ImageButton上觸發

[英]ASP.net javascript fire on asp:ImageButton

嘿,我試圖弄清楚為什么我的頁面加載時出現此錯誤:

 Compiler Error Message: BC30456: 'showNotifier' is not a member of 'ASP.pro_aspx'.

showNotifier是一個JavaScript調用,我必須顯示一條錯誤消息,該錯誤消息使用jQuery從瀏覽器屏幕的頂部下拉。 我的主頁上加載了javascript代碼,因此我在任何頁面上都知道它。

這是我的imagebutton代碼:

 <asp:ImageButton ID="btnDelete" CommandName="Delete" CommandArgument='<%#Eval("id") %>' Text="Delete" runat="server" CssClass="delIcon" AlternateText="Delete" ToolTip="Delete" ImageUrl="~/del.png" />

任何幫助將是巨大的!

附加代碼

Protected Sub grdView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdView.RowCommand
    Dim cid As String = e.CommandArgument.ToString
    Dim command As String = e.CommandName.ToString

    If cid.Length <= 0 Then Exit Sub

    Select Case command
        Case "Delete"
            Delete(cid)
    End Select
End Sub

附加代碼2

Private Sub grdView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdView.RowDataBound
    If (e.Row.RowType = DataControlRowType.DataRow) Then
        Dim l As ImageButton = CType(e.Row.FindControl("btnDelete"), ImageButton)

        l.Attributes("OnClientClick") = "if(msgBoxShow('Are you sure you want to delete " & DataBinder.Eval(e.Row.DataItem, "Name") & "?','" & DataBinder.Eval(e.Row.DataItem, "Name") & "','')==false){return false;}"
    End If
End Sub

JavaScript代碼是這樣的:

<script type="text/javascript">
    function msgBoxShow(boxsaying, prodItemName, codeBehind) {
    apprise(boxsaying, {
        'verify': true,
        'textYes': 'Delete ' + prodItemName,
        'textNo': 'Cancel delete'
    },
                function (r) {
                    if (r) {
                        //alert('yes');
                        return true;
                    } else {
                        //alert('no');
                        return false;
                    }
                }
           );
}
</script>

<asp:ImageButton>中的OnClick用於標識代碼中的功能,該功能將在您單擊並將頁面回發到服務器時運行(與普通的<asp:Button>

您的意思是OnClientClick ,它將在客戶端上運行javascript


根據OP的評論進行更新

為了僅在您的本地javascript函數返回false時返回,請執行以下操作...

OnClientClick="if(myFn()==false){return false;}"

這意味着,當函數返回false ,它將取消單擊按鈕的操作,這意味着將不發布表單(這將導致不運行任何客戶端驗證,如果有的話)。

如果該函數返回true則它將繼續進行並回發到您的服務器。

關於在服務器上調用正確的功能,我將專門為圖像按鈕創建一個功能,例如...

Protected Sub imgBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
   ...
End Sub

然后,您想擁有OnClick="imgBtn_Click"

元素的onClick屬性(尤其是在頁面上的數百個元素上)實際上是一種古老的處理方式。 尤其是如果您關心較舊的瀏覽器(和IE6-8),它們會為基本上都調用相同事物且屬於相同類型元素的每個元素創建“腳本”塊(每個單獨一個)。 這些在舊版瀏覽器中會極大地減慢頁面速度,我強烈建議不要使用它們。

最好在所有這些項目上簡單地創建一個整體實時事件處理程序,如下所示:

$(document).on('click', '.delIcon', function () {
    showNotifier(3000,'#cf5050','Delete this?');
});

這將創建一個事件,等待所有這些項目(使用delIcon類)。 我假設它也可以解決您的問題。 ASP.NET假定showNotifier是一種后端方法。

ImageButtonOnClick旨在在代碼隱藏而不是客戶端腳本中引用事件處理程序。 您應該改用OnClientClick

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM