【问题标题】:How to call a bool on click of a button如何在单击按钮时调用布尔值
【发布时间】:2016-06-01 03:05:34
【问题描述】:

我想在单击按钮时插入或调用布尔函数。

据说,这是布尔:

private bool IsCritical(int ProductID, int Quantity, int Available, int Criticallevel){
    bool existing = true;

    cn.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    cmd.CommandText = "SELECT Products.CriticalLevel, Products.Available, OrderDetails.Quantity FROM Products, OrderDetails WHERE ProductID=@ProductID";
    cmd.Parameters.AddWithValue("@productid", ProductID);


    cmd.Parameters.AddWithValue("@productid",ProductID);

    SqlDataReader dr = cmd.ExecuteReader();

    if(Available < Quantity)
    {
        return false;
    }
    else
    {
        int currentQty = Available - Quantity;
        if(currentQty >= Criticallevel)

        {
            return false;
        }
    else return true;
    }

    cn.Close();

    return existing;
}

但我找不到调用它的方法。

protected void btnAdd_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Account/AddToCart.aspx?ID=" + 
    Request.QueryString["ID"].ToString() + 
    "&qty=" + txtQty.Text);
}

数量对应于客户购买的一种产品的数量。 数量应低于或等于临界水平。

我想要发生的是,当我单击“添加”按钮时,布尔值将运行。如果 Quantity 低于或等于临界水平,则程序应继续 Add_Click 事件中的“Response.Redirect”。如果不是,则应显示一条错误消息,表明已达到临界级别。

【问题讨论】:

    标签: c# events onclick boolean


    【解决方案1】:

    试试这个:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Change ProductID, Quantity, Available, Criticallevel to actual values
        if (!IsCritical(ProductID, Quantity, Available, Criticallevel))
        {
            Response.Redirect("~/Account/AddToCart.aspx?ID=" + 
            Request.QueryString["ID"].ToString() + 
            "&qty=" + txtQty.Text);
        }
        else
        {
            //Write error message here
        }
    }
    

    【讨论】:

    • 感谢您的帮助 :) 但是,我可以知道您所说的“实际值”是什么意思吗?
    • 我不知道您为 IsCritical 方法传递的值是什么,这就是为什么您需要更改为实际传递的值。
    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2019-10-10
    • 2020-01-29
    • 2019-04-04
    • 2016-04-05
    相关资源
    最近更新 更多