Messagebox asp.net c#

本文关键字:net asp Messagebox | 更新日期: 2023-09-27 18:06:37

int approvalcount; 
if (approvalcount > 0)
{
    string script = @"confirm('Click OK or Cancel to Continue') ;";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "confirm_entry", script, true);
}
else
{
    return true;
}

我需要帮助上面的代码。如果单击确定需要返回true或单击取消需要返回false。我怎样才能得到返回值?在asp.net和c#中还有其他方法来显示消息框吗?

approvalcount是int类型变量。

Messagebox asp.net c#

如果你想在客户端返回值,那么使用return关键字

  string script = @"return confirm('Click OK or Cancel to Continue') ;";

补充:
我想我误解了你的问题。您希望客户端在服务器端使用true/false值。这可以通过某些调整来实现。下面是代码

    protected void Page_Load(object sender, EventArgs e)
    {
        bool a = false; //initialize the default value
        //this will be called later
        if (Request["val"] != null)
        {
            if (Request["val"].ToString() == "true")
                a = true;
            else
                a = false;
        }
        else
        {
            // this will be called first
            a = somefunction();
        }
    }
    public bool somefunction()
    {
        int approvalcount = 1;
        if (approvalcount > 0)
        {
            string script = @"  if(confirm('Click OK or Cancel to Continue')) { 
                                    document.location='default.aspx?val=true';
                                } else { 
                                    document.location='default.aspx?val=false';
                                }";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "confirm_entry", script, true);
            return false;
        }
        else
        {
            return true;
        }
    }