如何在后端运行javascript ?



我想运行这个确认脚本,但它不适合HTML和asp.net按钮。它只显示"你点击取消"。有人能帮我一下吗?这是我的代码。

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<button id="btnperfdel" runat=server type="button" class="btn btn-outline-secondary" onclientclick="confirm()"><i class="mdi mdi-delete">Delete</i> </button>                                   
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="confirm()"/>                    
</div>
<script type = "text/javascript">
function confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Confirm Delete?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
Here's my backend 

Protected Sub btnperfdel_Click(ByVal sender As Object, ByVal e As System.EventArgs)处理btnperfdel。ServerClick

Dim confirmValue As String = ""
confirmValue = Request.Form("confirm_value")
If confirmValue = "Yes" Then
Label1.Visible = False
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim confirmValue As String = ""
confirmValue = Request.Form("confirm_value")
If confirmValue = "Yes" Then
Label1.Visible = False
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
End If
End Sub    

和你使用相同的名称为一个函数作为内置的js函数名称确认?

那是一个"very"坏主意。

试着给你的js函数名起一个和内置函数不一样的名字。

像这样说:

<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="myconfirm()"/>                    
<script type = "text/javascript">
function myconfirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Confirm Delete?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>

所以,这看起来是一个非常糟糕的函数名

还要注意,您可以"返回";真/假按钮点击。我认为在99%的情况下,用户单击取消,然后您不希望代码运行,并且不需要进一步的操作。所以你可以这样做:

<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="return myconfirm2()"/>                    
<script type = "text/javascript">
function myconfirm2() {
return confirm("Confirm Delete?");
}

如果您执行上述操作,则如果用户单击cancel,则按钮单击不会运行。

事实上,你甚至可以转储所有的代码,这样做:

<asp:Button ID="Button1" runat="server" Text="Button" 
onclientclick="return confirm('confirm delete');"/>  

因此,如果您为客户端单击事件返回true或false,则服务器端代码按钮将不会运行。在十分之九的情况下,你不需要知道用户是否点击了取消,只需要知道我们不运行服务器端按钮代码。

无论如何,更改confirm的名称,因为这是js内建的函数名,使用相同的名称是非常令人困惑的。

编辑 :====================================================

我建议你试试这个:

<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="myconfirm()"/>                    
<asp:HiddenField ID="myanswer" runat="server" ClientIDMode="Static" />
<script type = "text/javascript">
function myconfirm() {
var myanswer = document.getElementById("myanswer")
if (confirm("Confirm Delete?")) {
myanswer.value = "Yes"
}
else {
myanswer.value = "No"
}
}
</script>

后面的代码是:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If myanswer.Value = "Yes" Then
' do whatever here for yes
Debug.Print("yes")
Else
' do whtever here for no
Debug.Print("no")
End If
End Sub

最新更新