我试图从数据库中获取库存产品的 TotalQuantity,并将其用作范围验证器的最大值,同时将产品添加到购物车。我在前端使用了代码:
<td class="style2">
QUANTITY<br />
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="must fill some value"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="must fill value lesser than the Total Quantity in stock"
MaximumValue="<%=maxValue %>" MinimumValue="1" Display="Dynamic" ></asp:RangeValidator>
<br />
后端cs代码为:
public partial class productDetail : System.Web.UI.Page
{
public int maxValue=0;
public int minValue = 1;
.....
protected void Page_Load(object sender, EventArgs e)
{
//Defined SQL Conn string...
connection1.Open();
string cartCmd = "select TotalQuantity from Product where ProductName= \'"+prodName+"\';";
SqlCommand cmd = new SqlCommand(cartCmd, connection1);
SqlDataReader readerTotQquantity = cmd.ExecuteReader();
if (readerTotQquantity .HasRows)
{
readerTotQquantity .Read();
TotalQuantity = readerTotQquantity ["TotalQuantity"].ToString();
}
double val = Convert.ToDouble(TotalQuantity);
maxValue = (int)val;
// also tried maxValue=Convert.ToInt32(TotalQuantity);
// tired maxValue=int.Parse(TotalQuantity); etc
connection1.Close();
}
}
我厌倦了在 Visual Studio 输出面板上打印该值,它显示正确的值和类型为 System.Int32。但页面上的错误显示“最大值 <%=maxValue %> 不能小于 RangeValidator1 的最小值 1”。
我还尝试了几次将 Type="Integer" 添加/删除到 RangeValidator 但错误仍然存在。请帮助我,因为这花了我几个小时的时间试图找出问题所在。