基于服务器端逻辑触发javascript确认(ASP.NET)

发布于 2024-12-06 22:32:43 字数 460 浏览 1 评论 0原文

有没有办法让下面的代码工作?基本上,当我单击 btnOne 时,如果 a 为 true,我希望它弹出确认框。如果我按 yes,那么它将执行 btnTwo 方法。否则,它不会做任何事情。目前,它没有弹出窗口,我不知道为什么。有人可以指出我正确的方向吗?或者可能让我知道实现这一目标的其他方法。

任何信息将不胜感激。

例如:

public void btnTwo(object sender, EventArgs e)
{
    //Do some processing
}

public void btnOne(object sender, EventArgs e)
{
    if( a == true )
        btnTwo.Attributes["onClick"] = "return confirm('test');"

    btnTwo(sender, new EventArgs());
}

Is there a way to make the following code works? Basically, when I click on btnOne, If a is true, i expect it to popup the confirm box. And If i press yes, then it will execute btnTwo method. Otherwise, it will not do anything. At the moment, it doesn't do the popup and i am not sure why. Can someone please point me in the right direction? or possibly let me know other way of achieving this.

Any information would be much appreciated.

For example:

public void btnTwo(object sender, EventArgs e)
{
    //Do some processing
}

public void btnOne(object sender, EventArgs e)
{
    if( a == true )
        btnTwo.Attributes["onClick"] = "return confirm('test');"

    btnTwo(sender, new EventArgs());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池予 2024-12-13 22:32:43

我认为你混合了很多客户端和服务器端?你能在客户端检查“a”(隐藏字段)吗?

或者在检查后面代码中的值后,添加带有确认的启动脚本,然后在“是”上“单击”javascript 中的第二个按钮。

Protected Sub btnSecond_Click(sender As Object, e As System.EventArgs) Handles btnSecond.Click
    Me.lblInfo.Text = "SecondClick is done!"
End Sub

Protected Sub btnFirst_Click(sender As Object, e As System.EventArgs) Handles btnFirst.Click
    a = 10
    Dim action As String = "<script> if(confirm('sure ?')){  document.getElementById('" & btnSecond.ClientID & "').click()} </script>"
    If (a > 5) Then
        Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "startConfirm", action)
    End If
End Sub

和标记:

<form id="form1" runat="server">
<div>
    <asp:Button runat="server" ID="btnFirst" />
    </br>
    <asp:Button runat="server" ID="btnSecond" />
    <asp:Label runat="server" ID="lblInfo" />
</div>
</form>

I think you are mixing to much client and server side? Could you check "a" (hidden field) on client side?

Or after check value in code behind add start up script with confirm and then on yes "click" second button in javascript.

Protected Sub btnSecond_Click(sender As Object, e As System.EventArgs) Handles btnSecond.Click
    Me.lblInfo.Text = "SecondClick is done!"
End Sub

Protected Sub btnFirst_Click(sender As Object, e As System.EventArgs) Handles btnFirst.Click
    a = 10
    Dim action As String = "<script> if(confirm('sure ?')){  document.getElementById('" & btnSecond.ClientID & "').click()} </script>"
    If (a > 5) Then
        Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "startConfirm", action)
    End If
End Sub

And markup:

<form id="form1" runat="server">
<div>
    <asp:Button runat="server" ID="btnFirst" />
    </br>
    <asp:Button runat="server" ID="btnSecond" />
    <asp:Label runat="server" ID="lblInfo" />
</div>
</form>
梦中楼上月下 2024-12-13 22:32:43

确保已到达该代码,并使用 OnClientClick 代替:

protected void Page_Load(object sender, EventArgs e)
{
    bool a = true;        
    if (a)
        btnTwo.OnClientClick = "return confirm(\"Are you sure?\");";
}

Make sure that the code is being reached, and use OnClientClick instead:

protected void Page_Load(object sender, EventArgs e)
{
    bool a = true;        
    if (a)
        btnTwo.OnClientClick = "return confirm(\"Are you sure?\");";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文